Home > DeveloperSection > Articles > Multithreading in Java

Multithreading in Java


Java Java 
Ratings:
0 Comment(s)
 662  View(s)
Rate this:

Multithreading in Java

Concurrency is the ability to run several programs or several parts of a program in parallel. If a time consuming task can be performed asynchronously or in parallel, this improve the throughput and the interactivity of the program.

Java multithreading allows you to do multiple tasks at the same time. This is possible because modern day computers have multiple CPUs (CPUs are the brain of your computer, and it has a bunch!). One CPU can work on one Thread at a time (unless your CPUs have hyper-threading, in which case it can handle two at a time).

What is Thread-: A thread is light weighted sub process, a smallest unit of processing. Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.

Life Cycle of Thread

  

New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

 

Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

 

Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

 

Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

 

Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.

 

Advantages of Multithreading -:

1.      It does not block the user because thread are independent and you can perform multiple operations at same time.

2.      It saved time because we can perform multiple operation at a same time.

Here I am creating example of Multithread

 

class RunnableDemo implements Runnable {

     private Thread t;

     private String threadName;

 

     RunnableDemo(String name) {

           threadName = name;

           System.out.println("Creating " + threadName);

     }

 

     public void run() {

           System.out.println("Running " + threadName);

           try {

                for (int i = 4; i > 0; i--) {

           System.out.println("Thread: " + threadName + ", " + i);

                     // Let the thread sleep for a while.

                     Thread.sleep(50);

                }

          } catch (InterruptedException e) {

        System.out.println("Thread " + threadName + " interrupted.");

           }

           System.out.println("Thread " + threadName + " exiting.");

     }

 

     public void start() {

           System.out.println("Starting " + threadName);

           if (t == null) {

                t = new Thread(this, threadName);

                t.start();

           }

     }

 

}

 

public class TestThread {

     public static void main(String args[]) {

 

           RunnableDemo R1 = new RunnableDemo("Thread-1");

           R1.start();

 

           RunnableDemo R2 = new RunnableDemo("Thread-2");

           R2.start();

     }

}

 


Don't want to miss updates? Please click the below button!

Follow MindStick