The thread pool is implemented in cooperation with future, but the main request thread is blocked. High concurrency will still cause too many threads. CPU context switching. Through future, n requests can be sent concurrently, and then wait for the slowest return. The total response time is the time returned by the slowest request
public class Test{
final static ExecutorService executor = Executors.newFixedThreadPool(2);
public static void main(String[] args){
RpcService rpcService = new RpcService();
HttpService httpService = new HttpService();
Future<Map<String,String>> future1 = null;
Future<Integer> future2 = null;
try{
future1 = executor.submit(()->rpcService.getRpcResult());
future2 = executor.submit(()->httpService.getHttpResult());
}catch(Exception e){
if(future1 != null){
future1.cancel(true);
}
if(future2 != null){
future2.cancel(true);
}
throw new RuntimeEException(e);
}
}
static class RpcService{
Map<String,String> gerRpcResult() throws Exception{
//Calling remote methods, taking 10ms
}
}
static class HttpService{
Integer getHttpResult() throws Exception{
//Calling remote methods, taking 30ms
}
}
}
Read More:
- Python parallel processing makes full use of CPU to realize acceleration
- Error domain = nsurlerrordomain code = – 1001 “request timeout occurred in swift alamofire get request. ” UserInfo={NSUnderlyingErro
- An error occurred while processing your request
- Request processing failed;nested exception is java.lang.*
- Processing method of HTTP 411 error code
- Magento prompts “there has been an error processing your request”
- IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
- errorCode: 500, msg: , result: {“Message”:”There was an error processing the request
- There has been an error processing your request magento
- In the HTML page request Ajax times 400 error, solve Yii submit post form 400 error, and Ajax post request 400 problem (example code)
- WebView loadrequest request request error “nsurlconnection finished with error – Code – 1022”
- What about ibtool failed with exit code 255? processing method
- Flask Request an extension before_request after_request errorhandler
- From in Python__ future__ The role of import *
- Error: request failed with status code 500
- Problems in the construction of CUDA environment (GPU parallel programming)
- Design of MQ asynchronous collection report data
- NuxtSe rverError:Request failed With status code 500 my solution and thinking
- TypeError: An asyncio.Future, a coroutine or an awaitable is
- Error in v-on handler (Promise/async): “Error: Request failed with status code 500“