我有以下代码:
ConcurrentHashMap taskMap= new ConcurrentHashMap();
....
taskMap.compute(key, (k, queue) -> {
CompletableFuture<Void> future = (queue == null)
? CompletableFuture.runAsync(myTask, poolExecutor)
: queue.whenCompleteAsync((r, e) -> myTask.run(), poolExecutor);
//to prevent OutOfMemoryError in case if we will have too much keys
future.whenComplete((r, e) -> taskMap.remove(key, future));
return future;
});
在future
已完成whenComplete
函数参数的情况下,此代码的问题在与compute
相同的线程中调用。在这个方法的主体中,我们从地图中删除条目。但是计算方法文档禁止这个和应用程序冻结。
如何解决此问题?
答案 0 :(得分:2)
最明显的解决方案是使用whenCompleteAsync
而不是whenComplete
,因为前者保证使用提供的Executor
而不是调用线程来执行操作。
Executor ex = r -> { System.out.println("job scheduled"); new Thread(r).start(); };
for(int run = 0; run<2; run++) {
boolean completed = run==0;
System.out.println("*** "+(completed? "with already completed": "with async"));
CompletableFuture<String> source = completed?
CompletableFuture.completedFuture("created in "+Thread.currentThread()):
CompletableFuture.supplyAsync(() -> {
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
return "created in "+Thread.currentThread();
}, ex);
source.thenApplyAsync(s -> s+"\nprocessed in "+Thread.currentThread(), ex)
.whenCompleteAsync((s,t) -> {
if(t!=null) t.printStackTrace(); else System.out.println(s);
System.out.println("consumed in "+Thread.currentThread());
}, ex)
.join();
}
将打印类似
的内容*** with already completed
job scheduled
job scheduled
created in Thread[main,5,main]
processed in Thread[Thread-0,5,main]
consumed in Thread[Thread-1,5,main]
*** with async
job scheduled
job scheduled
job scheduled
created in Thread[Thread-2,5,main]
processed in Thread[Thread-3,5,main]
consumed in Thread[Thread-4,5,main]
所以你可以使用
taskMap.compute(key, (k, queue) -> {
CompletableFuture<Void> future = (queue == null)
? CompletableFuture.runAsync(myTask, poolExecutor)
: queue.whenCompleteAsync((r, e) -> myTask.run(), poolExecutor);
//to prevent OutOfMemoryError in case if we will have too much keys
future.whenCompleteAsync((r, e) -> taskMap.remove(key, future), poolExecutor);
return future;
});
如果提前完成的可能性很大,您可以使用
减少开销taskMap.compute(key, (k, queue) -> {
CompletableFuture<Void> future = (queue == null)
? CompletableFuture.runAsync(myTask, poolExecutor)
: queue.whenCompleteAsync((r, e) -> myTask.run(), poolExecutor);
//to prevent OutOfMemoryError in case if we will have too much keys
if(future.isDone()) future = null;
else future.whenCompleteAsync((r, e) -> taskMap.remove(key, future), poolExecutor);
return future;
});
也许,你没有找到这个明显的解决方案,因为你不喜欢将依赖操作总是作为新任务安排到池中,即使已经在另一个任务中完成了。您可以使用专门的执行程序解决此问题,该执行程序仅在必要时重新安排任务:
Executor inPlace = Runnable::run;
Thread forbidden = Thread.currentThread();
Executor forceBackground
= r -> (Thread.currentThread()==forbidden? poolExecutor: inPlace).execute(r);
…
future.whenCompleteAsync((r, e) -> taskMap.remove(key, future), forceBackground);
但您可能会重新考虑是否真的需要这种复杂的每映射清理逻辑。它不仅复杂,而且可能会产生显着的开销,可能会安排许多清理操作,这些操作在执行时已经过时并不是真正需要的。
执行
可能更简单,效率更高taskMap.values().removeIf(CompletableFuture::isDone);
不时清理整个地图。