原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11871132.html
Java线程--ThreadPoolExecutor使用
public static void main(String[] args) { ExecutorService service = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy()); for(int i=0 ; i<3 ; i++) { service.execute(new ThreadPoolExecutorTask()); } }
/** * 线程池执行器任务 */ class ThreadPoolExecutorTask implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()); } }
打印结果如下:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task concurrent.ThreadPoolExecutorTask@6d6f6e28 rejected from java.util.concurrent.ThreadPoolExecutor@135fbaa4[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379) at concurrent.MainConcurrent.main(MainConcurrent.java:37) pool-1-thread-1 pool-1-thread-2
可以看出, 超出最大的线程池设置大小之后, 就会报异常!
其他参数不过多做介绍.