| ||||
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 |