Home »
Articles
10 things you should know about thread life cycle as a Java Programmer
In this article, we will know about the 10 things you should know about thread life cycle as a Java Programmer.
Submitted by IncludeHelp, on JUL 02, 2022
Introduction
In Java Programming, threads are the topics that confuse beginners but are worth the time is given as it is important and provides strength to the Java language.
When a program is executed it is executed in steps and the flow of control within a program in a sequence is called a thread.
Threads are independent and are not affected by the exception in any of the strings in the shared memory area.
There are multiple threads getting executed simultaneously and executing a very different task in every single program.
The execution of multiple threads at the same time makes it work more efficiently. It makes a process concurrent.
It can also execute a time and space-consuming complex task in the background without any interruption in the main program.
There are two types of threads in Java Programming which are user threads and daemon threads.
User threads are the threads with high priority and the Java Virtual Machine before terminating it, waits for a user to complete the task.
Daemon threads are the threads having low priority having the task to provide services to user threads. In this, we will Learn the thread life cycle in depth.
Things you should know about thread life cycle as a Java Programmer
1) Multithreading in Java Programming
The process of execution of multiple threads at the same time is called Multithreading in Java Programming. Multithreading is preferred over multiprocessing as in threads a shared memory area is used rather than allocating a separate memory area to save memory and it makes context switching between threads faster.
2) Creation of threads in Java
There are two ways to create a thread first by extending the thread class and another by implementing of Runnable interface.
3) Life Cycle of a Thread in Java Programming
There are multiple states in the life cycle of a thread and a thread is always in any one of the states.
- New: At the time of the creation of a new thread, it is always in the new state. If a thread is in a new state that means the code has not been run yet and it is the initial state of every thread. If a thread is in this state that means its execution is not started yet.
-
Active: When the start() method is called for a thread, it moves to the active state from the new state. It has two states within it: the first one is runnable and another one is running.
- Runnable State: A thread is when ready to run, it moves to a runnable state. A thread may be running or ready to run at any moment in this state.
A Thread scheduler provides time for a thread to run. A fixed portion of time is allotted to a thread and that thread gets executed in that time only when the time gets over thread automatically gives up the execution on the CPU and the other thread gets executed for the allocated time period.
In such a way, all the threads wait in a queue to get executed on the CPU.
- Running State: When the thread gets time in the CPU. It gets running and moves to a running state. In most of the cases, the state changes from runnable to running and then again to runnable when the execution gets complete and it waits for the next execution.
- Blocked/Waiting: A thread that is either inactive or blocked for some time but not permanently, then the thread either moves to the blocked state or the waiting state. For example when there are two threads to print some data through a printer. If thread A is using a printer to print some data so thread B can not use the printer at that time and have to wait till the time thread A completes the printing of data. In such a case thread B goes to the blocked state. In the blocked state a thread can not get executed.
- Time Waiting: when a thread is in the critical state another thread waits for its completion and it has to wait forever to avoid this we call the sleep() method for a particular thread. It puts the thread into the timed wait state and after the time gets over, the thread wakes up again and starts its execution from the point where it left before.
- Terminated: When a thread has completed its job it gets terminated after it and is normal termination. When there are some unhandled exceptional cases or segmentation errors in code at that time also a thread can be terminated. If a thread enters a termination state it means that the thread is deleted and can not be brought back and it does not exist on the computer.
4) Daemon Thread
In Java programming, a Daemon Thread is a service provider thread used to provide service to the user thread for supporting tasks in the background. When the user threads die, Java virtual Machine terminates all the daemon threads automatically. It is a thread with low priority.
5) Thread Scheduler
In Java Programming the component of Java that decides which thread should be executed first and which one should wait to get executed is a thread schedular.
It is up to the thread scheduler to move a thread into the running and runnable state and this is based upon two factors which are: priority and time of arrival. The one with higher priority is more probable to be taken up by the thread schedular.
When two threads are having same priority then the thread scheduler's algorithm decides the priority.
6) Thread sleep() method
The purpose of the sleep() method is to stop the execution of a thread temporarily for a given span of time. The time spent by a thread in a sleeping state is called sleeping time. After the sleeping time, the thread starts getting executed from where it stopped.
7) Join() method in Java
This method is defined in java.lang.Thread class and this allows a thread to wait until the other one completes the execution.
When the join() method is called, the thread that is currently executing stops and goes to the wait state and remains there till the time the thread with the join() method is executed and goes to the dead state.
8) Priority of a Thread in Java programming
All the threads have priorities and priority is represented by a number between 1 to 10. The thread with higher priority will get executed before the one with low priority. When two threads are having same priority then the thread scheduler's algorithm decides the priority.
9) Thread Pool in Java
It represents a group of worker threads that have been already used many times and still waiting for the job. When the service provider gets a job it just takes a thread from the thread pool and assigns it a job and after completion of the job, it places the thread back into the thread pool. There is always a risk of Thread leakage while using a thread pool in java programming.
10) Shutdown Hook in Java Programming
A construct that helps the developer to write some code that has to be run when the Java Virtual Machine is shutting down is called the Java shutdown hook.
It is very useful when we need to do special cleanup work of resources while the Java virtual machine is shutting down. The JVM shuts down when the command prompt is closed by any method be it by pressing CTRL + C or by user log off or by system shut down.
Conclusion
Threads are an important topic in Java programming and also vital to learning the thread life cycle in depth. In this, we discussed various things we should know about the Thread life cycle. It makes the processing faster and more efficient. By knowing these things about the thread life cycle you will get a good insight into the thread life cycle.