Threading is very important topic while talking about Java programming language. We will learn thread basics, thread states and thread life cycle in this post. First we will see some basic definitions.
Process
In simple words a program in execution is a called process. Process have their own address space. Processes are referred as heavy weight activities/task, because context switching between processes is very expensive. In a process there can be multiple threads.
Thread
A thread is an independent path of execution within a program/process. In a process multiple threads can run concurrently. Thread class in java controls the thread executions in java.Every thread in Java is created and controlled by the java.lang.Thread class.
Multi-threading
Running multiple tasks concurrently in a program is called multithreading.
Thread Lifecycle & Thread States
Thread execution in Java is controlled by java.lang.Thread class. Thread lifecycle starts with creation of thread object (new state) and ends on dead state of thread. Let's understand the thread states first - A thread mainly have four states (new, runnable, blocked and dead). But we will talk about one more state that is Running state.
This is all about thread life cycle. I will create a new post on ways of thread creation separately.
I hope you will like this article. If you have any doubts please put in comments.
Check below links also-
Bubble Sort Algorithm analysis and implementation in Java
Linear Search Algorithm analysis and implementation in Java
Binary Search Algorithm analysis and implementation in Java
Java8 - Lambda Expressions in Java
Java8 - Default and static methods in Interface
How to create Immutable classes in JAVA
Process
In simple words a program in execution is a called process. Process have their own address space. Processes are referred as heavy weight activities/task, because context switching between processes is very expensive. In a process there can be multiple threads.
Thread
A thread is an independent path of execution within a program/process. In a process multiple threads can run concurrently. Thread class in java controls the thread executions in java.Every thread in Java is created and controlled by the java.lang.Thread class.
Multi-threading
Running multiple tasks concurrently in a program is called multithreading.
Thread Lifecycle & Thread States
Thread execution in Java is controlled by java.lang.Thread class. Thread lifecycle starts with creation of thread object (new state) and ends on dead state of thread. Let's understand the thread states first - A thread mainly have four states (new, runnable, blocked and dead). But we will talk about one more state that is Running state.
- New State - When a new thread object is created using new() operator, thread will be in new state. At this state thread is not alive.
- Runnable State - When start() method of Thread class is called, thread moved to runnable state. In this state thread is picked up by scheduler for execution, but when the execution will start is totally depends on scheduling algorithm of the operating system.
- Running State - Running state is the actual execution state of the thread where run() of the thread is called and statements written in run() method will be executed. From running state a thread can enter into blocked or dead state.
- Blocked State - When thread is waiting on some I/O or any other reason, it is said to be in blocked state. After blocked state thread can not go to for execution directly, it needs to wait for thread scheduler to pick up for execution.
- Dead State - This is the final state of a thread. After completing execution thread enters into dead state.
This is all about thread life cycle. I will create a new post on ways of thread creation separately.
I hope you will like this article. If you have any doubts please put in comments.
Check below links also-
Bubble Sort Algorithm analysis and implementation in Java
Linear Search Algorithm analysis and implementation in Java
Binary Search Algorithm analysis and implementation in Java
Java8 - Lambda Expressions in Java
Java8 - Default and static methods in Interface
How to create Immutable classes in JAVA
No comments:
Post a Comment