zoukankan      html  css  js  c++  java
  • java 线程 捕获异常

    java 线程 捕获异常  来自:thinking in java 4 文件夹20.2.13


    package org.rui.thread.concurrent;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    /**
     * 捕获异常
     * 
     * 以下的任务总是会抛出一个异常。该异常会传播到其run方法的外部。
     * 而且main展示了当你执行它时,所发生的事情
     * @author lenovo
     *
     */
    public class ExceptionThread implements Runnable {
    
    	@Override
    	public void run() {
    		throw new RuntimeException();
    		
    	}
    	
    	public static void main(String[] args) {
    		/*ExecutorService exec=Executors.newCachedThreadPool();
    		exec.execute(new ExceptionThread());
    		*/
    		try {
    			ExecutorService exec=Executors.newCachedThreadPool();
    			exec.execute(new ExceptionThread());
    		} catch (Exception e) {
    			System.out.println("eeeeeeeeeeeeeeee 该语句将不执行!");
    		}
    		
    	
    	}
    
    }
    /**output: 以上输出结果一样:
     Exception in thread "pool-1-thread-1" java.lang.RuntimeException
    	at org.rui.thread.concurrent.ExceptionThread.run(ExceptionThread.java:15)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    	at java.lang.Thread.run(Thread.java:722)
     */
    

    package org.rui.thread.concurrent;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ThreadFactory;
    
    
    /** 
     *  捕获异常
     *  
     * 为了解决问题,我们要改动executor产生线程的方式。thread.UncaughtExceptionHandler是javaSE5中的新接口。
     * 它同意你在每一个Thread对象上都附着一个异常处理器......
     *
     * @author lenovo
     *
     */
    class ExceptionThread2 implements Runnable
    {
    	@Override
    	public void run() {
    	Thread t=Thread.currentThread();
    	System.out.println("run by : "+t);
    	System.out.println(t.getUncaughtExceptionHandler());
    	throw new RuntimeException();
    	}
    	
    }
    //////////////////无知的Exception
    class MyUncaughtExecptionHandler implements Thread.UncaughtExceptionHandler
    {
    	@Override
    	public void uncaughtException(Thread t, Throwable e) {
    		
    		System.out.println("caught "+e);
    		
    	}
    	
    }
    ////////////////
    class handlerThreadFactory implements ThreadFactory
    {
    
    	@Override
    	public Thread newThread(Runnable r) {
    		System.out.println("创建新的线程");
    		Thread t=new Thread(r);
    		t.setUncaughtExceptionHandler(new MyUncaughtExecptionHandler());
    		System.out.println("eh= "+t.getUncaughtExceptionHandler());
    		return t;
    	}
    	
    }
    
    public class CaptureUncaughtExecption {
    	public static void main(String[] args) {
    		ExecutorService exec=Executors.newCachedThreadPool(new handlerThreadFactory()	);
    		exec.execute(new ExceptionThread2());
    		
    		
    	}
    
    }
    
    /**
     output:
     创建新的线程
    eh= org.rui.thread.concurrent.MyUncaughtExecptionHandler@192c8d9
    run by : Thread[Thread-0,5,main]
    org.rui.thread.concurrent.MyUncaughtExecptionHandler@192c8d9
    创建新的线程
    eh= org.rui.thread.concurrent.MyUncaughtExecptionHandler@16f144c
    caught java.lang.RuntimeException
     */
    

    package org.rui.thread.concurrent;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    /**
     * 这个处理器仅仅有在不存在线程专有的末捕获异常处理器的情况下才会被调用。

    * 系统会检查线程专有版 本。假设没有发现。则检查线程组是否有其专有的uncaughtException()方法。 * 假设也没有。再调用defaultUncaughtExceptionHandler * @author lenovo * */ public class SettingDefaultHandler { public static void main(String[] args) { Thread.setDefaultUncaughtExceptionHandler( new MyUncaughtExecptionHandler() ); ExecutorService exec=Executors.newCachedThreadPool(); exec.execute(new ExceptionThread()); } } /** * output: caught java.lang.RuntimeException */



  • 相关阅读:
    19.递归法和非递归法反转链表[ReverseLinkedList]
    18.用两个栈实现队列[2StacksToImplementQueue]
    17.把字符串转换成整数[atoi]
    16.O(logn)求Fibonacci数列[Fibonacci]
    15.含有指针成员的类的拷贝[ClassCopyConstructorWithPointerMember]
    14.约瑟夫环问题[JosephusProblem]
    13.第一个只出现一次的字符[FindFirstNotRepeatingChar]
    12.从上往下遍历二元树[LevelOrderOfBinaryTree]
    洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm
    洛谷 P2733 家的范围 Home on the Range
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/7063522.html
Copyright © 2011-2022 走看看