Multithreading-thread, Runnable, Callbale, future

Source: Internet
Author: User

Thread :Java uses thread to represent threads, all thread objects must be the thread class or its subclasses, and you can create and start multithreading by inheriting the thread class.

Package Org.github.lujiango;public class Test10 {    static class MyThread extends Thread {        @Override        public void Run () {            System.out.println ("I am extend Thread ...");}    }    public static void Main (string[] args) {        MyThread mt = new MyThread ();        Mt.start ();    }}

  

Runnbale: You can create multiple threads by implementing the Runnable interface, create an instance of the Runnable implementation class, and use this as the target of the thread to create the thread object, which is the true thread object.

Package Org.github.lujiango;public class Test11 {    static class Myrunnable implements Runnable {        @Override        public void Run () {            System.out.println ("I am implement Runnable ...");}    }    public static void Main (string[] args) {        thread t = new Thread (new Myrunnable ());        T.start ();    }}

  

callable: Starting with Java 5, Java provides a callable interface that provides a call method that can be used as a thread execution body, but the call method can have a return value that declares an exception to be thrown. JAVA5 provides a future interface to represent the return value of the call method, and provides a Futuretask implementation class for the future, which implements the future and runnable.

Package Org.github.lujiango;import Java.util.concurrent.callable;import java.util.concurrent.ExecutionException; Import Java.util.concurrent.futuretask;public class Test12 {    static class Mycallable implements Callable<string > {        @Override public        String call () throws Exception {            return "my Callable";        }    }    public static void Main (string[] args) throws Interruptedexception, executionexception {        mycallable callable = new MyC Allable ();        futuretask<string> future = new futuretask<string> (callable);        New Thread (Future). Start ();        System.out.println (Future.get ());}    }

Three ways to create a thread comparison:

Multithreading can be implemented by inheriting the thread class or implementing the Runnable,callable interface.
Create multi-threaded with runnable,callable interface
(1) The thread class simply implements the Runnable interface or callable interface, and can inherit other classes.
(2) Multi-thread can share a target object, so suitable for multiple identical threads to handle the same resource situation, so that the CPU, code and data can be separated to form a clear model, better reflect the idea of object-oriented.
(3) programming is slightly more complex, if you need to access the current thread, you must use the Thread.CurrentThread () method
Creating multithreading with the method of inheriting the thread class
(1) Because multithreaded classes have inherited the thread class, they cannot inherit other parent classes.
(2) Write simple, if you need to access the current thread, you do not have to use the Thread.CurrentThread () method, directly use this to get the current thread.

Multithreading-thread, Runnable, Callbale, future

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.