Wednesday, 5 March 2014

getMaxPriority : get priority of thread group

public final int getMaxPriority()
Returns the maximum priority of this thread group. Threads that are part of this group cannot have a higher priority than the maximum priority.

class PriorityEx extends Thread{
 public void run(){
  try{
   Thread.currentThread().sleep(1000);
   Thread local = Thread.currentThread();
   System.out.println(local.getName() + " priority is " + local.getPriority());
  }
  catch(InterruptedException e){
  }
 }

 PriorityEx(ThreadGroup grp, String name){
  super(grp, name);
 }

 public static void main(String args[]){
  ThreadGroup group1 = new ThreadGroup("group1");

  PriorityEx t1 = new PriorityEx(group1, "Thread1");
  t1.setPriority(9);
  t1.start();

  group1.setMaxPriority(8);
  System.out.println("Group1 priority is set to " + group1.getMaxPriority());

  PriorityEx t2 = new PriorityEx(group1, "Thread2");
  t2.setPriority(9);
  t2.start();
 }
}

Output
Group1 priority is set to 8
Thread1 priority is 9
Thread2 priority is 8
  
Group1 maximum priority is set to 9, but thread t1 set to priority 9, before setting thr grup1 priority, so it is no affected. But Thread t2 trying to set priority of 9, after setting the group maximum priority of 8, So Thread t2 limited to its priority 8.

Related Posts



Set maximum Priority                                                 Interrupt Thread Group                                                 Home

No comments:

Post a Comment