Java Thread Model
The Java run-time system depends on the threads for many things, and all the class libraries are designed with multithreading in mind. In fact, Java uses the threads to enable the entire environment to be asynchronous. This helps reduce inefficiency by preventing the waste of CPU cycles.
The value of a multithreaded environment is best understood in contrast to its counterpart.
Single-threaded systems use an approach called an event loop with polling. In this model, a single thread of control runs in an infinite loop, polling a single event queue to decide what to do next. Once this polling mechanism returns with, say, a single that network file is ready to be read, then the event loop dispatches the control to the appropriate event handler. Until this event handler returns, nothing else can happen in the program. This wastes the CPU time. It can also result in one part of a program dominating the system and preventing any other events from being processed. In general, in a single-threaded environment, when a thread blocks (i.e., suspends execution) because it is waiting for some resource, the entire program stops running.
The benefit of Java's multithreading is that the main loop/polling mechanism is eliminated. One thread can pause without stopping the other parts of your program. For example, the idle time created when a thread reads the data from a network or waits for the user input can be utilized elsewhere. Multithreading allows animation loops to sleep for a second between each frame without causing the whole system to pause. When a thread blocks in a Java program, only the single thread that is blocked pauses. All the other threads continue to run.
As most readers know, over past few years, multi-core systems have become commonplace. Of course, single-core systems are still in widespread use. It is important to understand that the Java's multithreading features work in both types of systems. In a single-core system, concurrently executing threads share the CPU, with each thread receiving a slice of CPU time. Therefore, in a single-core system, two or more threads do not actually run at the same time, but idle CPU time utilized. However, in a multi-core systems, it is possible for two or more threads to actually execute simultaneously. In many cases, this can further improve the program efficiency and increase the speed for certain operations.
A thread can be running. It can be ready to run as soon as it gets CPU time. A running thread can be suspended, which temporarily halts its activity. A suspended thread can then be resumed, allowing it to pick up where it left off. A thread can be blocked when waiting for a resource. At any time, a thread can be terminated, which halts its execution immediately. Once terminated, a thread cannot be resumed.
Java Messaging System
After you divide your program into separate threads, you need to define how they will communicate with one another.
When programming with some other languages, you must depend on the operating system to establish communication between the threads. This, of course, adds overhead. By contrast, Java provides a clean, low-cost way for two or more threads to talk to one another, via calls to predefined methods that all the objects have. Java's messaging system allows a thread to enter a synchronized method on an object, and then wait there until some other thread explicitly notifies it to come out.
Java Thread Class and Runnable Interface
Java's multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable. Thread encapsulates a thread of execution. Since you can't directly refer to the ethereal state of a running thread, you will deal with it through its proxy, the Thread instance that spawned it. To create a new thread, your program will either extend Thread or implement the Runnable interface.
The Thread class defines several methods that help to manage the threads. Some important methods are shown in the following table :
|getName()||Obtain a thread's name|
|getPriority()||Obtain a thread's priority|
|isAlive()||Determine if a thread is still running|
|join()||Wait for a thread to terminate|
|run()||Entry point for the thread|
|sleep()||Suspend a thread for a period of time|
|start()||Start a thread by calling its run method|
« Previous Tutorial Next Tutorial »