Passing a Runnable Object to a Threads Constructor

Chapter 4 - Implementing Runnable Versus Extending Thread

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Summary
This chapter shows that extending Thread is not always an option and that a second way to allow a thread to run within a class is to have it implement Runnable . In fact, in most cases, implementing Runnable is preferable to extending Thread .
Here are a few other lessons learned:
  Do not use the event handling thread to perform long-running tasks; it should be allowed to return to the business of handling events relatively quickly. For the long tasks , use a worker thread. This is critical if a Stop or Cancel Request button exists and could be pressed before the original task completes.
  Proper use of the volatile keyword is not trivial and is necessary in many cases to guarantee desired code execution behavior when two or more threads access the same member variable. Chapter 7 explains volatile in detail.
  The amount of time it takes to execute even a few statements is not predictable. This time can become significant if the statements are executed over and over in a loop. When accuracy is important, you should check the system clock to see how much real time actually elapsed and then make adjustments accordingly .

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net