原创

java.util.concurrent.RejectedExecutionException解决办法

1.先上问题代码

public static void main(String[] args) {        // TODO Auto-generated method stub       // ExecutorService exe = Executors.newCachedThreadPool();     
   ExecutorService exe = Executors.newFixedThreadPool(5);        for (int i = 0; i < 5; i++) {                    exe.execute(new LiftOff());                                  exe.shutdown();                                }              System.out.println("pool end ...");             }

2、再上异常

#0(9).Exception in thread "main" #0(8).#0(7).#0(6).#0(5).#0(4).#0(3).#0(2).#0(1).#0(LiftOff).
java.util.concurrent.RejectedExecutionException: Task com.tjbool.enumtest.LiftOff@5c647e05 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Shutting down, pool size = 1, active threads = 1, 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 com.tjbool.enumtest.CachedTheadPool.main(CachedTheadPool.java:42)

3、解决办法参考https://blog.csdn.net/novelly/article/details/19646449

改过之后的代码

public static void main(String[] args) {        // TODO Auto-generated method stub       // ExecutorService exe = Executors.newCachedThreadPool();        ExecutorService exe = Executors.newFixedThreadPool(5);        for (int i = 0; i < 5; i++) {                        try {                if(!exe.isShutdown()){                                     exe.execute(new LiftOff());                }                if (i == 4) {                                     exe.shutdown();                    System.out.println("pool shutdown ...");                }                               } catch (Exception e) {                                e.printStackTrace();               }              }              System.out.println("pool end ...");             }


正文到此结束
Loading...