问题
new Thread(new Runnable() {
public void run() {
.............
.............
.............
}
}).start();
If i will do this in main it will create a new thread and will submit a task to it for asynchronous calculation.
If you see FutureTask documentation it also says:
A cancellable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation.
So how FutureTask
is an asynchronous computation
does it create thread internally and submit the task that we give it at the time of instantiating FutureTask
like:
FutureTask f = new FutureTask(new MyCallable());
Otherwise it can't be an asynchronous computation , please provide me the code snippet from the FutureTask
source code where it submits the task to thread, to make it asynchronous computation. Thanks.
I got the answer. It's is basically trying to run the task in the same thread as that of caller. It is pretty evident in the given code:
When you call futureTask.run()
it just calls sync.innerRun();
and sync
is the instance of inner class Sync
. In that it just calls call()
on the callable object in the same thread.
void innerRun() {
if (!compareAndSetState(READY, RUNNING))
return;
runner = Thread.currentThread(); //here it is getting the current thread
if (getState() == RUNNING) {
V result;
try {
result = callable.call();//here calling call which executes in the caller thread.
} catch (Throwable ex) {
setException(ex);
return;
}
set(result);
} else {
releaseShared(0); // cancel
}
}
回答1:
So how FutureTask is an asynchronous computation does it create thread internally and submit the task that we give it at the time of instantiating FutureTask like:
FutureTask
is not designed to be used directly by the user. It is designed to be used through the ExecutorService
interface and the classes that implement it. It is those classes that use FutureTask
and fork the threads, etc.. You may need to read more information about how to use the ExecutorService concurrency classes.
The ThreadPoolExecutor
class is the main one that actually does the management of the threads in the pool. Typically you call Executors.newCachedThreadPool()
or Executors.newFixedThreadPool(10)
to get an instance of it.
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// define your jobs somehow
for (MyCallable job : jobsToDo) {
// under the covers this creates a FutureTask instance
Future future = threadPool.submit(job);
// save the future if necessary in a collection or something
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
// now we can go back and call `future.get()` to get the results from our jobs
From an academic standpoint, under the covers TPE extends AbstractExecutorService
and it is there that you can see the FutureTask
class being used to manage the tasks in the thread-pool:
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
...
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTask<T>(callable);
}
The code inside of TPE is pretty complicated and it's not easy to show a "snippet" that performs the asynchronous calls. The TPE sees if it needs to add more threads to the pool. submits it to a task queue which can either reject it or accept it, and then the threads dequeue the task and run them in the background.
来源:https://stackoverflow.com/questions/19768540/how-futuretask-is-asynchronous-computation