Skill-Building Exercises


Summary

A thread is a sequence of executable lines of code. Java programs can create and run more than one thread concurrently. Threads are organized into a tree structure composed of threads and thread groups. All Java programs are multi-threaded because the JVM and the Swing/AWT framework are themselves multi-threaded.

A thread can be created by extending Thread and overriding its run() method, or by passing a Runnable into one of the standard Thread constructors. A thread must be started by calling its start() method. Calling Thread.sleep() causes a thread to suspend execution and frees the CPU for other threads. A sleeping thread can be woken by calling interrupt().

The JVM employs a preemptive algorithm for scheduling threads waiting to be run. Setting a thread’s priority can affect how and when the JVM schedules it, which, in the face of other threads competing for CPU time, can affect the thread’s performance. A thread should call yield() or sleep() or otherwise make provisions for sharing the CPU with competing threads. A thread that does not make provisions for sharing the CPU with competing threads (by calling yield(), sleep() or some other mechanism) is called a selfish thread. There is no guarantee that the underlying system will prevent selfish threads from taking complete control of the CPU. Because of variations in how systems handle the scheduling of threads, the correctness of a program’s behavior should not rely on the particulars of any system.

A race condition is a condition where more than one thread accesses the same resource at the same time, resulting in errors and/or resource corruption. Synchronization can be used to prevent race conditions by ensuring that a block of code is executed by only one thread at a time. Synchronization is only effective when all threads attempting to execute a block of code are synchronized on a common object. Synchronization is applied to a block of code. Synchronized methods have a slightly different syntax but they too are simply blocks of code that have been synchronized.

In a producer-consumer relationship, a producer thread produces something that a consumer thread consumes and neither thread makes an effort to coordinate with the other. Coordination is handled instead by the object they both access. The wait() and notify() methods of Object can be used to negotiate the coordination between inter-dependent threads.

Deadlock is the situation where two or more threads remain permanently blocked because they are waiting for locks each other already holds. Deadlock should be avoided and cannot be easily detected. Unfortunately, the only method for avoiding it is to understand the threads you write and their potential interactions and to program in such a way that deadlock is never a possibility.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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