Java線程學習筆記(二) 線程的異常捕捉,java學習筆記

來源:互聯網
上載者:User

Java線程學習筆記(二) 線程的異常捕捉,java學習筆記

線程異常的捕捉:

正常的情況下,我們在main()方法裡是捕捉不到線程的異常的,例如以下代碼:

public class ExceptionThread implements Runnable{    @Override    public void run() {        throw new NullPointerException();    }    public static void main(String[] args) {        ExecutorService executorService = Executors.newCachedThreadPool();        try {            System.out.println("執行線程");            executorService.execute(new ExceptionThread());        } catch (Exception e) {            e.printStackTrace();            System.out.println("捕捉異常");        }    }}
上述代碼並不能在main方法裡捕捉線程異常,那麼我們怎麼才能捕捉到線程的異常資訊呢?

下面我們看這段代碼

/** * 定義異常線程內容 */class MyExceptionThread implements Thread.UncaughtExceptionHandler {    @Override    public void uncaughtException(Thread t, Throwable e) {        // 捕捉異常後的業務處理放在這裡        System.out.println("捕捉的異常資訊如下");        System.out.println(e);    }}/** * 定義異常線程工廠 */class ExceptionThreadFactory implements ThreadFactory {    @Override    public Thread newThread(Runnable r) {        Thread thread = new Thread(r);        // 此處是捕捉異常的設定。        thread.setUncaughtExceptionHandler(new MyExceptionThread());        return thread;    }}/** * 運行線程 */class ExceptionThread2 implements Runnable{    @Override    public void run() {        try {            Thread.sleep(1000l);        } catch (InterruptedException e) {            e.printStackTrace();        }        // 拋出異常        throw new NullPointerException();    }    public static void main(String[] args) {        // 通過我們自己寫的ExceptionThreadFactory線程工廠,構造線程池        ExecutorService executorService = Executors.newCachedThreadPool(new ExceptionThreadFactory());        try {            System.out.println("執行線程");            // 啟動三個線程            executorService.execute(new ExceptionThread2());            executorService.execute(new ExceptionThread2());            executorService.execute(new ExceptionThread2());        } catch (Exception e) {            e.printStackTrace();            System.out.println("捕捉異常");        }    }}
上面的輸出結果是
執行線程捕捉的異常資訊如下java.lang.NullPointerException捕捉的異常資訊如下java.lang.NullPointerException捕捉的異常資訊如下java.lang.NullPointerException
結論就是:main()方法依然沒有捕捉到線程的異常,當然這個設定是合理的。而每個線程現在有了自己的異常捕捉機制,如何做到的呢,一句話,建立線程的時候就聲明好~

收工!


Java Swing 怎等待第一個線程執行結束後自動開始第二個線程

class one{
public One(){
start();

}

void start(){
logic();
new Thread(...).start();//開新的線程

}

void logic(){
//第一個頁面要做的事情
......

}

}
難道不就是這樣嗎?
 
Java異常處理線程問題

用這個介面試試,java.util.concurrent .ScheduledExecutorService,當串連失敗時啟動下面的任務再去串連。
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);

public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.