Multithreading-thread,runnable,callable,future,runnablefuture,futuretask

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.