描述
這個java.lang.ThreadGroup.uncaughtException()當該線程組中的線程由於未捕獲的異常而停止,並且該線程沒有安裝特定的 Thread.UncaughtExceptionHandler 時,Java 虛擬機將調用該方法。
聲明
以下是聲明java.lang.ThreadGroup.uncaughtException()方法
public void uncaughtException(Thread t, Throwable e)
參數
t- 這是即將退出的線程。
e- 這是未捕獲的異常。
返回值
此方法不返回任何值。
異常
NA
示例
下麵的例子展示了 java.lang.ThreadGroup.uncaughtException() 方法的用法。
package com.tutorialspoint;
import java.lang.*;
public class ThreadGroupDemo implements Runnable {
public static void main(String[] args) {
ThreadGroupDemo tg = new ThreadGroupDemo();
tg.func();
}
public void func() {
try {
// create a new ThreadGroup and a child for that ThreadGroup.
newThreadGroup pGroup = new newThreadGroup("ParentThreadGroup");
newThreadGroup cGroup = new newThreadGroup
(pGroup,"ChildThreadGroup");
// create another thread
Thread thr2 = new Thread(pGroup, this);
System.out.println("Starting " + thr2.getName() + "...");
// this will call run() method
thr2.start();
// create third thread
Thread thr3 = new Thread(cGroup, this);
System.out.println("Starting " + thr3.getName() + "...");
// this will call run() method
thr3.start();
try {
Thread.sleep(500);
} catch(InterruptedException ex) {}
// interrupt the two threads
thr2.interrupt();
thr3.interrupt();
// block until the other threads finish
thr2.join();
thr3.join();
} catch(InterruptedException e) {
System.out.println(e.toString());
}
}
public void run() {
try {
System.out.print(Thread.currentThread().getName());
System.out.println(" executing...");
while(true) {
Thread.sleep(500);
}
} catch(InterruptedException e) {
Thread currThread = Thread.currentThread();
System.out.print(currThread.getName());
System.out.println(" interrupted:" + e.toString());
// rethrow the exception
throw new RuntimeException(e.getMessage());
}
}
}
class newThreadGroup extends ThreadGroup {
newThreadGroup(String n) {
super(n);
}
newThreadGroup(ThreadGroup parent, String n) {
super(parent, n);
}
public void uncaughtException(Thread t, Throwable e) {
System.out.println(t + " has unhandled exception:" + e);
}
}
讓我們編譯並運行上麵的程序,這將產生以下結果 -
Starting Thread-0... Starting Thread-1... Thread-0 executing... Thread-1 executing... Thread-0 interrupted:java.lang.InterruptedException:sleep interrupted Thread[Thread-0,5,ParentThreadGroup] has unhandled exception:java.lang.RuntimeException:sleep interrupted Thread-1 interrupted:java.lang.InterruptedException:sleep interrupted Thread[Thread-1,5,ChildThreadGroup] has unhandled exception:java.lang.RuntimeException:sleep interrupted
相關用法
- Java Java.lang.ThreadGroup.parentOf()用法及代碼示例
- Java Java.lang.ThreadGroup.getName()用法及代碼示例
- Java Java.lang.ThreadGroup.isDestroyed()用法及代碼示例
- Java Java.lang.ThreadGroup.interrupt()用法及代碼示例
- Java Java.lang.ThreadGroup.destroy()用法及代碼示例
- Java Java.lang.ThreadGroup.isDaemon()用法及代碼示例
- Java Java.lang.ThreadGroup.list()用法及代碼示例
- Java Java.lang.ThreadGroup.checkAccess()用法及代碼示例
- Java Java.lang.ThreadGroup.enumerate()用法及代碼示例
- Java Java.lang.ThreadGroup.toString()用法及代碼示例
- Java Java.lang.ThreadGroup.setDaemon()用法及代碼示例
- Java Java.lang.ThreadGroup.activeCount()用法及代碼示例
- Java Java.lang.ThreadGroup.getParent()用法及代碼示例
- Java Java.lang.ThreadGroup.getMaxPriority()用法及代碼示例
- Java Java.lang.ThreadGroup.setMaxPriority()用法及代碼示例
- Java Java.lang.Thread.run()用法及代碼示例
- Java Java.lang.Thread.isDaemon()用法及代碼示例
- Java Java.lang.Thread.checkAccess()用法及代碼示例
- Java Java.lang.Thread.getState()用法及代碼示例
- Java Java.lang.Thread.yield()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.lang.ThreadGroup.uncaughtException() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。