我想要做這樣的事情:runnableFuture設置結果
在線程1:
Future<String> future = socketService.registerRequest(uuid);
System.out.println(future.get()); // I want to be blocked here while I will not signal from another thread
registerRequest:
public Future<String> registerRequest(String uuid) {
RunnableFuture<String> runnableFuture = new FutureTask<String>(); // this code doesn't compile because it there is no default constructor
results.put(uuid, runnableFuture);
return runnableFuture;
}
而在另一個線程我想說這樣的水手
results.get(uuid).setResult("It is result from another thread")
我該如何在java中實現它?
我幾乎監視到你可能想要從兩個線程訪問一個'Map' ...如果是這樣,你應該從頭說明一下。如果沒有,'ExecutorService.submit'是阻止'future.get()';-)的最簡單的答案 – Roland