Java - ThreadGroup interrupt() 方法



描述

Java ThreadGroup getParent() 方法中断此线程组中的所有线程。

声明

以下是 java.lang.ThreadGroup.interrupt() 方法的声明

public final void interrupt()

参数

返回值

此方法不返回值。

异常

SecurityException − 如果当前线程不允许访问此线程组或线程组中的任何线程。

ThreadGroup 对象中中断线程示例

以下示例演示了在单个 ThreadGroup 对象的情况下使用 ThreadGroup interrupt() 方法。我们创建了一个 ThreadGroup 对象并为其指定了一个名称。然后,我们使用前面创建的 threadgroup 对象创建了两个线程。这两个线程都启动了,并且使用 Thread.sleep() 我们引入了 1 秒的延迟。然后,我们打印组中的活动线程。使用 stopThread() 方法,我们允许当前线程停止,然后使用 interrupt() 方法,我们中断组内所有正在运行的线程。

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();
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

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

多个 ThreadGroup 对象中中断线程示例

以下示例演示了在两个 ThreadGroup 对象的情况下使用 ThreadGroup interrupt() 方法。我们创建了父、子 ThreadGroup 对象并为其指定了名称。然后,我们使用前面创建的 threadgroup 对象创建了两个线程。这两个线程都启动了,并且使用 Thread.sleep() 我们引入了 1 秒的延迟。然后,我们打印组中的活动线程。使用 stopThread() 方法,我们允许当前线程停止,然后使用 interrupt() 方法,我们中断父线程组内所有正在运行的线程。

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();
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

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

子/孙 ThreadGroup 对象中中断线程示例

以下示例演示了在多个 ThreadGroup 对象的情况下使用 ThreadGroup interrupt() 方法。我们创建了父、子 ThreadGroup、孙子 ThreadGroup 对象并为其指定了名称。然后,我们使用前面创建的 threadgroup 对象创建了两个线程。这两个线程都启动了,并且使用 Thread.sleep() 我们引入了 1 秒的延迟。然后,我们打印组中的活动线程。使用 stopThread() 方法,我们允许当前线程停止,然后使用 interrupt() 方法,我们中断父线程组内所有正在运行的线程。

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();
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

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
广告