Original: Reprinted indicate the original address https://www.cnblogs.com/fanerwei222/p/11871132.html
Java threads use --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()); } }
/ ** * task execution thread pool * / class ThreadPoolExecutorTask the implements the Runnable { @Override public void RUN () { System.out.println (. Thread.currentThread () getName ()); } }
Print results are as follows:
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
Then it can be seen, exceeds the maximum size of the thread pool settings, an exception will be reported!
But it does introduce other parameters.