Thread 中的start()

 1 /**
 2      * Causes this thread to begin execution; the Java Virtual Machine
 3      * calls the <code>run</code> method of this thread.
 4      * <p>
 5      * The result is that two threads are running concurrently: the
 6      * current thread (which returns from the call to the
 7      * <code>start</code> method) and the other thread (which executes its
 8      * <code>run</code> method).
 9      * <p>
10      * It is never legal to start a thread more than once.
11      * In particular, a thread may not be restarted once it has completed
12      * execution.
13      *
14      * @exception  IllegalThreadStateException  if the thread was already
15      *               started.
16      * @see        #run()
17      * @see        #stop()
18      */
19     public synchronized void start() {
20         /**
21          * This method is not invoked for the main method thread or "system"
22          * group threads created/set up by the VM. Any new functionality added
23          * to this method in the future may have to also be added to the VM.
24          *
25          * A zero status value corresponds to state "NEW".
26          */
27         if (threadStatus != 0)
28             throw new IllegalThreadStateException();
29 
30         /* Notify the group that this thread is about to be started
31          * so that it can be added to the group's list of threads
32          * and the group's unstarted count can be decremented. */
33         group.add(this);
34 
35         boolean started = false;
36         try {
37             start0();
38             started = true;
39         } finally {
40             try {
41                 if (!started) {
42                     group.threadStartFailed(this);
43                 }
44             } catch (Throwable ignore) {
45                 /* do nothing. If start0 threw a Throwable then
46                   it will be passed up the call stack */
47             }
48         }
49     }
50 
51     private native void start0();

从jdk的官方文档可以看出一些关于start方法的一些信息:

  1.当一个thread被new出来后,线程的内部属性threadStatus==0;

  2.new thread之后,都会重写run方法实现业务,纵观start方法中,只包含了一个native方法,从文档中看出   

        /* Causes this thread to begin execution; the Java Virtual Machine calls the <code>run</code> method of this thread   */ 猜测推断出,native方法statr0调用了重写的run方法。

  3.当一个线程被start之后,再次start的时候,会抛出 IllegalThreadStateException 异常。

  4.  /*  In particular, a thread may not be restarted once it has complete execution. */   当一个线程结束他的生命周期之后,是不能再次调用start的。

  5.当线程start之后,会被加入到一个threadGroup中,最后会根据started标识位,判断是否移除此线程。

 
 

猜你喜欢

转载自www.cnblogs.com/fengyue0520/p/10421828.html