1. Trang chủ
  2. » Công Nghệ Thông Tin

Java Threads, Third Edition ppt

1,7K 340 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Java Threads, Third Edition
Chuyên ngành Computer Science
Thể loại Book
Định dạng
Số trang 1.684
Dung lượng 2,95 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

9.1 An Overview of Thread Scheduling 9.2 Scheduling with Thread Priorities 9.3 Popular Threading Implementations 9.4 Summary Chapter 10... Task Scheduling 11.1 Overview of Task Schedu

Trang 2

Java Threads, Third Edition Table of Contents

Copyright

Preface

Who Should Read This Book? Versions Used in This Book What's New in This Edition? Organization of This Book

Conventions Used in This Book Code Examples

How to Contact Us

Safari Enabled

Acknowledgments

Trang 4

2.6 Threads and Objects

Trang 6

6.1 Synchronization Terms 6.2 Synchronization Classes Added in J2SE 5.0

Trang 7

8.4 Using the Collection Classes 8.5 Summary

Chapter 9 Thread Scheduling

Trang 8

9.1 An Overview of Thread Scheduling

9.2 Scheduling with Thread Priorities

9.3 Popular Threading

Implementations

9.4 Summary

Chapter 10 Thread Pools

10.1 Why Thread Pools?

10.2 Executors

10.3 Using a Thread Pool

10.4 Queues and Sizes

10.5 Thread Creation

10.6 Callable Tasks and Future

Trang 9

Results

10.7 Single-Threaded Access 10.8 Summary

Chapter 11 Task Scheduling 11.1 Overview of Task

Scheduling

11.2 The java.util.Timer Class 11.3 The javax.swing.Timer Class

11.4 The

ScheduledThreadPoolExecutor Class

11.5 Summary

Chapter 12 Threads and I/O

Trang 10

12.1 A Traditional I/O Server 12.2 A New I/O Server

13.6 Threads, Stacks, and

Memory Usage

Trang 11

13.7 Summary

Chapter 14 Thread

Performance

14.1 Overview of Performance 14.2 Synchronized Collections 14.3 Atomic Variables and

Contended Synchronization 14.4 Thread Creation and

Thread Pools

14.5 Summary

Chapter 15 Parallelizing Loops for Multiprocessor Machines 15.1 Parallelizing a Single-

Threaded Program

Trang 12

15.2 Multiprocessor Scaling 15.3 Summary

Appendix A Superseded Threading Utilities

A.1 The BusyFlag Class A.2 The CondVar Class A.3 The Barrier Class

A.4 The RWLock Class A.5 The ThreadPool Class A.6 The JobScheduler Class A.7 Summary

Colophon

Index

index_A

Trang 13

index_B index_C index_D index_E index_F index_G index_H index_I index_J index_K index_L index_M index_N index_O

Trang 14

index_P index_Q index_R index_S index_T index_U index_V index_W

Trang 15

Java Threads, Third Edition

By Scott Oaks, Henry Wong

Publisher : O'Reilly

Pub Date : September 2004ISBN : 0-596-00782-5

Trang 16

Pages : 360

Threads are essential to Java programming, but learning to use them effectively is a

nontrivial task This new edition

of the classic Java Threads

shows you how to take full

advantage of Java's threading facilities and brings you up-to- date with the watershed

Trang 17

changes in Java 2 Standard Edition version 5.0 (J2SE 5.0).

It provides a thorough, step approach to threads

step-by-programming.

Trang 18

Java Threads, Third Edition

By Scott Oaks, Henry Wong

Publisher : O'Reilly

Pub Date : September 2004ISBN : 0-596-00782-5

Trang 20

Section 1.3 Why Threads?

Section 1.4 Summary

Chapter 2 Thread Creation and Management Section 2.1 What Is a Thread?

Section 2.2 Creating a Thread

Section 2.3 The Lifecycle of a Thread Section 2.4 Two Approaches to Stopping a

Thread

Section 2.5 The Runnable Interface

Section 2.6 Threads and Objects

Section 2.7 Summary

Chapter 3 Data Synchronization

Section 3.1 The Synchronized Keyword Section 3.2 The Volatile Keyword

Section 3.3 More on Race Conditions Section 3.4 Explicit Locking

Section 3.5 Lock Scope

Section 3.6 Choosing a Locking Mechanism Section 3.7 Nested Locks

Section 3.8 Deadlock

Trang 21

Section 3.9 Lock Fairness

Section 3.10 Summary

Chapter 4 Thread Notification

Section 4.1 Wait and Notify

Section 4.2 Condition Variables

Section 5.2 Atomic Variables

Section 5.3 Thread Local Variables Section 5.4 Summary

Chapter 6 Advanced Synchronization Topics Section 6.1 Synchronization Terms

Section 6.2 Synchronization Classes Added

in J2SE 5.0

Section 6.3 Preventing Deadlock

Section 6.4 Deadlock Detection

Section 6.5 Lock Starvation

Trang 22

Section 6.6 Summary

Chapter 7 Threads and Swing

Section 7.1 Swing Threading Restrictions Section 7.2 Processing on the Event-

Classes

Section 8.3 The Producer/Consumer Pattern Section 8.4 Using the Collection Classes Section 8.5 Summary

Chapter 9 Thread Scheduling

Section 9.1 An Overview of ThreadScheduling

Section 9.2 Scheduling with Thread

Priorities

Trang 23

Section 9.3 Popular Threading

Implementations

Section 9.4 Summary

Chapter 10 Thread Pools

Section 10.1 Why Thread Pools?

Section 10.2 Executors

Section 10.3 Using a Thread Pool

Section 10.4 Queues and Sizes

Section 10.5 Thread Creation

Section 10.6 Callable Tasks and Future

Results

Section 10.7 Single-Threaded Access Section 10.8 Summary

Chapter 11 Task Scheduling

Section 11.1 Overview of Task Scheduling Section 11.2 The java.util.Timer Class Section 11.3 The javax.swing.Timer Class Section 11.4 The

ScheduledThreadPoolExecutor Class Section 11.5 Summary

Trang 24

Chapter 12 Threads and I/O

Section 12.1 A Traditional I/O Server Section 12.2 A New I/O Server

Section 12.3 Interrupted I/O

Chapter 14 Thread Performance

Section 14.1 Overview of Performance Section 14.2 Synchronized Collections

Section 14.3 Atomic Variables and

Contended Synchronization

Section 14.4 Thread Creation and Thread

Trang 25

Section A.2 The CondVar Class

Section A.3 The Barrier Class

Section A.4 The RWLock Class

Section A.5 The ThreadPool Class

Section A.6 The JobScheduler Class Section A.7 Summary

Colophon

Index

Trang 26

Copyright © 2004 O'Reilly Media, Inc.All rights reserved.

Printed in the United States of America

Published by O'Reilly Media, Inc., 1005Gravenstein Highway North, Sebastopol,

Trang 27

Nutshell Handbook, the Nutshell

Handbook logo, and the O'Reilly logo areregistered trademarks of O'Reilly Media,Inc Java Threads, the image of a marineinvertebrate, and related trade dress aretrademarks of O'Reilly Media, Inc

Java and all Java-based trademarks andlogos are trademarks or registered

trademarks of Sun Microsystems, Inc., inthe United States and other countries.O'Reilly Media, Inc is independent of SunMicrosystems, Inc

Many of the designations used by

manufacturers and sellers to distinguishtheir products are claimed as trademarks.Where those designations appear in this

Trang 28

book, and O'Reilly Media, Inc was aware

of a trademark claim, the designationshave been printed in caps or initial caps

While every precaution has been taken inthe preparation of this book, the publisherand authors assume no responsibility forerrors or omissions, or for damages

resulting from the use of the informationcontained herein

Trang 29

buzzwords seemed contradictory: howcould a language that is multithreaded besimple?

Trang 30

It turns out that Java's threading system issimple, at least relative to other threadingsystems This simplicity makes Java'sthreading system easy to learn so that evendevelopers who are unfamiliar with

threads can pick up the basics of threadprogramming with relative ease

In early versions of Java, this simplicitycame with tradeoffs; some of the advancedfeatures that are found in other threadingsystems were not available in Java Java 2Standard Edition Version 5.0 (J2SE 5.0)changes all of that; it provides a largenumber of new thread-related classes thatmake the task of writing multithreadedprograms that much easier

Trang 31

Still, programming with threads remains acomplex task This book shows you how

to use the threading tools in Java to

perform the basic tasks of threaded

programming and how to extend them toperform more advanced tasks for morecomplex programs

Trang 32

Who Should Read This

Book?

This book is intended for programmers ofall levels who need to learn to use threadswithin Java programs This includes

developers who have previously usedJava and written threaded programs; J2SE5.0 includes a wealth of new thread-related classes and features Therefore,even if you've written a threaded program

in Java, this book can help you to exploitnew features of Java to write even moreeffective programs

The first few chapters of the book dealwith the issues of threaded programming

in Java, starting at a basic level; no

Trang 33

assumption is made that the developer hashad any experience in threaded

programming As the chapters progress,the material becomes more advanced, interms of both the information presentedand the experience of the developer thatthe material assumes For developers whoare new to threaded programming, thissequence should provide a natural

progression of the topic

This book is ideally suited to developerstargeting the second wave of Java

programs​more complex programs thatfully exploit the power of Java's threadingsystem We make the assumption thatreaders of the book are familiar withJava's syntax and features In a few areas,

Trang 34

we present complex programs that depend

on knowledge of other Java features:AWT, Swing, NIO, and so on However,the basic principles we present should beunderstandable by anyone with a basicknowledge of Java We've found thatbooks that deal with these other APIs tend

to give short shrift to how multiple threadscan fully utilize these features of Java(though doubtless the reverse is true; wemake no attempt to explain nonthread-related Java APIs)

Though the material presented in this bookdoes not assume any prior knowledge ofthreads, it does assume that the reader hasknowledge of other areas of the Java APIand can write simple Java programs

Trang 35

Versions Used in This Book

Writing a book on Java in the age of

Internet time is hard​the sand on whichwe're standing is constantly shifting Butwe've drawn a line in that sand, and theline we've drawn is at the Java 2 StandardEdition (J2SE) Version 5.0 from SunMicrosystems This software was

previously known as J2SE Version 1.5

It's likely that versions of Java that

postdate this version will contain somechanges to the threading system not

discussed in this edition of the book Wewill also point out the differences

between J2SE 5.0 and previous versions

of Java as we go so that developers using

Trang 36

earlier releases of Java will also be able

to use this book

Most of the new threading features inJ2SE 5.0 are available (with differentAPIs) from third-parties for earlier

versions of Java (including classes wedeveloped in earlier editions of thisbook) Therefore, even if you're not usingJ2SE 5.0, you'll get full benefit from thetopics covered in this book

Trang 37

What's New in This Edition?

This edition includes information aboutJ2SE 5.0 One of the most significantchanges in J2SE 5.0 is the inclusion ofJava Specification Request (JSR) 166,often referred to as the "concurrencyutilities." JSR-166 specifies a number ofthread-related enhancements to existingAPIs as well as providing a large package

Trang 38

A set of classes that provide

threadsafe operations without

Trang 40

threads to run certain tasks

New implementations of existing features

The Java language has always had

Trang 41

the capability to perform data

synchronization and thread

notification However,

implementation of these features wassomewhat limited; you could, forexample, synchronize blocks of code

or entire methods but synchronizingacross methods and classes requiredextra programming In J2SE 5.0,explicit locks and condition

variables allow you more flexibilitywhen using these features

These new implementations do notintroduce new concepts for a

developer A developer who wants

to write a threadsafe program mustensure that her data is correctly

Trang 42

synchronized, whether she uses J2SE5.0's explicit locks or the more basic

synchronized keyword Therefore,

both are presented together when wetalk about data synchronization Thesame is true of condition variables,which provide thread notificationand are discussed alongside Java's

wait( ) and notify( ) methods, and of

queues, which are discussed alongwith Java's other collection classes

Important thread utilities

At some point in time, virtually alldevelopers who write threadedprograms will need to use basic

Trang 43

thread utilities such as a pool or ascheduler; many of them will alsoneed to use advanced synchronizationprimitives A recognition of this fact

is one thing that drove JSR-166​it wascertainly possible in previous

versions of Java to develop yourown thread pools and schedulers Butgiven the importance of threading inthe Java platform, adding these basicutilities greatly increases

programmer productivity

Minimal synchronization utilities

Java's new atomic classes provide ameans by which developers can,

Trang 44

when necessary, write applicationsthat avoid synchronization This canlead to programs that are highlyconcurrent.

If you've read previous editions of thisbook, the concepts presented in the firsttwo categories will be familiar In

previous editions, we developed our owndata synchronization classes, thread pools,and so on In those editions, we explained

in detail how our implementations workedand then used them in several examples Inthis edition, we focus solely on how to usethese classes effectively

The information that falls into the thirdcategory is completely new to this edition

Trang 45

The classes that perform minimal

synchronization require new support fromthe virtual machine itself and could not bedeveloped independent of those changes

Trang 46

Organization of This Book

Here's an outline of the book, which

includes 15 chapters and 1 appendix:

Chapter 1

This chapter forms a basic

introduction to the topic of threads:why they are useful and our approach

to discussing them

Chapter 2

This chapter shows you how to

Trang 47

create threads and runnable objectswhile explaining the basic principles

of how threads work

Chapter 3

This chapter discusses the basiclevel at which threads share datasafely​coordinating which thread isallowed to access data at any time.Sharing data between threads is theunderlying topic of our next fourchapters

Chapter 4

Trang 48

This chapter discusses the basictechnique threads use to communicatewith each other when they have

changed data This allows threads torespond to data changes instead ofpolling for such changes

Chapter 5

This chapter discusses classes andprogramming methods that achievedata safety while using a minimalamount of synchronization

Chapter 6

Trang 49

In this chapter, we complete ourexamination of data sharing andsynchronization with an examination

of deadlock, starvation, and

miscellaneous locking classes

Trang 50

Java collection classes are writtenfor a variety of circumstances Someare threadsafe and some are not, andJ2SE 5.0 introduces new collectionclasses for use specifically withthread utilities We sort all that out inthis chapter.

Chapter 9

Scheduling is the process whereby asingle CPU selects a thread to run.Thread scheduling is more a property

of an operating system (OS) than aJava program, and this chapter

discusses the relationship between

Trang 51

the virtual machine and the OS in thisarea.

Chapter 11

Task schedulers execute a task one or

Trang 52

more times at some point in thefuture This set of classes includestimers (Java has had timer classessince JDK 1.3) and a general taskscheduler available in J2SE 5.0.

Chapter 12

Dealing with I/O is one of the

primary reasons why developers usethreads in Java In this chapter, weuse all of Java's threading features toshow you how to handle I/O

effectively in multithreaded

programs

Ngày đăng: 06/03/2014, 11:20

TỪ KHÓA LIÊN QUAN