ThreadGroup類interrupt()方法
- interrupt() 方法在 java.lang 包中可用。
- interrupt() 方法用於停止該線程組中的所有線程。
- interrupt() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- interrupt() 方法可能會在中斷該線程組中的線程時拋出異常。
用法:
public final void interrupt();
參數:
- 它不接受任何參數。
返回值:
這個方法的返回類型是void
——它什麽都不返回。
例:
// Java program to demonstrate the example
// of void interrupt () method of ThreadGroup
public class Interrupt implements Runnable {
public static void main(String[] args) {
Interrupt in = new Interrupt(); in .interrupted();
}
public void interrupted() {
// Create two thread group and the named are base
// and derived
ThreadGroup base = new ThreadGroup("Base ThreadGroup");
ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");
// create two threads
Thread th1 = new Thread(base, this);
Thread th2 = new Thread(derived, this);
// By using getName() method is to retrieve the
// name of the thread th1
System.out.println(th1.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th1
th1.start();
//By using getName() method is to retrieve the
// name of the thread th2
System.out.println(th2.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th2
th2.start();
try {
th1.interrupt();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// Override run()
public void run() {
try {
Thread.sleep(1000);
System.out.println("We will wait till another thread execution");
} catch (Exception ex) {
System.out.println("Thread interrupted:" + ex.getMessage());
}
}
}
輸出
Thread-0 begins..... Thread-1 begins..... Thread interrupted:sleep interrupted We will wait till another thread execution
相關用法
- Java ThreadGroup isDaemon()用法及代碼示例
- Java ThreadGroup isDestroyed()用法及代碼示例
- Java ThreadGroup enumerate()用法及代碼示例
- Java ThreadGroup getMaxPriority()用法及代碼示例
- Java ThreadGroup getParent()用法及代碼示例
- Java ThreadGroup getName()用法及代碼示例
- Java ThreadGroup parentOf()用法及代碼示例
- Java ThreadGroup suspend()用法及代碼示例
- Java ThreadGroup setDaemon()用法及代碼示例
- Java ThreadGroup checkAccess()用法及代碼示例
- Java ThreadGroup resume()用法及代碼示例
- Java ThreadGroup toString()用法及代碼示例
- Java ThreadGroup destroy()用法及代碼示例
- Java ThreadGroup list()用法及代碼示例
- Java ThreadGroup stop()用法及代碼示例
- Java ThreadGroup activeCount()用法及代碼示例
- Java ThreadGroup setMaxPriority()用法及代碼示例
- Java ThreadGroup activeGroupCount()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java ThreadGroup interrupt() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。