Fifth Zhou (three ways to create Java multithreading)

Source: Internet
Author: User

I am learning Java recently multi-threading, and I think the multi-threaded this piece in the future Java development is very important, talk about Java implementation of three ways of multithreading.

Java Multi-threaded implementation of the main three kinds: inherit the thread class, implement Runnable interface, use Executorservice, callable, the future implementation has the result of multithreading. There are no return values for the first two methods after the thread has been executed, and only the third is with the return value, which is generally more demanding and more difficult than the first two.

1, inherit the thread class to realize multithreading
Inheriting the thread class essentially also implements an instance of the Runnable interface, which represents an instance of a thread that executes the thread by invoking the start () method.

eg

Public class MyThread extends Thread {

Public void Run () {

System.out.println ("Mythread.run ()");

}

MyThread myThread1 = new MyThread ();

MyThread myThread2 = new MyThread ();

Mythread1.start ();

Mythread2.start ();

}

2, realize the Runnable interface way to realize multithreading
If your class already extends another class, you cannot directly extends the Thread, at which point you must implement a runnable interface.

Example code:

Public class MyThread extends Otherclass implements Runnable {

Public void Run () {

System.out.println ("Mythread.run ()");

}

}

public class main{

public static void Main (string[] args) {

MyThread MyThread = new MyThread ();

Thread thread = new Thread (myThread);

Thread.Start ();

}

3, the use of Executorservice, callable, the future to achieve a return result of multi-threading
Executorservice, callable, future This object is actually a function class in the executor framework. To learn more about the accessible http://www.javaeye.com/topic/366591 of the executor framework, here is a detailed explanation of the framework. The thread that returns the result is a new feature introduced in JDK1.5, and it's really practical, and with this feature I don't have to go any further to get the return value, and it can be flawed even if it's done.
A task that can return a value must implement the callable interface, and similarly, a task that has no return value must runnable the interface. After performing the callable task, you can get a future object, call get on the object to get to the callable task returned object, and combined with the thread pool interface Executorservice can realize the legend has the return result of multithreading.

Since three ways can be used to create multi-threading, and three ways to implement the principle of the first two methods of the same nature, personally, I recommend the second way, by implementing the Runnable interface implementation of multi-threading, not recommended to use the first, in terms of Java features, does not support multiple inheritance, If a few into the thread class to achieve multi-want accomplishment can not let the class in a few other classes, through the second method more can embody the Java object-oriented features, and the JVM bottom thread tired is also implemented runnable interface to achieve, the third way because it is more difficult, generally do not require too much control, But when the production environment is needed, we will have fun learning.

Fifth Zhou (three ways to create Java multithreading)

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.