Multithreading for callable and future
The structure type that the future obtains and the type of results returned by the callable must be consistent.
Callable to be submitted using the executorservice Submit method, the returned Future Object can cancel a task
1. submit a task and get an example of the return result of the task
threadPool  =  executors. newsinglethreadexecutor ();
future<string> Future = ThreadPool . Submit (new callable<string> () {
@Override
Public String call () throws Exception {
// TODO auto-generated Method Stub
return "Hello";
}
});
Future . get ();
2.Completionservice is used to submit a set of callable tasks whose take method returns a completed callable task corresponding to the
Future objects. It is one thing to get multiple threads to execute, and whoever executes first will get the results. Examples are as follows:
executorservice threadPool2 = executors. Newfixedthreadpool (ten);
completionservice<string> Completionservice = New executorcompletionservice<string>
(threadPool2);
for int i i i
Completionservice . Submit (new callable<string> () {
@Override
Public String call () throws Exception {
return "Hello";
}
});
}
for int i i i
Completionservice . Take (). get ();
}
This article from "No Worry" blog, declined reproduced!
Multithreading for callable and future