daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 59 in 6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.concurrent.ExecutorService;
  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.ThreadFactory;
  4.  
  5.  
  6. class ExceptionThread2 implements Runnable {
  7.  
  8.     public void run() {
  9.         Thread t = Thread.currentThread();
  10.         System.out.println("run() by " + t);
  11.         System.out.println("eh = " + t.getUncaughtExceptionHandler());
  12.         throw new RuntimeException();
  13.     }
  14. }
  15.  
  16. class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
  17.  
  18.     public void uncaughtException(Thread t, Throwable e) {
  19.         System.out.println(t + " caught " + e);
  20.     }
  21. }
  22.  
  23. class HandlerThreadFactory implements ThreadFactory {
  24.     public Thread newThread(Runnable r) {
  25.         System.out.println(this + " creating new Thread");
  26.         Thread t = new Thread(r);
  27.         System.out.println("created " + t);
  28.         t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
  29.         System.out.println("eh = " + t.getUncaughtExceptionHandler());
  30.         return t;
  31.     }
  32. }
  33.  
  34. public class CaptureUncaughtException {
  35.     public static void main(String[] args) {
  36.         ExecutorService exec = Executors.newCachedThreadPool(
  37.                 new HandlerThreadFactory());
  38.         exec.execute(new ExceptionThread2());
  39.     }
  40. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top