Use of Java Multi-threading

Source: Internet
Author: User

Java has two ways of implementing multithreading.

The first-- inherit the thread class and override the Run method

Steps:

    1. The definition class inherits the thread class;
    2. Rewrite the subclass's Run method to write the code that the thread needs to execute in the Run method;
    3. Creates a child class object, then creates a thread object;
    4. Call the object's Start method to open the thread.

Instance:

 Public class extends thread () {    @Override    publicvoid  run () {        //  multithreaded code      }    //  other code }
New MyThread (); T.start ();

The second kind- implements the Runnable interface, constructs the thread class with the object of the class

Steps:

    1. Define class implementation runnable interface;
    2. Implement the Run method in the interface, write the code that the thread needs to execute in the Run method;
    3. Constructs a thread object with an interface object;
    4. Call the Start method of the thread object to open the thread.

Instance:

 Public class Implements Runnable () {    publicvoid  run () {        //  multithreaded code     }    //  other code }
Newnew  Thread (m); T.start ();

Function of the Start method

    1. Thread was started
    2. Let the JVM invoke the Run method of the thread class (or subclass) object

Why are there two ways to implement multithreading?

It's easy to understand the first way. Since the run method of the thread class is overridden, the JVM invokes the Run method that the subclass overrides when the Start method is called.

For the second way, when the Start method is called, the JVM invokes the Run method of the thread class with the following code:

 Public void run () {    ifnull) {        target.run ();    }?}

Where target is the member variable of the thread class, and the type is runnable. When you construct a thread with a Runnable object, the target references that interface object, so when you execute Target.run (), the Run method of the interface object is actually executed.

Use of Java Multi-threading

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.