The difference between start () and run () in the Java multithreaded 3:thread

Source: Internet
Author: User

Original: http://www.cnblogs.com/skywang12345/p/3479083.html

Description of the difference between start () and run ()
Start (): Its role is to start a new thread, and the new thread executes the corresponding run () method. Start () cannot be called repeatedly.
Run (): Run () is the same as the normal member method and can be called repeatedly. Calling run () alone will execute run () in the current thread and will not start a new thread!

The following is illustrated in code.

class extends thread{      publicvoid Run ( ) {...     New MyThread ();

Mythread.start () starts a new thread and runs the Run () method in a 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 shows the difference between them. The source code is as follows:

classMyThreadextendsthread{ PublicMyThread (String name) {Super(name); }     Public voidrun () {System.out.println (Thread.CurrentThread (). GetName ( )+ "is running"); }} Public classDemo { Public Static voidMain (string[] args) {Thread myThread=NewMyThread ("MyThread"); System.out.println (Thread.CurrentThread (). GetName ()+ "Call Mythread.run ()");        Mythread.run (); System.out.println (Thread.CurrentThread (). GetName ()+ "Call Mythread.start ()");    Mythread.start (); }}

Operation Result:

Main call Mythread.run () main was Runningmain call Mythread.start () Mythread is running

Result Description :
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 the main thread main, and the Run () method runs directly on the main thread main.
Mythread.start () will start "Thread Mythread", and "thread Mythread" will invoke the run () method after it is started, and the Run () method at this time is run on thread mythread.

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

/*** Causes this thread to begin execution; the Java Vsan * calls the Run method of this thread. * It's Neve R legal to start a thread more than once. * In particular, a thread is not being restarted once it has completed * execution.*/ Public synchronized voidstart () {/*** This method isn't invoked for the main method thread or ' system ' * Group threads Created/set up by the VM.     Any of the new functionality added * to this method in the future May has to also is added to the VM.     * * A Zero status value corresponds to state "NEW". */    if(Threadstatus! = 0)        Throw Newillegalthreadstateexception (); /*Notify the group that this thread was about to being started * so that it can be added to the group's list of threads * and the group ' s unstarted count can be decremented. */Group.add ( This); Booleanstarted =false; Try{start0 (); Started=true; } finally {        Try {            if(!started) {group.threadstartfailed ( This); }        } Catch(Throwable ignore) {/*Do nothing . If Start0 threw a throwable then it 'll be is passed the call stack*/            }        }    }Private native voidStart0 ();

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

Private native void start0 ();

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

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

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

The difference between start () and run () in Java multithreaded 3:thread

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.