zzh

zzh

ThreadPoolExecutor callback function Future

This text only explores what happens when a future callback function is generated after submitting a task in ThreadPoolExecutor.

As shown in the above figure, after submitting, it first enters this code segment, where two tasks are mainly executed: creating a RunnableFuture object and starting a thread to execute the RunnableFuture.

Next, let's go to the first line of the core code, as shown below, a FutureTask object is created.

image

image

The most core member variables of the FutureTask object are as follows:

image

Next, let's go to the second line of the core code, which is essentially to start a thread and execute:

image

Then we consider how Java uses future.get() and future.cancel() to start and cancel a task.
First, let's look at the future.get() method:

image

image

So where does the outcome come from? Of course, it comes from the run method in FutureTask, which assigns the result of the task completion to the outcome member variable.
As for the future.cancel() method, it sets the thread to an interrupted state to cancel the task:

image

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.