In the source code of FutureTask<V>
, there is a volatile
instance variable Thread runner
, where the comment told that it is the thread running the callable. However, the runner
is never initialized in the source code. Moreover, I could not find any clue how this runner
is used to run the callable.
Question: Since the runner
is never initialized, how it is used to run the Callable
?
In the code you can see
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
at the start of the run
method. Ignoring the state != NEW
part; this attempts to set the runner
variable to the result of Thread.currentThread()
(and will succeed only if it is currently null
). Only if this succeeds (returns true
) will the run
method be able to go through the rest of the code in this block. And since the result of Thread.currentThread()
will be the Thread
which called the run
method, the documentation is accurate (at least, after this initial if
part is successfully evaluated).