The difference between start of and run of in threads in the Java basic tutorial

  • 2020-04-01 02:44:25
  • OfStack

The Thread class contains the start() and run() methods. What is the difference? This chapter will answer this question. This chapter includes:
Note the difference between start() and run()
Example of the difference between start() and run()
Start () and run() related source code (based on jdk1.7.0_40)

Note the difference between start() and run()
Start () : this starts a new thread that executes the run() method. Start () cannot be called repeatedly.
The run ()     : run() can be called repeatedly just like a regular member method. A separate call to run() executes run() in the current thread without starting 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 the new thread.
Mythread.run (), on the other hand, runs the run() method directly in the current thread and does not start a new thread to run run().

 

Example of the difference between start() and run()
Here's a simple example to illustrate the difference. Source as follows:


public synchronized void start() {
    //If the thread is not "ready", an exception is thrown!
    if (threadStatus != 0)
        throw new IllegalThreadStateException();
    //Adds a thread to a ThreadGroup
    group.add(this);
    boolean started = false;
    try {
        //Start the thread with start0()
        start0();
        //Set the started flag
        started = true;
    } finally {
        try {
            if (!started) {
                group.threadStartFailed(this);
            }
        } catch (Throwable ignore) {
        }
    }
}

Operation results:


main call mythread.run()
main is running
main call mythread.start()
mythread is running

Results:
(01) thread.currentthread ().getname () is the name used to get the "currentThread". The current thread is the thread that is being scheduled for execution in the CPU.
(02) mythread.run() is called in the "main thread" method, which runs directly on the "main thread".
(03) mythread.start() will start "thread". After "thread" starts, it will call the run() method. The run() method at this point is running on "thread mythread".

 

Start () and run() related source code (based on jdk1.7.0_40)
The source code of the start() method in thread.java is as follows:

The same code at the page code block index 1

Note: start() actually starts the thread with the local method start0(). Start0 () runs a new thread, which calls 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 directly calls the Runnable member of the Thread without creating a new Thread.


Related articles: