Thursday, 9 February 2017

THREADS EXECUTION PROCEDURE IN JAVA

JVM executes threads based on their priority and scheduling.

Thread Scheduler – Schedulers in JVM implementation usually applies one of the two following strategies –

1. Preemptive Scheduling - If a thread with a higher priority than the current running thread moves to the ready-to-run state. The current running thread can be preempted to let the higher priority thread execute.

2. Time-Sliced or Round-Robin Scheduling – A running thread is allowed to execute for a fixed length of time, after which it moves to the ready-to-run state to wait for its turn to run again.


Thread Priority – Every thread created in JVM is assigned with a priority. The priority range is between 1 and 10. ! is called minimum priority. 5 is called normal priority. 10 is called maximum priority. In thread class, the following three variables are defined to represent above three values.
§  public static final int MIN_PRIORITY
§  public static final int NORM_PRIORITY
§  public static final int MAX_PRIORITY
Thread inherits the priority from its parent thread. The default priority of every thread is normal priority, because main thread priority is 5. The priority of a thread can be set using the setPriority() method and read using the getPriority() method, both of which are defined in the thread class with the below prototype –
§  public final void setPriority(int newpriority)
§  public final int getPriority
Example –
class MyThread1 extends Thread{
          MyThread1(String s)
          {
                   super(s);
                   start();
          }
          public void run()
          {
                   for(int i=0;i<3;i++)
                   {
                             Thread curr = Thread.currentThread();
                             curr.setPriority(Thread.MIN_PRIORITY);
                             System.out.println("Thread Name : "+Thread.currentThread().getName());
                             System.out.println("Thread priority : "+curr);
                   }
          }       
}
class MyThread2 extends Thread{
          public MyThread2(String s) {
                   super(s);
                   start();
          }
          public void run()
          {
                   for(int i=0;i<3;i++)
                   {
                   Thread curr = Thread.currentThread();
                   curr.setPriority(Thread.MAX_PRIORITY);
                   System.out.println("Thread Name : "+Thread.currentThread().getName());
                   System.out.println("Thread priority : "+curr);
                   }
          }
}
public class ThreadPriority {
          public static void main(String[] args) {
                   MyThread2 m2 = new MyThread2("My Thread m2");
                   MyThread1 m1 = new MyThread1("My Thread 1");
          }
}

Output –
Thread Name : My Thread m2
Thread priority : Thread[My Thread m2,10,main]
Thread Name : My Thread m2
Thread priority : Thread[My Thread m2,10,main]
Thread Name : My Thread m2
Thread priority : Thread[My Thread m2,10,main]
Thread Name : My Thread 1
Thread priority : Thread[My Thread 1,1,main]
Thread Name : My Thread 1
Thread priority : Thread[My Thread 1,1,main]
Thread Name : My Thread 1
Thread priority : Thread[My Thread 1,1,main]


Thread Name – User defined thread is created with the default name “Thread”+<index>, where index is the integer number which starts with 0. The first user defined thread name will be Thread-0, second thread name is Thread-1 and so on. The name of a thread can be set using the setName() method and read using the getName() method, both of which are defined in the thread class with the below prototype –
§  public final void setName(String name)
§  public final String getName()
Example –
class MyThread extends Thread{
          public MyThread()
          {
                   super();
          }
          public MyThread(String name)
          {
                   super(name);
          }
          public void run(){
                   for(int i=0; i<3; i++)
                   {
                             try{
                                      Thread.sleep(500);
                             }
                             catch(Exception e){}
                             System.out.println(getName()+"i:"+i);
                   }
          }
}
public class ThreadNameandPriority {
          public static void main(String[] args) {
                   MyThread mt1 = new MyThread();
                   System.out.println("mt1 thread's initial name and priority");
                   System.out.println(mt1.getName());
                   System.out.println(mt1.getPriority());
                   System.out.println();
                   MyThread mt2 = new MyThread("CMP");
                   System.out.println("mt2 thread's initial name and priority");
                   System.out.println(mt2.getName());
                   System.out.println(mt2.getPriority());
                   System.out.println();
                   Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
                   mt1.setPriority(5);
                   mt2.setPriority(8);
                   System.out.println(mt1.getPriority());
                   System.out.println();
                   System.out.println(mt2.getPriority());
                   mt1.start();
                   mt2.start();
          }
}

Output –
mt1 thread's initial name and priority
Thread-0
5

mt2 thread's initial name and priority
CMP
5

5

8
Thread-0i:0
CMPi:0
Thread-0i:1
CMPi:1
Thread-0i:2
CMPi:2 

No comments:

Post a Comment