【发布时间】:2021-09-30 07:09:06
【问题描述】:
我们有以下场景,我们有一个具有两个方法并在多个线程之间共享的类。
public class Response {
Map <String, APIResponse> requestIdToResponse = new ConcurrentHashMap();
public void sendResponse(ApiRequest apirequest) {
String requestId = apiRequest.getRequestId();
// Send async call to invoke the rest API. and populate the hashmap with results.
}
// This should be sync call. Once the async call finish
// concurrent hashmap should be populated with request id and response
public ApiResponse getAPiResponse(String requestId) {
// How to make a current thread wait for certain timeout lets say(15 min) until the response
// is available in the concurrent hashmap for given request id.
}
}
【问题讨论】:
-
你的意思是一个线程调用
getAPiResponse,应该等待另一个线程调用sendResponse?
标签: java concurrency java.util.concurrent java-threads