In this chapter, we discussed some of the possibilities available to you when using threads, and we pointed out some trouble spots to avoid. We discussed safe threading issues, threads and Swing, blocking, waiting, and a host of other topics.
The best way to understand threading is to design your threading scheme, make predictions about how it will function, and then test those predictions with the handy method in the Thread class, holdsLock(Object). Thread’s getState() and getStackTrace() methods can be very handy for checking what another thread is doing. If your threads aren’t behaving the way you expected them to, explicitly record your assumptions (we suggest writing them down) and then examine them one by one. Threading is a lot like grammar: There are a lot of rules, but eventually you develop a sense for what works and what doesn’t. (Or so I’m told.)
As you read the next chapters, please don’t hesitate to refer back to this chapter when needed.
KeywordsRace Condition Main Thread Automate Teller Machine Collection Plate Member Variable
Unable to display preview. Download preview PDF.