目录

使用场景

测试

现有线程池的缺点

    static class MyPolicy implements RejectedExecutionHandler {

        private int submitTimeoutms;

        static RejectedExecutionException exp = new RejectedExecutionException();

        public MyPolicy(int submitTimeoutms) {
            this.submitTimeoutms = submitTimeoutms;
        }
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            try {
                // queue 指线程池中的任务队列
                boolean res = queue.offer(r, submitTimeoutms, TimeUnit.MILLISECONDS);
                if(!res){
                    ((FutureTask) r).cancel(true);
                    throw exp;
                }
            } catch (InterruptedException e1) {
                throw exp;
            }
        }
    }