SamsJavaThreadProgramming.pdf

(1602 KB) Pobierz
1
Java Thread
Programming
Java Thread Programming
Paul Hyde
Copyright © 1999 Sams Publishing
Java Thread Programming
Paul Hyde
Copyright © 1999 by Sams Publishing
Executive Editor
Tim Ryan
Acquisitions Editor
Steve Anglin
Development Editors
Tiffany Taylor
Jon Steever
Managing Editor
Jodi Jensen
Senior Editor
Susan Ross Moore
Copy Editors
Margaret Berson
Kate Talbot
Indexer
Rebecca Hornyak
Proofreaders
Mona Brown
Jill Mazurczyk
Technical Editors
Alexandre Calsavara
Mike Forsythe
Team Coordinator
Karen Opal
Software Development Specialist
Michael Hunter
Interior Design
Anne Jones
Cover Design
Anne Jones
Copy Writer
Eric Borgert
Layout Technicians
Stacey DeRome
Ayanna Lacey
Heather Miller
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or
transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,
without written permission from the publisher. No patent liability is assumed with respect to
the use of the information contained herein. Although every precaution has been taken in the
preparation of this book, the publisher and author assume no responsibility for errors or
omissions. Neither is any liability assumed for damages resulting from the use of the
information contained herein.
1
2
International Standard Book Number: 0-672-31585-8
01
00
99
4
3
2
1
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have
been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this
information. Use of a term in this book should not be regarded as affecting the validity of any
trademark or service mark.
Sun, Sun Microsystems, SunWorld, Java, and HotJava are trademarks or registered
trademarks of Sun Microsystems, Inc.
Toc
Introduction
Java Thread
Programming
Paul Hyde
Copyright © 1999 Sams Publishing
Introduction
Structure of This Book
2
3
This book is for those of you who have started working with Java and have realized that you
want to develop multithreaded applications and applets. I don’t assume that you know
anything about thread programming, so the book starts off with simple, straightforward
examples. From there, the chapters become more advanced and comprehensively cover all
aspects of thread programming in Java. The second part of the book is dedicated to
demonstrating various advanced techniques that can be used in the real world. Chapters 1
through 10 can be read in order because each chapter builds upon the concepts in the
preceding one. You can hop around the techniques in Chapters 11 through 18, reading them
in just about any order. Some of the techniques are so valuable that they are used in
demonstrating other techniques, so you can read up on each technique as you come across
it.
I developed the example code used in this book using the
Java 2 SDK, Standard Edition,
version 1.2 (also known as JDK 1.2)
from Sun Microsystems. I used this development kit on
an Intel Pentium 166MHz machine running Microsoft Windows 95. In this book, some of the
statements in the code listings appear in bold-face type simply for emphasis. These source
code files are available for download from
www.samspublishing.com.
When you reach
that page, click the Product Support link. On the next page, enter this book’s ISBN number
(0672315858) to access the page containing the code.
The following is an overview of what is covered in each of the chapters (not available).
Toc
Java Thread Programming
by Paul Hyde
Sams
© 1999, 510 pages
Learn how to use threads for faster, more efficient Java
programming.
ISBN: 0672315858
Table of Contents
Source Code
Synopsis
by Rebecca Rohan
Back Cover
Professional Java developers who've come as far as they can without
exploiting threads will find their skills bumped up a few notches by the
time they finish Paul Hyde's
Java Thread Programming.
In a
five-and-a-half-page first chapter, the book gives a basic concept briefing,
then gets down to business with an example-rich education from the
starting thread through inter-thread communication, thread groups, thread
pooling, threads and Swing, and more. You'll get an experienced voice on
how to gracefully exit from a thread -- and find out when to use the
lead-between-the-eyes SureStop class instead. You'll even find out when
multiple threads aren't a good idea. If you're serious about learning what it
takes to do Java really, really well, this book is a good place to invest your
time.
Table of Contents
Java Thread Programming
Introduction
Part I
Threads
Chapter 1
Chapter 2
Chapter 3
- Introduction to Threads
- A Simple Two-Thread Example
- Creating and Starting a Thread
3
4
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Part II
- Implementing Runnable Versus Extending Thread
- Gracefully Stopping Threads
- Thread Prioritization
- Concurrent Access to Objects and Variables
- Inter-thread Communication
- Threads and Swing
Techniques
Chapter 10
- Thread Groups
Chapter 11
- Self-Running Objects
Chapter 12
- Exception Callback
Chapter 13
- Thread Pooling
Chapter 14
- Waiting for the Full Timeout
Chapter 15
- Breaking Out of a Blocked I/O State
Chapter 16
- The SureStop Utility
Chapter 17
- The BooleanLock Utility
Chapter 18
- First-In-First-Out (FIFO) Queue
Part III
Appendixes
Appendix A
- The Thread API
Appendix B
- The ThreadGroup API
Back Cover
Learn professional thread management techniques from Paul Hyde, a
professional Java developer, Sun Certified Programmer for the Java 2
Platform, and advanced Java language instructor. Apply the concepts,
code, and real-world solutions in this book to make your Java applications
faster, more stable, and more robust.
Written by a professional software developer for software developers, Java
Thread Programming provides a code-intensive, solution-oriented
approach to mastering threads.
LEARN THE CONCEPTS AND BUILD THE APPLICATIONS
Start by learning the basics of multithreaded programming in Java and
work up to the more advanced concepts.
Suitable tutorial for Java developers that have never worked with threads
before, and an excellent reference and source of proven, advanced
techniques for Java developers who have had experience working with
threads.
Explains how
volatile
and
synchronized
should be used to control
concurrent access to objects and variables and how to avoid deadlocks.
Discusses how to implement safe and efficient inter-thread
communications using the
wait/notify
mechanism.
Explains how thread prioritization and scheduling affect the execution of
threads within an application.
Discusses pros and cons to different approaches and teaches you how to
choose the best solutions.
Covers the proper use of threads and Swing, and shows how to use
threads to create animation.
Shows you how to use the Collections API in a thread-safe manner.
Comprehensively covers multithread code based on the Java 2 SDK
version 1.2.1 and discusses the differences form JDK 1.1 and JDK 1.0.
About the Authors
4
5
Paul Hyde is a professional Java developer and Sun Certified Programmer
for the Java 2 Platform. He began developing Java applications at AT&T
in 1996 and now has over three years of on-the-job Java development
experience. Paul is currently a senior Java consultant for Programix
Incorporated, the consulting company he founded in early 1997. He also
develops and teaches introductory to advanced Java courses for IT
professionals and developers.
Chapter 1 - Introduction to
Threads
Java Thread Programming
Paul Hyde
Copyright © 1999 Sams Publishing
Chapter 1:
Introduction to Threads
Overview
Isn’t it nice to be able to read and scroll the text of a Web page while the graphics continue to
load? How about having a document in a word processor print in the background while you
open another document for editing? Perhaps you’ve enjoyed writing a response to an email
message while another incoming message with a large file attached is quietly downloaded
simultaneously? Threads make all this convenient functionality possible by allowing a
multithreaded program to do more than one task at a time. This book helps you learn the
skills and techniques necessary to incorporate that kind of useful functionality into your Java
programs.
Toc
Chapter 1 - Introduction to
Threads
Java Thread Programming
Paul Hyde
Copyright © 1999 Sams Publishing
What Is a Thread?
When a modern operating system wants to start running a program, it creates a new
process. A
process
is a program that is currently executing. Every process has at least one
thread running within it. Sometimes threads are referred to as
lightweight processes.
A
thread
is a path of code execution through a program, and each thread has its own local
variables, program counter (pointer to the current instruction being executed), and lifetime.
Most modern operating systems allow more than one thread to be running concurrently
within a process. When the Java Virtual Machine (JavaVM, or just VM) is started by the
operating system, a new process is created. Within that process, many threads can be
spawned
(created).
Normally, you would think of Java code execution starting with the
main()
method and
proceeding in a path through the program until all the statements in
main()
are completed.
This is an example of a single thread. This “main” thread is spawned by the JavaVM, which
5
Zgłoś jeśli naruszono regulamin