Tuesday, January 26, 2016

Thread Groups , Priority and Scheduler

Thread Group

Java provides a convenient way to group multiple threads in a single object. In such way, we can suspend, resume or interrupt group of threads by a single method call.



Example code,

public class ThreadGroupDemo implements Runnable{
 
public void run() {
          System.out.println(Thread.currentThread().getName());
    }
 
    public static void main(String[] args) {

          ThreadGroupDemo runnable = new ThreadGroupDemo();
     
ThreadGroup tg1 = new ThreadGroup("Parent ThreadGroup");
         
          Thread t1 = new Thread(tg1, runnable,"one");
          t1.start();
          Thread t2 = new Thread(tg1, runnable,"two");
          t2.start();
          Thread t3 = new Thread(tg1, runnable,"three");
          t3.start();
             
          System.out.println("Thread Group Name: "+tg1.getName());
         tg1.list();

    }
   }
 
Output:

one

two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
    Thread[one,5,Parent ThreadGroup]
    Thread[two,5,Parent ThreadGroup]
    Thread[three,5,Parent ThreadGroup]



Thread Priorities
  • Each thread have a priority.
  • Priorities are represented by a number between 1 and 10. 
  • In most cases, thread scheduler schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.

3 constants defiend in Thread class:

public static int MIN_PRIORITY

public static int NORM_PRIORITY

public static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.


Example of priority of a Thread:

class TestMultiPriority1 extends Thread{

 public void run(){

   System.out.println("running thread name is:"+Thread.currentThread().getName());
   System.out.println("running thread priority is:"+Thread.currentThread().getPriority());

  }

 public static void main(String args[]){

  TestMultiPriority1 m1=new TestMultiPriority1();
  TestMultiPriority1 m2=new TestMultiPriority1();
  m1.setPriority(Thread.MIN_PRIORITY);
  m2.setPriority(Thread.MAX_PRIORITY);
  m1.start();
  m2.start();
 
 }
}  


Output:
       running thread name is:Thread-0
       running thread priority is:10
       running thread name is:Thread-1

       running thread priority is:1


Thread Scheduler 

Thread scheduler in java is the part of the JVM that decides which thread should run.

There is no guarantee that which runnable thread will be chosen to run by the thread scheduler.

Only one thread at a time can run in a single process.

The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.

Difference between preemptive scheduling and time slicing

Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.

Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks.

 The scheduler then determines which task should execute next, based on priority and other factors.




No comments:

Post a Comment