999yzc亚洲城在线娱乐城java.lang.InterruptedException: sleep interrupted

( 999yzc亚洲城在线娱乐城 )

以下是main函数中的代码,想测试一下shutdownNow的结果。

public class TestLa {
    public static void main(String... args){
        /*省略了一段故意义的测试代码*/
        
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        
        class Task implements Callable<Integer> {
            public int id;
            Task(int id) {
                this.id = id;
            }
            @Override
            public Integer call() throws Exception {
                System.out.println(String.format("我的id是:%s,末尾谋略>>>...",id));
                int sum = 0;
                for(int i = 0; i < 100; i++){
                    sum += i;
                    Thread.sleep(100);
                }
                System.out.println(String.format("我的id是:%s,实行终了>>>...",id));
                return sum;
            }
        }
        
        Future<Integer> future = executorService.submit(new Task(1));
        executorService.submit(new Task(2));
        executorService.submit(new Task(3));
        executorService.submit(new Task(4));
        
        List<Runnable> tasksWaitExecutingList = executorService.shutdownNow();
        
        System.out.println("我们在实行器终了时未完成:");
        for(Runnable item : tasksWaitExecutingList) {
            Thread thisThread = new Thread(item);
            thisThread.start();
        }
        /*executorService.shutdown();*/
        try {
            System.out.println("谋略后果:"+ future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

次序运转的后果:

我们在实行器终了时未完成:
我的id是:3,末尾谋略>>>...
我的id是:1,末尾谋略>>>...
我的id是:2,末尾谋略>>>...
我的id是:4,末尾谋略>>>...
java.util.concurrent.ExecutionException: java.lang.InterruptedException: sleep interrupted
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at com.mq.xx.entrust.action.TestLa.main(TestLa.java:137)
Caused by: java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at com.mq.xx.entrust.action.TestLa$1Task.call(TestLa.java:116)
    at com.mq.xx.entrust.action.TestLa$1Task.call(TestLa.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
我的id是:4,实行终了>>>...
我的id是:3,实行终了>>>...

不知道主线程加入怎样中缀了其他线程的,这里调用 future.get() 并不会壅闭主线程。而运转executorService.shutdown(),而不是shutdownNow的时分,次序不会抛出十分,主线程会壅闭到future.get()有前往的时分,不太明白

就应该是这个结果呀,shutdownNowdoc

There are no guarantees beyond best-effort attempts to stop processing
actively executing tasks. For example, typical implementations will
cancel via Thread.interrupt, so any task that fails to respond to
interrupts may never terminate.

线程调用interrupt假如正在sleep就会抛出这个十分。和future并没有什么干系。详细的你可以看看ExecutorService的完成呀。

shutdownNow是立即加入,不论有没有实行终了,以是会报InterruptedException
shutdown是会等到实行终了再加入,统统不会有十分。

(看完/读完)这篇文章有何感想! 999yzc亚洲城在线娱乐城的分享…

发表评论

电子邮件地址不会被公开。 必填项已用*标注