SamsJavaThreadProgramming

SamsJavaThreadProgramming, IT

[ Pobierz całość w formacie PDF ]
//-->1Java ThreadProgrammingJava Thread ProgrammingPaul HydeCopyright © 1999 Sams PublishingJava Thread ProgrammingPaul HydeCopyright © 1999 by Sams PublishingExecutive EditorTim RyanAcquisitions EditorSteve AnglinDevelopment EditorsTiffany TaylorJon SteeverManaging EditorJodi JensenSenior EditorSusan Ross MooreCopy EditorsMargaret BersonKate TalbotIndexerRebecca HornyakProofreadersMona BrownJill MazurczykTechnical EditorsAlexandre CalsavaraMike ForsytheTeam CoordinatorKaren OpalSoftware Development SpecialistMichael HunterInterior DesignAnne JonesCover DesignAnne JonesCopy WriterEric BorgertLayout TechniciansStacey DeRomeAyanna LaceyHeather MillerAll rights reserved. No part of this book shall be reproduced, stored in a retrieval system, ortransmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,without written permission from the publisher. No patent liability is assumed with respect tothe use of the information contained herein. Although every precaution has been taken in thepreparation of this book, the publisher and author assume no responsibility for errors oromissions. Neither is any liability assumed for damages resulting from the use of theinformation contained herein.12International Standard Book Number: 0-672-31585-80100994321TrademarksAll terms mentioned in this book that are known to be trademarks or service marks havebeen appropriately capitalized. Sams Publishing cannot attest to the accuracy of thisinformation. Use of a term in this book should not be regarded as affecting the validity of anytrademark or service mark.Sun, Sun Microsystems, SunWorld, Java, and HotJava are trademarks or registeredtrademarks of Sun Microsystems, Inc.TocIntroductionJava ThreadProgrammingPaul HydeCopyright © 1999 Sams PublishingIntroductionStructure of This Book23This book is for those of you who have started working with Java and have realized that youwant to develop multithreaded applications and applets. I don’t assume that you knowanything about thread programming, so the book starts off with simple, straightforwardexamples. From there, the chapters become more advanced and comprehensively cover allaspects of thread programming in Java. The second part of the book is dedicated todemonstrating various advanced techniques that can be used in the real world. Chapters 1through 10 can be read in order because each chapter builds upon the concepts in thepreceding one. You can hop around the techniques in Chapters 11 through 18, reading themin just about any order. Some of the techniques are so valuable that they are used indemonstrating other techniques, so you can read up on each technique as you come acrossit.I developed the example code used in this book using theJava 2 SDK, Standard Edition,version 1.2 (also known as JDK 1.2)from Sun Microsystems. I used this development kit onan Intel Pentium 166MHz machine running Microsoft Windows 95. In this book, some of thestatements in the code listings appear in bold-face type simply for emphasis. These sourcecode files are available for download fromwww.samspublishing.com.When you reachthat 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).TocJava Thread Programmingby Paul HydeSams© 1999, 510 pagesLearn how to use threads for faster, more efficient Javaprogramming.ISBN: 0672315858Table of ContentsSource CodeSynopsisby Rebecca RohanBack CoverProfessional Java developers who've come as far as they can withoutexploiting threads will find their skills bumped up a few notches by thetime they finish Paul Hyde'sJava Thread Programming.In afive-and-a-half-page first chapter, the book gives a basic concept briefing,then gets down to business with an example-rich education from thestarting thread through inter-thread communication, thread groups, threadpooling, threads and Swing, and more. You'll get an experienced voice onhow to gracefully exit from a thread -- and find out when to use thelead-between-the-eyes SureStop class instead. You'll even find out whenmultiple threads aren't a good idea. If you're serious about learning what ittakes to do Java really, really well, this book is a good place to invest yourtime.Table of ContentsJava Thread ProgrammingIntroductionPart IThreadsChapter 1Chapter 2Chapter 3- Introduction to Threads- A Simple Two-Thread Example- Creating and Starting a Thread34Chapter 4Chapter 5Chapter 6Chapter 7Chapter 8Chapter 9Part II- Implementing Runnable Versus Extending Thread- Gracefully Stopping Threads- Thread Prioritization- Concurrent Access to Objects and Variables- Inter-thread Communication- Threads and SwingTechniquesChapter 10- Thread GroupsChapter 11- Self-Running ObjectsChapter 12- Exception CallbackChapter 13- Thread PoolingChapter 14- Waiting for the Full TimeoutChapter 15- Breaking Out of a Blocked I/O StateChapter 16- The SureStop UtilityChapter 17- The BooleanLock UtilityChapter 18- First-In-First-Out (FIFO) QueuePart IIIAppendixesAppendix A- The Thread APIAppendix B- The ThreadGroup APIBack CoverLearn professional thread management techniques from Paul Hyde, aprofessional Java developer, Sun Certified Programmer for the Java 2Platform, and advanced Java language instructor. Apply the concepts,code, and real-world solutions in this book to make your Java applicationsfaster, more stable, and more robust.Written by a professional software developer for software developers, JavaThread Programming provides a code-intensive, solution-orientedapproach to mastering threads.LEARN THE CONCEPTS AND BUILD THE APPLICATIONS•Start by learning the basics of multithreaded programming in Java andwork up to the more advanced concepts.•Suitable tutorial for Java developers that have never worked with threadsbefore, and an excellent reference and source of proven, advancedtechniques for Java developers who have had experience working withthreads.•Explains howvolatileandsynchronizedshould be used to controlconcurrent access to objects and variables and how to avoid deadlocks.•Discusses how to implement safe and efficient inter-threadcommunications using thewait/notifymechanism.•Explains how thread prioritization and scheduling affect the execution ofthreads within an application.•Discusses pros and cons to different approaches and teaches you how tochoose the best solutions.•Covers the proper use of threads and Swing, and shows how to usethreads 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 SDKversion 1.2.1 and discusses the differences form JDK 1.1 and JDK 1.0.About the Authors45Paul Hyde is a professional Java developer and Sun Certified Programmerfor the Java 2 Platform. He began developing Java applications at AT&Tin 1996 and now has over three years of on-the-job Java developmentexperience. Paul is currently a senior Java consultant for ProgramixIncorporated, the consulting company he founded in early 1997. He alsodevelops and teaches introductory to advanced Java courses for ITprofessionals and developers.Chapter 1 - Introduction toThreadsJava Thread ProgrammingPaul HydeCopyright © 1999 Sams PublishingChapter 1:Introduction to ThreadsOverviewIsn’t it nice to be able to read and scroll the text of a Web page while the graphics continue toload? How about having a document in a word processor print in the background while youopen another document for editing? Perhaps you’ve enjoyed writing a response to an emailmessage while another incoming message with a large file attached is quietly downloadedsimultaneously? Threads make all this convenient functionality possible by allowing amultithreaded program to do more than one task at a time. This book helps you learn theskills and techniques necessary to incorporate that kind of useful functionality into your Javaprograms.TocChapter 1 - Introduction toThreadsJava Thread ProgrammingPaul HydeCopyright © 1999 Sams PublishingWhat Is a Thread?When a modern operating system wants to start running a program, it creates a newprocess. Aprocessis a program that is currently executing. Every process has at least onethread running within it. Sometimes threads are referred to aslightweight processes.Athreadis a path of code execution through a program, and each thread has its own localvariables, program counter (pointer to the current instruction being executed), and lifetime.Most modern operating systems allow more than one thread to be running concurrentlywithin a process. When the Java Virtual Machine (JavaVM, or just VM) is started by theoperating system, a new process is created. Within that process, many threads can bespawned(created).Normally, you would think of Java code execution starting with themain()method andproceeding in a path through the program until all the statements inmain()are completed.This is an example of a single thread. This “main” thread is spawned by the JavaVM, which5 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • jutuu.keep.pl