There are several ways to implement multithreading in Java and how to implement multithreading

Source: Internet
Author: User

Multithreading is a major feature of the Java language that distinguishes it from other languages. Other languages to C + + if you want to implement multithreading, you must invoke the multithreading mechanism of the operating system (I'm not sure I remember right

the interested in their own online check).

There are two ways to implement multithreading before Java5 (inheriting the thread class and implementing the Runnable interface)

Each of them is:

Use the new thread () and new Thread (Runnable) form

The first is to call the thread 's run method directly, so we tend to use the thread subclass, the new Subthread ().

The second kind of invocation

Runnable The run method.

The first type:

New Thread () {}.start (); This means that the run method of the thread subclass object is called, and new Thread () {} represents an instance object of an anonymous subclass of thread, with the following code for the subclass plus the Run method:

New Thread () {

public void Run () {

}

}.start ();

The second type:

New Thread (

New Runnable () {}

). Start ();

This means that the run method of the Runnable object accepted by the thread object is called, and new Runnable () {} represents an instance object of an Runnable anonymous subclass.

The code for the Runnable subclass plus the Run method is as follows:

New Thread (New Runnable () {

public void Run () {

}

}

). Start ();

Starting with Java5, there are a number of thread pools that create multithreading:

Package Com.yonyou.test;import java.util.concurrent.executorservice;import java.util.concurrent.executors;/** * Test class * @author Little Hao * @ Created date 2015-3-2 */public class Test{public static void Main (string[] args) {Executorservice pool = executors. Newfixedthreadpool (3); for (int. i=0;i<10;i++) {Pool.execute (new Runnable () {public void run () {System.out.println ()}});} Executors.newcachedthreadpool (). Execute (new Runnable () {public void Run () {}}); Executors.newsinglethreadexecutor (). Execute (new Runnable () {public void Run () {}});}

  

There are several ways to implement multithreading in Java and how to implement 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.