关闭静态创建的 executorservice 块分配的变量本身?
closing statically created executorservice blocks assigned variables itself?
我只是想知道这段代码是如何工作的
假设下面有工作class
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();
}
}
我在下面的方法中的某个地方调用这个 class
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 executorservice 可以阻止分配的一个。
实际上 executorService 持有与 Work.executorService() 相同的引用,因此它受到关闭 Work 的 executorservice 的影响。
我只是想知道这段代码是如何工作的
假设下面有工作class
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();
}
}
我在下面的方法中的某个地方调用这个 class
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 executorservice 可以阻止分配的一个。
实际上 executorService 持有与 Work.executorService() 相同的引用,因此它受到关闭 Work 的 executorservice 的影响。