Concurrent multi-threaded base of the right start

Compare start method and the run method

Many people including myself have previously thought run and start methods can start a thread, and both functions are similar, and then realize that this is wrong in the process of learning, among them are very different of. Look at a demo code:

/**
 * @author Chen
 * @Description start()和run()方法比较
 * @create 2019-11-05 22:01
 */
public class StartAndRunMethod {
    public static void main(String[] args) {
        //使用run方法
        Runnable runnable = ()-> System.out.println(Thread.currentThread().getName());
        runnable.run();

        //使用start方法
        new Thread(runnable).start();
    }
}

Output:

main
Thread-0

We can see, the thread run method is the main thread, and start execution method is a new child threads.

Interpretation of the start method

We all know that start is to start a new thread, in fact really is that during the execution of the start method involves two threads, one is the main thread, the other is a new child thread , when the start method is invoked, not the child thread will immediately start up, the child thread will get some resources in addition to the cpu, to enter the state, the CPU waits for the call . As for when to call, this is not a decision we can, but the thread scheduler to decide. So there are some slow thread starts to run the start method of thread may start method is called after than.

When what we called twice repeated start method would happen?

Will throw an exception IllegalThreadStateException (illegal thread state), in fact, a thread, if implemented, will enter the runnable state, once finished, will enter the end of the state, will not return back.

Next we take a closer look at to start()understand what the process of execution in the end it did.

  public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be 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);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

In start()the first checks the state of the thread, judgment threadStatusis not equal to 0, that judgment is not a new start thread, which is performed twice start()reason why thrown. (Online Chenggang Gang is initialized when the default state is 0)

Then made a thread group to join this behavior, and then execute a native method of a start0.

Interpretation run method

In fact, the run method is relatively simple, only three lines of code: If the target is the incoming interface is not empty, on the implementation of the run method of rewriting the content, or the content of the incoming run on the implementation of the method.

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

So when we execute the run method, it will as a general method, and write our own as well, so print out the console is a main thread.

So we want to start a new thread can not directly call run method, but should start method is called indirect call to the run method.

Guess you like

Origin www.cnblogs.com/chen88/p/11802279.html