因此,我一直在检查在ExecutorService中使用ExecutorService接口的可能性。然后,我看到Lukas回答here,它只是使用方法引用:
ExecutorService service = Executors.newSingleThreadExecutor();
try (Closeable close = service::shutdown) {
}因此,我一直在摸索,并希望在try块中初始化服务:
@Test(expected = RejectedExecutionException.class)
public void singleThreadTest() throws Exception {
ExecutorService es = null;
try (Closeable closeable = (es = Executors.newSingleThreadExecutor())::shutdown){
es.execute(()->System.out.println("executed " + Thread.currentThread().getId()));
}
System.out.println("executed " + Thread.currentThread().getId());
System.out.println(es.isShutdown());
es.execute(()->{});
}这个部分有点混乱,但如果有例外,我相信会被压制,然后我试过:
@Test(expected = RejectedExecutionException.class)
public void singleThreadTest() throws Exception {
ExecutorService es = null;
try (Closeable closeable = es::shutdown){
es = Executors.newSingleThreadExecutor();
es.execute(()->System.out.println("executed " + Thread.currentThread().getId()));
}
System.out.println("executed " + Thread.currentThread().getId());
System.out.println(es.isShutdown());
es.execute(()->{});
}对于这个代码块,我得到了NullPointerException,但是代码块成功地执行了(创建了新线程,但是在关闭()阶段,我得到了异常)。我注意到,当我宣布:
Closeable closeable = es::shutdownes必须是(有效的)最终的,这个问题只会发生在双冒号操作符上。也就是说,下面不编译:
try (Closeable closeable = new Closeable() {
@Override
public void close() throws IOException {
es.shutdown();
}
}){这是个虫子吗?造成这种情况的原因是什么?*方法引用impl?*IDE *JVM (oracle - debian)?
后续行动:
@Test(expected = RejectedExecutionException.class)
public void singleThreadTest() throws Exception {
ExecutorService es = null;
es = Executors.newSingleThreadExecutor();
es = Executors.newSingleThreadExecutor();
es = null;
try (Closeable closeable = ()->es.shutdown();){
es = Executors.newSingleThreadExecutor();
es.execute(()->System.out.println("executed " + Thread.currentThread().getId()));
}
System.out.println("executed " + Thread.currentThread().getId());
System.out.println(es.isShutdown());
es.execute(()->{});
}此代码不编译,但确实编译:
@Test(expected = RejectedExecutionException.class)
public void singleThreadTest() throws Exception {
ExecutorService es = null;
es = Executors.newSingleThreadExecutor();
es = Executors.newSingleThreadExecutor();
es = null;
try (Closeable closeable = es::shutdown;){
es = Executors.newSingleThreadExecutor();
es.execute(()->System.out.println("executed " + Thread.currentThread().getId()));
}
System.out.println("executed " + Thread.currentThread().getId());
System.out.println(es.isShutdown());
es.execute(()->{});
}发布于 2017-03-13 14:18:13
不不是窃听器。您正在关闭的变量/参数的值需要在捕获点知道。
当您获得NPE时,这是因为您在es = null时声明了匿名内部类(或lambda或方法引用)。这就是捕捉到的价值:未来的任务是看不见的。
Why can method reference use non-final variables?
为了阻止您犯这个错误,有一个限制"X必须(实际上)是最终的“,这意味着如果您试图更改要捕获的变量X的值,它将不会编译。
Why are only final variables accessible in anonymous class?
基本上,您需要将代码重新排列如下:
final ExecutorService es = Executors.newSingleThreadExecutor();
try (Closeable closeable = es::shutdown) {
// ...https://stackoverflow.com/questions/42765714
复制相似问题