Publish your story - poem - jokes free online--click here

JAVA

Mutli Threading in Java

Multi Thread

                             Java supports multi threading mean java can run more than one independent process concurrently and each process is termed as thread.
In general a thread can be an object or method.
Life cycle of a thread:--

There are four different stage of any thread
  1.     Running -----àthis is the stage while thread is currently running.
  2.     Ready -----à   this is stage just before the running stage.
  3.    Waiting ---à in this stage thread waits for their next chance to get executed.
  4.     Dead -----à stage after the successful or unsuccessful termination of thread.


Making our own thread

All we have to do with our class which is going to be a thread is that extends the class “Thread” class or implements the “Runnable” interface. And override the run method. And call start method to run the run().
Multi thread using “Thread” class (Run the below program and see output)

import java.util.*;
class cls1 extends Thread // extending thread
{
          public void run() //overriding run method of Thread class
          {
                   for(int i=1;i<=20;i++)
                   {
                             System.out.println(i+"  :this is first class");
                   }
          }
}
class cls2 extends Thread
{
          public void run() //overriding run method of Thread class
          {
                   for(int i=1;i<=20;i++)
                   {
                             System.out.println(i+"  :this is second class");
                   }
          }
}
public class abc
{
          public static void main(String as[])
          {
                   cls1 c1=new cls1();
                   cls2 c2=new cls2();
                   c1.start(); //calling run() using start()
                   c2.start();//calling run() using start()
          }
}

Note: ----- in above program you must notice both thread is running concurrently.

Note: -----since thread doesn’t have any pre defined execution pattern so each time you excute the programme the output may vary.

Multithreading using “Runnable” interface

Use of Runnable require a thread object to call the start() {see the code below}

import java.util.*;
class cls1 implements Runnable // extending thread
{
          public void run() //overriding run method of Thread class
          {
                   for(int i=1;i<=20;i++)
                   {
                             System.out.println(i+"  :this is first class");
                   }
          }
}
class cls2 implements Runnable
{
          public void run() //overriding run method of Thread class
          {
                   for(int i=1;i<=20;i++)
                   {
                             System.out.println(i+"  :this is second class");
                   }
          }
}
public class abc
{
          public static void main(String as[])
          {
                   cls1 c1=new cls1();
                   cls2 c2=new cls2();
                   Thread t1=new Thread(c1);
                   Thread t2=new Thread(c2);
                   t1.start();
                   t2.start();
          }
}

Giving priority to the thread

Execution of thread is totally depended on the java run time but we can set the priority  explicitly  to the thread.

Priority level                                    Range
MAX_ PRIORITY                              6-10
NORMAL_PRIORITY                       5
MIN_PRIORITY                                4-1

class thread1 extends Thread
{
          public void run()
          {
                   for(int i=1;i<=5;i++)
                   {
                             for(int j=1;j<=i;j++)
                             {
                                      System.out.print("*");
                             }
                             System.out.println();
                   }
          }
}
class thread2 extends Thread
{
          public void run()
          {
                   for(int i=1;i<=5;i++)
                   {
                             for(int j=1;j<=i;j++)
                             {
                                      System.out.print("@");
                             }
                             System.out.println();
                   }
          }
}
class thread3 extends Thread
{
          public void run()
          {
                   for(int i=1;i<=5;i++)
                   {
                             for(int j=1;j<=i;j++)
                             {
                                      System.out.print("&");
                             }
                             System.out.println();
                   }
          }
}
class abc
{
          public static void main(String as[])
          {
                   thread1 t1 = new thread1();
                   thread2 t2 = new thread2();
                   thread3 t3 = new thread3();
                  
                   t1.setPriority(Thread.MAX_PRIORITY);
                   t2.setPriority(Thread.MIN_PRIORITY);
                   t3.setPriority(Thread.NORM_PRIORITY);
                  
                   t1.start();
                   t2.start();
                   t3.start();
          }
}



No comments:

Post a Comment