Exception 和 stack 溢出

如果我们程序中有太多关于exception 的处理,就有可能引起

 
Exception in thread "main" java.lang.StackOverflowError
at java.lang.Exception.<init>(Exception.java:66)
at java.lang.RuntimeException.<init>(RuntimeException.java:62)

 

因为Exception 的初始化和处理都有要对栈的操作,所以自然占栈的空间。

 

比如

 

public class ThreadTest {

    public static void main(String[]args){
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(0, 1, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1), new ThreadPoolExecutor.AbortPolicy());
        List<String> imagesForTask =new ArrayList<String>();
        for(int i=0;i<100000;i++){
            imagesForTask.add("aaa");
        }
        scanRegistryImage(threadPool,0,imagesForTask);

    }
    private static void scanRegistryImage(final ThreadPoolExecutor threadPool,int idx, List<String> imagesForTask) {
        if (idx >= imagesForTask.size()) {
            return;
        }
        int current = idx;
        try {
            for (; current < imagesForTask.size(); current++) {
                System.out.println("current task :" + current + " is prepared to goin pool.");
                threadPool.execute(new ScanImageTask());
                System.out.println("current task :" + current + " is ok to goin pool.");
            }
        } catch (RejectedExecutionException e) {
            try {
                Thread.sleep(1);
                System.gc(); //如果这行不加,就会有栈溢出。
                System.out.println("scanRegistryImage has RejectedExecutionException with " + current + " so wait seconds, will do in next loop.");
                System.out.println("getActiveCount :"+threadPool.getActiveCount());
                System.out.println("getPoolSize :"+threadPool.getPoolSize());
                System.out.println("getTaskCount :"+threadPool.getTaskCount());
                scanRegistryImage(threadPool,  current, imagesForTask);
            } catch (Exception e1) {
                e1.printStackTrace();
            }

        }catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
}

 

public class ScanImageTask implements Runnable, Serializable {
	@Override
	public void run() {
		
		try {
            System.out.println("ScanImageTask start ##########################################################################################");
            Thread.sleep(100);
            System.out.println("ScanImageTask end ##########################################################################################");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

 

如果没有这行 System.gc(); 程序运行一半就会奔溃退出。

current task :86 is prepared to goin pool.
Exception in thread "main" java.lang.StackOverflowError
    at java.lang.Exception.<init>(Exception.java:66)
    at java.lang.RuntimeException.<init>(RuntimeException.java:62)
    at java.util.concurrent.RejectedExecutionException.<init>(RejectedExecutionException.java:64)
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值