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
廣告
© . All rights reserved.