Multithreading is a technique that allows a program or a process to execute many tasks concurrently (at the same time and parallel). It allows a process to run its tasks in parallel mode on a single processor system
In the multithreading concept, several multiple lightweight processes are run in a single process/task or program by a single processor. For Example, When you use a word processor you perform a many different tasks such as printing, spell checking and so on. Multithreaded software treats each process as a separate program.
In Java, the Java Virtual Machine (JVM) allows an application to have multiple threads of execution running concurrently. It allows a program to be more responsible to the user. When a program contains multiple threads then the CPU can switch between the two threads to execute them at the same time.
For example, look at the diagram shown as:
In this diagram, two threads are being executed having more than one task. The task of each thread is switched to the task of another thread.
Advantages of multithreading over multitasking :
- Reduces the computation time.
- Improves performance of an application.
- Threads share the same address space so it saves the memory.
- Context switching between threads is usually less expensive than between processes.
- Cost of communication between threads is relatively low.
Different states implementing Multiple-Threads are:
As we have seen different states that may be occur with the single thread. A running thread can enter to any non-runnable state, depending on the circumstances. A thread cannot enters directly to the running state from non-runnable state, firstly it goes to runnable state. Now lets understand the some non-runnable states which may be occur handling the multithreads.
- Sleeping ? On this state, the thread is still alive but it is not runnable, it might be return to runnable state later, if a particular event occurs. On this state a thread sleeps for a specified amount of time. You can use the method sleep( ) to stop the running state of a thread.
static void sleep(long millisecond) throws InterruptedException
- Waiting for Notification ? A thread waits for notification from another thread. The thread sends back to runnable state after sending notification from another thread.
final void wait(long timeout) throws InterruptedException
final void wait(long timeout, int nanos) throws InterruptedException
final void wait() throws InterruptedException
- Blocked on I/O ? The thread waits for completion of blocking operation. A thread can enter on this state because of waiting I/O resource. In that case the thread sends back to runnable state after availability of resources.
- Blocked for joint completion ? The thread can come on this state because of waiting the completion of another thread.
- Blocked for lock acquisition ? The thread can come on this state because of waiting to acquire the lock of an object.
// Multiple Threading
class newtread implements Runnable
// Creates a second thread
// Creates other thread
t= new Thread(this , "New tread");
System.out.println("Child Thread "+t);
t.start();// Start the thread
// Entry point for the second thread
public void run()
System.out.print("Child Thread "+i+" ");
// use all thrre values and run again
System.out.println("Child interruptedd ");
System.out.print("Exiting child thread ");
public static void main(String args)
// start multiple thread
Thread t= Thread.currentThread();
//System.out.print("Current Thread "+t);
// renaming the thread
t.setName("Main Thread ");
Thread.sleep(1000);// sleep in milliseconds