NavBar

Monday 23 April 2018

what is Multithreading in java with example

   Multithreading:-

  • Java provides built in support for multithreaded programming concept .
  • Process of executing multiple threads simultaneously.
  • Multiprocessing and multithreading,both are used to achieve multitasking.
  • A multithreaded program contains two or more parts that can run concurrently.
  • Multithreading is mostly used in games, animation etc.
  • Thread is basically  lightweight sub process.
  • Cost of communication between the thread is low.

Life Cycle of Thread (Thread states)

what is Multithreading in java with example

New:-When a new thread object is created the thread is said to be new state. 

2.Runnable:- When start()method is invoke on thread it is said to be in runnable state.It is only                                      ready for run.

3.Running:- In this state the thread is actually executting a code.

4.Blocked:- A thread can move to block or wait state at any time.

5.Dead:- The thread has finished his work and moves to dead state.


                  
Multithreding programs are run above 5 states.

Creating a thread:-


Thread can be created using two ways.
  1. By defining class that extends the thread class.
  2. By implementing the runnable interface.
Note:-(In both cases run method should be implemented)



1.Extending the thread class:-

For example:-
                        public class MyThread extends Thread
                        {
                                  public void run()
                                  {
                                        System.out.println("This is Thread 1");
                                   }
                         }

                      MyThread t1=new MyThread();                 //Creating object of thread
                      t1.start();                                                     //Start thread

     2.Implementing runnable interface:-


                          public class MyThread extends Thread
                        {
                                  public void run()
                                  {
                                        System.out.println("This is Thread 2");
                                   }
                         }
                             MyThread t1=new MyThread();          //Creating object of thread
                             Thread t=new Thread(t1);
                              t1.start();                                             //Start thread


// Demo program for Runnable

public class Abc implements Runnable {
    public void run()
    {
        for(int i=0;i<=10;i++)
            System.out.println("i="+i);
    }
}

public class Xyz implements Runnable {
    public void run()
    {
        for(int j=0;j<=5;j++)
            System.out.println("j="+j);
    }

public class Multithreading  {
  public static void main(String[] args) {
       Abc obj1=new Abc();
       Xyz obj2=new Xyz();
       Thread T1=new Thread(obj1);
       Thread T2=new Thread(obj2);
       obj1.start();
       obj2.start();
    }
    
}

isAlive() and join() method:- 


  • isAlive():- This method is used to test wheather a thread is still alive.It returns true if the thread is running,otherwise it runs false.
  • join():- This method is used to halt the execution of current thread untile the thread on which the method is called finishes its execution.

//Demo program for isAlive() and join() methods

package multidemo;
public class Abc extends Thread {
    public void run()
    {
        for (int i=0;i<-10;i++)
        {
            if(i==5)
            {
                stop();
            }
            System.out.println("i="+i);
        }
    }
}


package multidemo;
public class Xyz extends Thread{
    public void run()
    {
        for(int j=0;j<=5;j++)
        {
            if(j==3)
            {
                try
                {
                    sleep(2000);
                }
                catch(Exception e)
                {
                    System.out.println(e);
                }
            }
            System.out.println("j="+j);
        }
    }
}


public class MultiDemo {
 public static void main(String[] args) {
        Abc obj1=new Abc();
        obj1.start();
        System.out.println("is thread is alive "+obj1.isAlive());
        Xyz obj2=new Xyz();
        try{
            System.out.println("check joining");
            obj1.join();
            obj2.join();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
       obj2.start();
    }
    
}  

Output:-
                       
isAlive() and join() demo java multithreading

No comments:

Post a Comment