JAVA multithreading implementation of three ways (inheriting the thread class, implementing the Runnable interface, using Executorservice, callable, future implementation has the return of the results of multithreading) __java

Source: Internet
Author: User
Tags instance method thread class

Java Multithreading Implementation method

Java multithreading implementation methods mainly have three kinds: inherit thread class, implement Runnable interface, use Executorservice, callable, future to achieve the return of the results of multithreading. In the first two ways, the thread does not return a value after execution, and only the last one is with the return value.


1, inheritance thread class to achieve multithreading
Methods that inherit the thread class although I am listed as a multithreaded implementation, thread is essentially an instance of the Runnable interface, which represents an instance of a thread, and the only way to start a thread is through the thread class's start () instance method. The start () method is a native method that starts a new thread and executes the run () method. This approach to multithreading is simple, by using your own class to extend thread directly, and by copying the run () method, you can start a new thread and execute your own defined run () method. For example:
[Java]View plain copy public class Mythread extends Thread {public void run () {System.out.println ("Mythread.run ()"); Start the thread in the appropriate place as follows:
[Java]View plain copy mythread myThread1 = new Mythread ();   Mythread myThread2 = new Mythread ();   Mythread1.start (); Mythread2.start ();
2, implement runnable interface mode to achieve multithreading
If your class is already extends another class, you cannot extends Thread directly, at which point you must implement a runnable interface, as follows:
[Java]View plain copy public class Mythread extends Otherclass implements Runnable {public void run () {System.out.pri     Ntln ("Mythread.run ()"); To start mythread, you need to instantiate a thread first and pass in your own Mythread instance:
[Java]View plain copy mythread mythread = new Mythread ();   Thread thread = new Thread (mythread);   Thread.Start (); In fact, when a runnable target parameter is passed to thread, the thread's Run () method invokes Target.run (), referencing the JDK source code:
[Java]View plain copy public void Run () {if (target!= null) {Target.run (); }   }
3, using Executorservice, callable, future to achieve a return of the results of multithreading
Executorservice, callable, future This object is actually a functional class within the executor framework. To get a detailed understanding of the executor framework's accessible http://www.javaeye.com/topic/366591, the framework is explained in detail here. The thread that returns the result is a new feature introduced in the JDK1.5, which is really practical, and with this feature I don't have to bother to get a return value, and even if it does, it can be a lot of holes.
A task that can return a value must implement the callable interface, and similarly, a task with no return value must runnable the interface. After performing the callable task, you can get a future object that invokes get on the object to obtain the object returned by the callable task, and then combine the thread pool interface Executorservice to achieve the legendary return of the results of multithreading. The following provides a complete example of a multithreaded test with return results, which can be used directly under JDK1.5. The code is as follows:

[Java] view plain copy import java.util.concurrent.*;   Import Java.util.Date;   Import java.util.List;      Import java.util.ArrayList; /** * Thread with return value/@SuppressWarnings ("Unchecked") public class Test {public static void main (string[] args) throws Executionexception,

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.