use thread pool:
ExecutorService exec = Executors.newFixedThreadPool(5);
FutureTask<Integer> ft = new FutureTask<Integer>(() -> rpcMethod());
exec.submit(ft);
call run method:
FutureTask<Integer> ft = new FutureTask<Integer>(() -> rpcMethod());
ft.run()
I looked at the run method source code, as if it was a synchronous call, it seems that there is no new thread. The reason I have this problem is because I looked at the source code of Dubbo's RpcContext#asyncCall and found that it is directly using FutureTask#run.Here is some part of the code.
public <T> Future<T> asyncCall(Callable<T> callable) {
try {
try {
setAttachment(Constants.ASYNC_KEY, Boolean.TRUE.toString());
final T o = callable.call();
//local调用会直接返回结果.
if (o != null) {
FutureTask<T> f = new FutureTask<T>(new Callable<T>() {
public T call() throws Exception {
return o;
}
});
f.run();
return f;
} else {
}