Java Thread Model
Java environment has been built around the multithreading model. In fact all Java class libraries have been designed keeping multithreading in mind. If a thread goes off to sleep for some time, the rest of the program does not get affected by this. Similarly, an animation loop can be fired that will not stop the working of rest of the system.
At a point of time a thread can be in any one of the following states – new, ready, running, inactive and finished. A thread enters the new state as soon as it is created. When it is started (by invoking start() method), it is ready to run. The start() method in turn calls the run() method which makes the thread enter the running state. While running, a thread might get blocked because some resource that it requires is not available, or it could be suspended on purpose for some reason (like put off to sleep by the programmer). In such a case the thread enters the state of being inactive. A thread can also be stopped purposely because its time has expired, then it enters the state of ready to run once again.
A thread that is in running state can be stopped once its job has finished. A thread that is ready to run can also be stopped. A thread that is stopped enters the finished state. A thread that is in inactive state can either be resumed, in which case it enters the ready state again, or it can be stopped in which case it enters the finished state.
In multithreading environment, one thread might require the attention of the CPU more quickly than other. In such a case that thread is said to be of high priority. Priority of a thread determines the switching from one thread to another. In other words, priority determines how a thread should behave with respect to the other threads.
The word priority should not be confused with the faster running of a thread. A high priority thread does not run any faster than the low priority thread. A thread can voluntarily leave the control by explicitly stopping, sleeping or blocking on pending I/O or it can pre-empted by the system to do so. In the first case the processor examines all threads and assigns the control to the thread having the highest priority. In the second case, a low priority thread that is not ready to leave the control is simply pre-empted by the higher priority thread no matter what it is doing. This is known as pre-emptive multi-tasking. It is advisable that in case two threads have the same priority, they must explicitly surrender the control to their peers.
Multithreading produces asynchronous behavior among the programs. This means that all threads run as independent units without affecting each other. But sometimes it becomes necessary to synchronize these threads. For example, synchronization must be provided when two threads share the same variable or data structure like an array. In such a case there must be way by which they should not come in each other’s way.
Since there are more than one thread in a multithreaded environment, inter-process communication becomes imperative. A thread should be able to communicate with the other threads. Threads can talk to each other by using method such as wait().
|All times are GMT +5.5. The time now is 04:05.|