Login    New User    Forgot Password    Help      

Multithreading

Java Tutorials

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.

Example

// Multiple Threading
class newtread implements Runnable
{
Thread t;
        // Creates a second thread
        newtread()
        {
        // 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()
        {
        try{
           for(int i=5;i>0;i--)
                {
                   System.out.print("Child Thread "+i+"  ");
                   Thread.sleep(100);
                   // use all thrre values and run again
                   //Thread.sleep(1000);
                   //Thread.sleep(2000);
                }
           }
       catch(InterruptedException e)
           {
           System.out.println("Child interruptedd ");
           }
        System.out.print("Exiting child thread  ");
        }
}
class tread3
{
public static void main(String args[])
{
// start multiple thread
new newtread();
new newtread();
new newtread();

Thread t= Thread.currentThread();
System.out.println();
//System.out.print("Current Thread "+t);

// renaming the thread
t.setName("Main Thread ");

int i;
try{
        for(i=1;i<=10;i++)
        {
        System.out.print(t);
        System.out.print("  ");
        Thread.sleep(1000);// sleep in milliseconds
        System.out.println(i);
        }
   }
   catch(InterruptedException e)
   {
    System.out.println("***");
   }
} }

 

Output

Java Tutorials