The difference between start () and run () of the Java Multithreaded Series Foundation (III)

Source: Internet
Author: User

Overview

The thread class contains the start () and run () methods, what is the difference between them? This will be answered in this chapter.

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 MyThread extends thread{public      void Run () {        ...    }}; MyThread MyThread = 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:

1//Demo.java Source 2 class MyThread extends thread{   3 public     MyThread (String name) {4         super (name); 5     } 6< C5/>7 public     Void Run () {8         System.out.println (Thread.CurrentThread (). GetName () + ' is running '); 9     } 10}; 11 public class Demo {$  public     static void Main (string[] args) {         mythread=new Thread mythread ("my Thread ");         System.out.println (Thread.CurrentThread (). GetName () +" Call Mythread.run () ");         Mythread.run ();         System.out.println (Thread.CurrentThread () getName () + "Call Mythread.start ()");         Mythread.start ();  22}

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 (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 "ready", an exception is thrown!)    if (threadstatus! = 0)        throw new Illegalthreadstateexception ();    Add a thread to the Threadgroup    Group.add (this);    Boolean started = false;    try {        //start thread start0 () through 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 call 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 calls the runnable member of the thread thread directly, and does not create a new thread.

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

The difference between start () and run () of the Java Multithreaded Series Foundation (III)

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.