Java - ThreadGroup interrupt() Method with Examples



Description

The Java ThreadGroup getParent() method interrupts all threads in this thread group.

Declaration

Following is the declaration for java.lang.ThreadGroup.interrupt() method

public final void interrupt()

Parameters

NA

Return Value

This method does not return any value.

Exception

SecurityException − if the current thread is not allowed to access this thread group or any of the threads in the thread group.

Example 1

The following example shows the usage of ThreadGroup interrupt() method in case of a single ThreadGroup object. We've created a ThreadGroup object and assigned it a name. Then we've created two threads using the threadgroup object created earlier. Both threads are started and using Thread.sleep() we're introducing a delay of a second. Then we're printing active threads in the group. using stopThread() method, we're allowing current thread to stop and then using interrupt() method, we're interrupting all running threads within the group.

package com.tutorialspoint;

class newThread extends Thread {
   boolean stop;

   newThread(ThreadGroup group, String name) {
      super(group, name);
      stop = false;
   }

   public void run() {
      System.out.println(Thread.currentThread().getName() + " starting.");
      try {
         for(int i = 1; i < 1000; i++) {
            Thread.sleep(500);
            synchronized(this) {
               if(stop)
                  break;
            }   
         }
      } catch(Exception e) {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" interrupted.");
      }
      System.out.println(Thread.currentThread().getName() + " exiting.");
   }

   synchronized void stopThread() {
      stop = true;
   }
}

public class ThreadGroupDemo {

   public static void main(String args[]) throws Exception {

      ThreadGroup group = new ThreadGroup("new Group");

      newThread t1 = new newThread(group, "Thread1");
      newThread t2 = new newThread(group, "Thread2");
   
      // this will call run() method
      t1.start();
      t2.start();
   
      Thread.sleep(1000);

      // it shows current active threads in Thread Group
      System.out.println(group.activeCount() + " threads in thread group...");

      // returns the number of thread groups
      Thread th[] = new Thread[group.activeCount()];
      group.enumerate(th);
      for(Thread t : th)
         System.out.println(t.getName());

      t1.stopThread();
      Thread.sleep(1000);
    
      System.out.println(group.activeCount() + " threads in thread group...");
      // thread group interrupted
      group.interrupt();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Thread1 starting.
Thread2 starting.
2 threads in thread group...
Thread1
Thread2
Thread1 exiting.
1 threads in thread group...
Thread2 interrupted.
Thread2 exiting.

Example 2

The following example shows the usage of ThreadGroup interrupt() method in case of a two ThreadGroup objects. We've created parent, child ThreadGroup objects and assigned them names. Then we've created two threads using the threadgroup object created earlier. Both threads are started and using Thread.sleep() we're introducing a delay of a second. Then we're printing active threads in the group. using stopThread() method, we're allowing current thread to stop and then using interrupt() method, we're interrupting all running threads within the parent thread group.

package com.tutorialspoint;

class newThread extends Thread {
   boolean stop;

   newThread(ThreadGroup group, String name) {
      super(group, name);
      stop = false;
   }

   public void run() {
      System.out.println(Thread.currentThread().getName() + " starting.");
      try {
         for(int i = 1; i < 1000; i++) {
            Thread.sleep(500);
            synchronized(this) {
               if(stop)
                  break;
            }   
         }
      } catch(Exception e) {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" interrupted.");
      }
      System.out.println(Thread.currentThread().getName() + " exiting.");
   }

   synchronized void stopThread() {
      stop = true;
   }
}

public class ThreadGroupDemo {

   public static void main(String args[]) throws Exception {

      // create a parent ThreadGroup
      ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
      // create a child ThreadGroup for parent ThreadGroup
      ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");

      newThread t1 = new newThread(cThreadGroup, "Thread1");
      newThread t2 = new newThread(cThreadGroup, "Thread2");
   
      // this will call run() method
      t1.start();
      t2.start();
   
      Thread.sleep(1000);

      // it shows current active threads in Thread Group
      System.out.println(cThreadGroup.activeCount() + " threads in child thread group...");

      // returns the number of thread groups
      Thread th[] = new Thread[cThreadGroup.activeCount()];
      cThreadGroup.enumerate(th);
      for(Thread t : th)
         System.out.println(t.getName());

      t1.stopThread();
      Thread.sleep(1000);
    
      System.out.println(cThreadGroup.activeCount() + " threads in parent thread group...");
      // thread group interrupted
      pThreadGroup.interrupt();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Thread1 starting.
Thread2 starting.
2 threads in child thread group...
Thread1
Thread2
Thread1 exiting.
1 threads in parent thread group...
Thread2 interrupted.
Thread2 exiting.

Example 3

The following example shows the usage of ThreadGroup interrupt() method in case of a multiple ThreadGroup objects. We've created parent, child ThreadGroup, grandchild ThreadGroup objects and assigned them names. Then we've created two threads using the threadgroup object created earlier. Both threads are started and using Thread.sleep() we're introducing a delay of a second. Then we're printing active threads in the group. using stopThread() method, we're allowing current thread to stop and then using interrupt() method, we're interrupting all running threads within the parent thread group.

package com.tutorialspoint;

class newThread extends Thread {
   boolean stop;

   newThread(ThreadGroup group, String name) {
      super(group, name);
      stop = false;
   }

   public void run() {
      System.out.println(Thread.currentThread().getName() + " starting.");
      try {
         for(int i = 1; i < 1000; i++) {
            Thread.sleep(500);
            synchronized(this) {
               if(stop)
                  break;
            }   
         }
      } catch(Exception e) {
         System.out.print(Thread.currentThread().getName());
         System.out.println(" interrupted.");
      }
      System.out.println(Thread.currentThread().getName() + " exiting.");
   }

   synchronized void stopThread() {
      stop = true;
   }
}

public class ThreadGroupDemo {

   public static void main(String args[]) throws Exception {

      // create a parent ThreadGroup
      ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
      // create a child ThreadGroup for parent ThreadGroup
      ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");
	  
	  // create a grand child ThreadGroup for parent ThreadGroup
      ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "grandchild ThreadGroup");

      newThread t1 = new newThread(gThreadGroup, "Thread1");
      newThread t2 = new newThread(gThreadGroup, "Thread2");
   
      // this will call run() method
      t1.start();
      t2.start();
   
      Thread.sleep(1000);

      // it shows current active threads in Thread Group
      System.out.println(gThreadGroup.activeCount() + " threads in grandchild thread group...");

      // returns the number of thread groups
      Thread th[] = new Thread[gThreadGroup.activeCount()];
      gThreadGroup.enumerate(th);
      for(Thread t : th)
         System.out.println(t.getName());

      t1.stopThread();
      Thread.sleep(1000);
    
      System.out.println(gThreadGroup.activeCount() + " threads in parent thread group...");
      // thread group interrupted
      pThreadGroup.interrupt();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Thread1 starting.
Thread2 starting.
2 threads in grandchild thread group...
Thread1
Thread2
Thread1 exiting.
1 threads in parent thread group...
Thread2 interrupted.
Thread2 exiting.
java_lang_threadgroup.htm
Advertisements