Chapter 16. Threads and Synchronization

The Java platform supports multitasking through threads. Each thread is a separate flow of control, executing concurrently with all other threads. If the computer has several CPUs available, separate threads may execute literally at the same time. If not, the JVM implementation can let each thread run for a while, then interrupt it to let a different thread run.

The features that make up the Java platform thread model are divided between the JVM itself and the class java.lang.Thread. This class is found on all Java platforms. It contains methods for spawning new threads, controlling thread priority, and stopping and starting thread execution.

The JVM's support for threads lies in its ability to provide synchronization. Synchronization allows two threads to communicate with each other so that resources can be shared safely between threads. Think of synchronization as a traffic light. There is a common resource (the intersection) and two simultaneous threads (lanes of cars) that want to use the intersection. It is critical that only one thread proceed. If both were allowed to proceed, disaster would ensue. If neither one were allowed to go, nobody would get anywhere.

To maximize traffic flow, many traffic lights use sensors to detect when a car wants to use the intersection. Otherwise, on lightly traveled roads, drivers would find themselves staring at a red light with nobody in sight. However, it is not enough to have the traffic light change to green immediately for anybody who trips the sensor. If two cars arrive at exactly the same time, which one gets the green light? In the traffic light, a computer decides which one came first and gives that car the first green light. If they appear to have come at the same time, it doesn't matter which one goes first, but it is crucial that only one goes first.

Synchronization works best when there is a single controlling authority in charge of access. At many intersections, it is unwise to let drivers control access to the intersection by cooperation between them. If two drivers arrive at the same time, there is often much waving of hands and honking of horns while each driver tries to signal to the other to go first. And when a driver does decide to go, the driver must move carefully, to ensure that the other driver hasn't made the decision to go simultaneously. Instead, the drivers agree to let the traffic light control the intersection.

In the JVM, different threads agree to let the JVM itself synchronize between threads. Threads request locks, and the JVM provides the lock to exactly one thread at a time. Requesting a lock is like tripping the sensor for the traffic light: it is a request to proceed, but the JVM ensures that one thread doesn't proceed while another possesses the lock. In the JVM, locks are called monitors.

As in the traffic light, only code that chooses to observe the synchronization restrictions actually pays any attention to the locks. It is up to the programmer to use the synchronization instructions and methods in all code that accesses a shared resource.

If the computer you are using has only a single processor, then it is the job of the JVM to switch back and forth between threads, which means that monitor requests can never occur quite simultaneously. However, if the computer has multiple processors, then two lock requests may occur literally at the same time. It is the responsibility of the JVM implementor to ensure that only one thread obtains control of a monitor at a time.



Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

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