Java is a multithreaded programming language. Java multithreading refers to having multiple threads that can run concurrently (at the same time). It is also know as Concurrency in Java. Each thread is a single executable piece of code that runs parallelly to other threads.
Life cycle of a thread
A life cycle of a thread has following stages:
- New: When a thread is created it is new. It is also known as born thread. A thread stays in new state until it is not started by a program.
- Runnable: When a program starts a new born thread, it goes into runnable state. The thread is executing its task when in runnable state.
- Running: When thread starts execution, then it goes into the running state.
- Waiting: When a thread has to wait then it is said to be in waiting state. As multiple threads are executing at a time so a threads waits for the other threads to complete execution. This maintains a synchronization among threads.
- Dead: After a thread has competed its execution, it is terminated and it goes into the Dead state.
Create a thread
There are two steps to create a thread. Each step is explained below:
In the first step, user can use the Runnable interface to create a thread. This interface provide run() method that is an entry point for thread. The syntax is:
The second step of creating a thread is creating an object of Thread class, using the Thread() constructor. The Thread() constructor takes two arguments. The first argument is the instance of a class from the Runnable interface. The second argument takes String input as the name of thread. The syntax is:
Start a thread
After user creates a thread, the thread can be started by using the start() method. The start() method invokes a call to run() method. The syntax of calling start() method is:
Here is an example that demonstrates the concept of Java multithreading:
class Multithreading implements Runnable { private Thread th; private String threadName; Multithreading( String name) { threadName = name; System.out.println(threadName + " Creating"); } public void run() { System.out.println(threadName + " Running"); try { for(int i = 2; i > 0; i--) { System.out.println("Thread: " + threadName + ", " + i); // Letting the thread sleep for some time. Thread.sleep(50); } }catch (InterruptedException e) { System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } public void start () { System.out.println(threadName + " Starting" ); if (th == null) { th = new Thread (this, threadName); th.start (); } } } public class Example { public static void main(String args[]) { Multithreading m1 = new Multithreading( "Thread-1"); m1.start(); Multithreading m2 = new Multithreading( "Thread-2"); m2.start(); } }
In the above code, the class Multithreading that implements from Runnable. The output of above code after compilation is:
Java Thread Methods
Here is a list of built in methods and their descriptions, provided by the Thread class:
Sr.No. | Method | Description |
1. | void start() | This method starts the execution of thread. It also invokes the run() method. |
2. | void sleep() | It makes a thread sleep for specified time. |
3. | void run() | It performs the actions assigned to a thread. |
4. | String getName() | It returns the name of the thread. |
5. | static Thread currentThread() | It returns reference object of the thread that is being executed at the moment. |
6. | void join() | This method waits for a thread to terminate or die. |
7. | void setName() | This method is used to change name of the specified thread. |
8. | boolean isAlive() | It checks whether the thread is alive or not. Returns true if is alive and returns false if it is dead. |
9. | void setPriority() | This method changes the priority of specified thread. |
10. | static void yield() | This method makes the currently executing thread to pause and lets other threads to execute. |
Advantages of Java Multithreading
- The advantage of using multithreading in Java is that it saves space. Each thread does not occupy individual memory space.
- Multithreading is also more efficient as the context switching between threads happens at a faster rate.
- Multithreading provides multitasking so user can perform multiple operations at one time.
- It provides cost efficiency by reducing the time and memory allocation.