关闭静态创建的executionservice块本身分配的变量



我只是想知道这个代码是如何工作的

假设我们在下面有工作

public  class Work  {
private static ThreadPoolExecutor executorService;
private  Work(){};
public static void instansiate(int numberOfThread){
executorService= (ThreadPoolExecutor) Executors.newFixedThreadPool(numberOfThread);
}
public static void shutDown(){
executorService.shutdown();
}
public static ExecutorService getExecutorService() {
return executorService;
}
public static int getThreadCount(){
return executorService.getCorePoolSize();
}
}

我在下面的方法中调用这个类

public static void xx() throws ExecutionException, InterruptedException {
Work.instansiate(2);
System.out.println("Thread count= " + Work.getThreadCount());
ExecutorService executorService = Work.getExecutorService();
Future<String> future1 = executorService.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "future1";
}
});
String resFuture1 = future1.get();
System.out.println(resFuture1);
Work.shutDown();
Future<String> future2 = executorService.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "future2";
}
});
String resFuture2 = future2.get();
System.out.println(resFuture2);
}

此代码在Work.shutDown((行后抛出异常,并表示从java.util.concurrent.ThreadPoolExecutor@234bef66[已终止,池大小=0,活动线程=0…

我已经将Work.getExecutorService分配给了另一个executorService,关闭Work executorServices如何阻止分配的一个。

实际上executorService与Work.execuleService((持有相同的引用,因此它受到了关闭Work的executorServices的影响。

相关内容

  • 没有找到相关文章

最新更新