Java ThreadGroup 类

描述

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

声明

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

public final void interrupt()

参数

返回值

此方法不返回任何值。

异常

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

示例 1

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

package com.yxjc123;

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");
   
      //这将调用 run() 方法
      t1.start();
      t2.start();
   
      Thread.sleep(1000);

      //显示线程组中当前活动的线程
      System.out.println(group.activeCount() + " threads in thread group...");

      //返回线程组的数量
      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...");
      //线程组中断
      group.interrupt();
   }
}

输出

Let我们编译并运行上面的程序,这将产生以下结果 -

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

示例 2

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

package com.yxjc123;

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
      ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
      //为父ThreadGroup创建子ThreadGroup
      ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");

      newThread t1 = new newThread(cThreadGroup, "Thread1");
      newThread t2 = new newThread(cThreadGroup, "Thread2");
   
      // 这将调用 run() 方法
      t1.start();
      t2.start();
   
      Thread.sleep(1000);

      //显示线程组中当前活动的线程
      System.out.println(cThreadGroup.activeCount() + " threads in child thread group...");

      //返回线程组的数量
      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...");
      //线程组中断
      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.

示例 3

以下示例显示了 ThreadGroup Interrupt() 方法在发生以下情况时的用法:多个 ThreadGroup 对象。我们创建了父 ThreadGroup、子 ThreadGroup、孙 ThreadGroup 对象并为它们分配了名称。然后我们使用之前创建的线程组对象创建了两个线程。两个线程都启动,并使用 Thread.sleep() 我们引入了一秒的延迟。然后我们打印组中的活动线程。使用stopThread()方法,我们允许当前线程停止,然后使用interrupt()方法,我们中断父线程组内所有正在运行的线程。

package com.yxjc123;

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
      ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
      //为父ThreadGroup创建子ThreadGroup
      ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");
	  
	  //为父ThreadGroup创建一个孙ThreadGroup
      ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "grandchild ThreadGroup");

      newThread t1 = new newThread(gThreadGroup, "Thread1");
      newThread t2 = new newThread(gThreadGroup, "Thread2");
   
      // 这将调用 run() 方法
      t1.start();
      t2.start();
   
      Thread.sleep(1000);

      //显示线程组中当前活动的线程
      System.out.println(gThreadGroup.activeCount() + " threads in grandchild thread group...");

      //返回线程组的数量
      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...");
      //线程组中断
      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.