Threads in Java.

Hello Friends,

This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads.

*What are Threads?

Threads are independently running processes that are isolated from each other upto some extent, they are sometimes called as the Light weighted processes. In Other words Threading is the facility to allow multiple activities to co-exists within a single running process. As many as threads can exists in a java program, but each of them have its own Program Counter, Stack, life time and its own local variables. Thus a process that has only one thread is referred to as a single-threaded process, while a process with multiple threads is referred to as a multi-threaded process. However threads within a single process are less insulated with each other and hence they share memory, file handlers and other process states.

Most programs written today runs as a single thread, causing problems when multiple events or actions need to occur at the same time. Every java program consists of at least one thread i.e. thread Main(). If no other threads are created by the main thread, then program terminates when the main() method complete its execution. The main thread creates some other threads called child threads. Any ways if the program contains some child threads then it starts with the run() method and it ends with the same, i.e. the the child threads will end their execution only when the run() method is executed completely.In other words we can say that,the main() method execution can finish, but the program will keep running until all the threads have completed its execution.

*Advantages of threads:-

· Make the UI more responsive
· Take advantage of multiprocessor systems
· Simplify modeling
· Perform asynchronous or background processing.

*Disadvantages of Threads:-

· Threads consumes Resources, so overuse of threads in a program can affect the performance of the execution.

· When two threads access same data such as static field or shared collection then there is the possibility of loosing the data.( However this can be avoided using Synchronization)

· Deadlock is another possibility while using the threads.

*Life Cycle of Threads in Java.

Just like any other process the Threads must have to pass through various states during its lifecycle. The above figure shows the lifecycle of a thread. There are five states where a thread has to pass through during its life time. Following will give you detail idea about each of the state and its functionality.

1) New state – Just after the creation of Thread instance the thread is in this state.At this stage the start() method is not invoked and hence the thread is considered not alive (**But Not Dead). A thread can enter this state only once during its creation.

2) Runnable (Ready-to-run) state – After the thread is born/created it start its life from Runnable state. Once the Start() method is called the thread enters into the Runnable state. Here, each thread waits for their time of scheduling, that CPU provides to them according to the scheduling criteria. But a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting in the pool for a turn on the processor.

3) Running state – Once the thread is scheduled for processing it enters the Running state. A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state. A process can enter into Running state only after coming from runnable State, in other words the scheduler select a thread from runnable pool.

4) Dead state – Its the last state where a thread can be present. A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again. A thread can enter into this state from any state, either after completed execution or incomplete execution.

5) Blocked – A thread can enter in this state because of waiting for the resources that are hold by another thread. If the thread is in running state and it needs some resources then it will move the thread to blocked state (**this is not the only case when thread is blocked). Once the thread is in blocked state then it can only return to either Runnable or Dead state, but not Running state even if it came from there.

Hope this may have helped you.

Nikhil. 🙂

One thought on “Threads in Java.”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.