【问题标题】:future.get after ScheduledThreadPoolExecutor shutdown, will it work?在 ScheduledThreadPoolExecutor 关闭后,future.get 会起作用吗?
【发布时间】:2013-10-29 12:01:58
【问题描述】:

我们使用 ScheduledThreadPoolExecutor 并在提交作业后立即调用关闭。 因为按照文档 Shutdown 不会杀死提交的任务,运行任务并允许它完成。

问题是shutdown后我们能否继续使用ScheduledThreadPoolExecutor提交返回的future对象。

私有静态未来 submitACall(Callable callableDelegate) { ScheduledThreadPoolExecutor threadPoolExe = null; 尝试 { threadPoolExe = new ScheduledThreadPoolExecutor(1); 返回 threadPoolExe.submit(callableDelegate); } 最后 { threadPoolExe.shutdown(); } } //在另一个方法中... 如果(未来.isDone()) 未来.get();

【问题讨论】:

    标签: threadpool java.util.concurrent scheduledexecutorservice


    【解决方案1】:

    是的,你可以,在try-catch

    package testsomething;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Future;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    
    public class TestSomething {
        private static Future future = null;
        private static ScheduledThreadPoolExecutor threadPoolExe = null;
    
        public static void main(String[] args) {
            Callable callableDelegate = new MyCallable();
            future = submitACall(callableDelegate);
            try {
                System.out.println("First get: " + ((Integer)future.get()));
            } catch (InterruptedException | ExecutionException ex) {
                System.out.println("Exception: " + ex);
            }
            try {
                Thread.sleep(100L);
            } catch (InterruptedException ex) {
                System.out.println("Exception: " + ex);
            }
    
            try {
                System.out.println("Thread pool shut down? " + threadPoolExe.isShutdown());
                System.out.println("Second get through 'anotherMethod': " + anotherMethod());
            } catch (InterruptedException | ExecutionException ex) {
                System.out.println("Exception: " + ex);
            }
        }
    
        private static Future submitACall(Callable callableDelegate) {
            try {
                threadPoolExe = new ScheduledThreadPoolExecutor(1);     
                return 
                    threadPoolExe.submit(callableDelegate);
            } finally {     
                threadPoolExe.shutdown();
            }
          }
        private static Integer anotherMethod() throws ExecutionException, InterruptedException {
            if(future.isDone())
                return ((Integer)future.get());
            else 
                return null;
        }
    
        private static class MyCallable implements Callable {
            @Override
            public Object call() throws Exception {
                return new Integer(0);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 2016-11-30
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多