Class Diagram:
First look at the respective source code:
Public interface Runnable {public abstract void run ();
Publicclass Thread implements Runnable {/* What'll be run. */ private Runnable target;}
Thread and runnable are actually an adorner pattern.
Public interface Callable<v> { V call () throws Exception;}
Public interface Future<v> { Boolean cancel (Boolean mayinterruptifrunning); Boolean iscancelled (); Boolean isDone (); V get () throws Interruptedexception, Executionexception; V get (long timeout, timeunit unit) throws Interruptedexception, Executionexception, TimeoutException;}
Public interface runnablefuture<v> extends Runnable, future<v> { void run ();
public class Futuretask<v> implements runnablefuture<v> {/** the underlying callable; nulled out after Runnin G */private callable<v> callable; /** the result to return or exception to throw from Get () */private Object outcome; public void Run () {if ' state! = NEW | | ! Unsafe.compareandswapobject (this, runneroffset, NULL, Thread.CurrentThread ())) Return try {callable<v> c = callable; if (c! = null && state = = NEW) {V result; Boolean ran; try {result = C.call (); ran = true; } catch (Throwable ex) {result = null; ran = false; SetException (ex); } if (ran) set (result); }} finally {//runner must be non-null until STATe is settled to//prevent concurrent calls to run () runner = null; State must is re-read after nulling runner to prevent//leaked interrupts int s = State; if (s >= interrupting) handlepossiblecancellationinterrupt (s); } }}
Judging from the structure of the class:
Runnable,callable,future interface is independent of each other, there is no direct relationship.
The thread's series of constructors requires a Runnable object, so the callable object is not suitable for the thread constructor, but rather the class with the Futuretask, which implements the class with the future and runnable interface capabilities, Combining the callable object at the same time, Futuretask calls the call () method of callable in the Run () method, which is a typical adapter pattern:
Runable is a target interface that defines the interface required for thread;
Callable is an interface that has been adapted to define an existing interface method call ()
Futuretask is an adapter class that, as a converter, runnable and callable, the adapter class is the core of the adapter pattern, and it makes a relationship by implementing Runnablefuture and combining callable.
Multithreading-thread,runnable,callable,future,runnablefuture,futuretask