Wednesday, 5 March 2014

ThreadGroup interrupt

ThreadGroup class provides interrupt method, to interrupt all the threads in a group.

public final void interrupt()
Interrupts all threads in this thread group and its sub groups.

Example
class InterruptEx extends Thread{
 public void run(){
  while(!isInterrupted()){
  }
  System.out.println(Thread.currentThread().getName() + " Interrupted");
 }

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

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

  InterruptEx t1 = new InterruptEx(group1, "Thread1Group1");
  InterruptEx t2 = new InterruptEx(group1, "Thread2Group1");
  InterruptEx t3 = new InterruptEx(group1, "Thread3Group2");
  InterruptEx t4 = new InterruptEx(group1, "Thread4Group2"); 
 
  t1.start();
  t2.start();
  t3.start();
  t4.start();

  group1.interrupt();

  t1.join();
  t2.join();
  t3.join();
  t4.join();
 }
}






Get maximum priority                                                 Daemon Thread Group                                                 Home

No comments:

Post a Comment