by, 11-13-2011 at 11:51 AM (951 Views)
A thread is a thread of execution in a program. Java environment has been built around the multithreading model. In fact all Java class libraries have been designed keeping multithreading in mind. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. 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.