Java Multithreading: The difference between start () and run () in thread of "basic article" 03

Source: Internet
Author: User
Tags thread thread class

Profile

The thread class contains the start () and run () methods, what are the differences? This chapter will provide an answer to this. The contents of this chapter include:

Description of the difference between start () and run ()

Example of the difference between start () and run ()

Start () and run () related source (based on jdk1.7.0_40)

Reprint please indicate the source: http://www.cnblogs.com/skywang12345/p/3479083.html

Description of the difference between start () and run ()

Start (): Its function is to start a new thread, the new thread will execute the corresponding run () method. Start () cannot be called repeatedly.

Run (): Run (), like a normal member method, can be called repeatedly. Calling run () alone executes run () in the current thread and does not start a new thread!

The following is explained in code.

Class Mythread extends thread{public  
    void Run () {
        ...}} 
;
Mythread mythread = new Mythread ();

Mythread.start () starts a new thread and runs the Run () method in the new thread.

Mythread.run () runs the Run () method directly in the current thread and does not start a new thread running run ().

Example of the difference between start () and run ()

Below, a simple example demonstrates the difference between them. The source code is as follows:

Demo.java source
class Mythread extends thread{public  
    mythread (String name) {
        super (name);
    }
    
    public void Run () {
        System.out.println (Thread.CurrentThread (). GetName () + ' is running ');
    } 
; 
    
public class Demo {public  
    static void Main (string[] args) {  
        Thread mythread=new mythread ("Mythread");
    
        System.out.println (Thread.CurrentThread (). GetName () + "Call Mythread.run ()");
        Mythread.run ();
    
        System.out.println (Thread.CurrentThread (). GetName () + "Call Mythread.start ()");
        Mythread.start ();
    }  

Run Result:

Main call Mythread.run ()

Main is running

Main call Mythread.start ()

Mythread is running

Results show:

() Thread.CurrentThread (). GetName () is the name used to get the "current thread". The current thread is the thread that is dispatching execution in the CPU.

Mythread.run () is called in main thread main, and the Run () method runs directly on the main thread main.

() Mythread.start () starts thread Mythread, the Run () method is invoked when thread mythread is started, and the Run () method is run on thread mythread.

Start () and run () related source (based on jdk1.7.0_40)

The source code for the start () method in Thread.java is as follows:

Public synchronized void Start () {
    //If the thread is not in ready state, throw an exception!
    if (threadstatus!= 0)
        throw new Illegalthreadstateexception ();
    
    Add a thread to the Threadgroup
    Group.add (this);
    
    Boolean started = false;
    try {
        //start the Thread start0 () by start0 ()
        ;
        Set started tag
        started = true;
    } finally {
        try {
            if (!started) {
                group.threadstartfailed (this );
            }
        } catch (Throwable ignore) {
        }
    }
}

Description: Start () actually starts the thread through the local method Start0 (). Start0 () will run a new thread, and the new thread will invoke the run () method.

Private native void Start0 ();

The code for Run () in Thread.java is as follows:

public void Run () {
    if (target!= null) {
        target.run ();
    }
}

Description: Target is a runnable object. Run () is the run () method that invokes the runnable member of the thread thread directly, and does not create a new thread.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Related Article

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.