What is Thread in Java
In this Java Thread Tutorials, we are going to learn about threading Java. Threads are the basic building block of Java programming. Every program has at least one thread called main thread. Basically, every process runs on either single or multiple threads. A single thread is nothing but a light weight and smallest unit of processing.
To work with threading, Java provides a class called Thread class. Every thread has a life cycle :
Thread Life Cycle:
Every thread goes through these above life cycle processes.
New: In this state, a thread is created using Thread class, and it will remain in new state until the program starts the thread.
Runnable: Once a thread is ready to run, it moved into runnable state. Depends on the scheduler, it is scheduled to run at any time.
Running: When Thread gets CPU, it moves to running state and back to running when it deallocates CPU.
Waiting/Blocked: When a thread is temporally in waiting state, it will be moved it either waiting/blocked state.
Terminated/Dead: A thread is in terminated state when, it exists on the completion of the code execution given to it and in case of error scenario.
Thread Example:
Let’s create a thread in Java.
Example of thread
package com.slb.thread;
public class ThreadDemo {
public static void main(String[] args) {
Thread thread = new Thread();
System.out.println(thread.getName());
System.out.println(thread.getState());
System.out.println(Thread.currentThread().getState() + "--" + Thread.currentThread().getName() );
}
}
Output:
Thread-0
NEW
RUNNABLE--main
Process finished with exit code 0
In the above example, we have created a thread called “thread” using new Thread() class. Then we are printing the name of the thread and the state of the thread. As shown in the output of the execution, we can see that Thread-0 is the name of the newly created thread and New is the current state of that thread.
In Java, main() is also a thread. It’s a daemon thread which runs in the background. In the above example, we are getting the current thread using Thread.currentThread() method, and we are printing its state and name. As you can see in the output, it’s RUNNABLE and main is the name of the thread.
Java Thread Class Methods
Will see some of the methods in the thread class:
1.run(): This method is the entry point which is used to start execution of the thread.
2.start(): Starts a new thread by calling run() of Thread/runnable object
3.sleep(): This method suspends the thread for a specified time specified. Syntax sleep(int duration). Duration is in milliseconds.
4.yeild(): This method is used to pause the execution temporarily and let other threads to execute.
5.join(): This method is used to permits one thread to wait until the other thread to finish its execution.
6.isAlive(): This method will check if thread is alive or dead.
Java Thread Example
Example:
package com.slb.thread;
public class ThreadMethodDemo extends Thread {
@Override
public void run() {
System.out.println("Running Thread...." + Thread.currentThread().getName());
}
public static void main(String[] args) {
ThreadMethodDemo threadMethodDemo_ONE = new ThreadMethodDemo();
ThreadMethodDemo threadMethodDemo_TWO = new ThreadMethodDemo();
threadMethodDemo_ONE.setName("SLB-Thread-1");
threadMethodDemo_TWO.setName("SLB-Thread-2");
threadMethodDemo_ONE.start();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main thread is running here....");
threadMethodDemo_TWO.start();
}
}
In the above example, we have created two thread objects, named threadMethodDemo_ONE and threadMethodDemo_TWO, and we are setting the name of these two thread objects as SLB-Thread-1 and SLB-Thread-2. We are calling the start() method on first thread as threadMethodDemo_ONE.start(); it will call the run() method and execute the block of statements present inside run().
Then we are calling sleep() method on the current thread (main thread which is running) with 10000 milliseconds i.e. 10 seconds, so the program execution will wait for 10 seconds before executing the next lines of instruction. Note that sleep method throws interrupted exception, so we need to throw it or handle it properly.
Next Topic: Creating Threads | Ways to create threads in Java | how to create thread in Java
Also Explore Multithreading in Java.