start thread analytical method

Thread is learning first class we learn about multi-threaded multi-threaded come into contact with, I believe that every studied or understood the Java multi-threaded small partners are aware of the Thread class. The major share of the start of the methods Thread to explain.

I believe we all know, start by starting a thread, and that thread into an executable state. In the actual coding, we rewrite the run () method, call the start () method to start a thread, then the run () and start () method What link? Here we talk about in detail.

First, source code analysis

First, we start to see the source code, as follows:

/**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the <code>run</code> method of this thread.
     * <p>
     * The result is that two threads are running concurrently: the
     * current thread (which returns from the call to the
     * <code>start</code> method) and the other thread (which executes its
     * <code>run</code> method).
     * <p>
     * It is never legal to start a thread more than once.
     * In particular, a thread may not be restarted once it has completed
     * execution.
     *
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        #run()
     * @see        #stop()
     */
    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 */
            }
        }
    }

After the start () method is very simple, if we look at the top is separate from the above source not see any clues, but we can see Thread is created, threadStatus this internal property value is 0. In the above source, the core part is start0 (); this local method, that is, JNI approach we often see in other post, so-called JNI method is actually not so big, in fact, JNI is the Java platform and for native C code interoperability API, JNI does not support any mechanism for mapping between Java classes and C ++ classes, where one simply mention. Why start0 (); it is part of the core? Look:

 So we rewrite the run method is invoked when it? See comments can start method, as shown below:

Causes this thread to begin execution; the Java Virtual Machine
calls the <code>run</code> method of this thread.

This comment is to say: at the beginning of the implementation of this thread, JVM will call the run method, that is to be run start0 called JNI method.

By Source summarize key points:

  1.  The NEW state is constructed Thread, threadStatus this internal attribute value is 0;

  2. You can not start twice Thread, otherwise there will be IllegalThreadStateException abnormal;

  3. group.threadStartFailed ( the this); the thread can be seen after a start will be added to the ThreadGroup;

  4. A thread life cycle to the TERMINATED state, is not allowed to call the start method.

Summary: jdk designed to start and run, in fact, is a typical template model, which is the parent structure of the code written algorithm, to achieve specific logic to complete by subclasses.

  Get real-time information, public concern number: "Programming Art", a two-dimensional code:

 

Guess you like

Origin www.cnblogs.com/winqi/p/11403013.html