From the Thread.Start () method to see the thread source code, multiple start a thread will do

Source: Internet
Author: User
Tags thread class throwable

This article as the thread class source analysis of the supplement, from a side view thread source code. Also answer the interview high-frequency question: "How many start a thread?" ”

The answer is: java.lang.IllegalThreadStateException thread state Illegal exception inheritance relationship is:--->extends illegalargumentexception--->extends RuntimeException a run-time exception, let's take a thorough look at start () from the source.

1 /**2 * Causes this thread to begin execution; the Java Virtual machine3 * Calls the <code>run</code> method of this thread.4 * <p> thread is executed, JVM calls the Run method5 * The result is this and the threads is running concurrently:the6 * Current thread (which returns the7 * <code>start</code> method) and the other thread (which executes its8 * <code>run</code> method).9 * <p>Ten * It's never legal to start a thread more than once. Multiple calls to start method starting a thread is illegal One * In particular, a thread is not being restarted once it has completed A * execution. -      * -      * @exceptionIllegalthreadstateexception If the thread was already start again, the exception the * started. -      * @see#run () -      * @see#stop () -      */ +      Public synchronized voidstart () { -         /** + * This method isn't invoked for the main method thread or "system" A * Group Threads Created/set up by the VM. Any new functionality added at * To the The also is added to the VM. -          * - * A Zero status value corresponds to state "NEW". -          */ -         if(Threadstatus! = 0)//Status check 0:new new state  -             Throw Newillegalthreadstateexception (); in  -         /*Notify The group , the This thread is an about-to-be started to * So, it can be added to the group ' s list of threads + * and the group ' s unstarted count can be decremented.*/ -Group.add ( This);// add into thread group  the  *         Booleanstarted =false; $         Try {Panax Notoginseng start0 ();//Call the native method to execute the thread run method -started =true; the}finally { +             Try { A                 if(!started) { theGroup.threadstartfailed ( This);// failed to start, removing the current prospect from the thread group.  +                 } -}Catch(Throwable ignore) { $                 /*Do nothing . If Start0 threw a throwable then $ It 'll be passed up the call stack*/ -             } -         } the     } - Wuyi     Private native voidStart0 ();

Greop.add (this), add the current thread into the thread group, the source code is as follows:

1 /**2 * Adds The specified thread to this thread group.3      *4 * <p> Note:this method is called from both library code5 * and the Virtual machine. It is called from VMs to add6 * Certain system threads to the system thread group.7      *8      * @paramT9 * The Thread to be addedTen      * One      * @throwsillegalthreadstateexception A * If the Thread group has been destroyed -      */ -     voidAdd (Thread t) { the         synchronized( This) { -             if(destroyed) {//thread group State check -                 Throw Newillegalthreadstateexception (); -             } +             if(Threads = =NULL) { -Threads =NewThread[4];//Initializes a thread array of length 4 +}Else if(Nthreads = =threads.length) {// array expands twice times when full  AThreads = arrays.copyof (Threads, Nthreads * 2); at             } -Threads[nthreads] =t;// new Thread T added into array  -  -             //This was done by the last so it doesn ' t matter in case the -             //thread is killed -nthreads++;// number of threads plus 1  in  -             //The thread is now a fully fledged member of the group, even to             //though it may, or could not, has been started yet. It'll prevent +             //The group from being destroyed so the unstarted Threads count is -             //decremented. thenunstartedthreads--;// number of threads not started-1  *         } $}

Call Group.threadstartfailed (this) after a failed start,is the lock method to remove the current thread from the thread group, the source code is as follows

1 voidthreadstartfailed (Thread t) {2         synchronized( This) {3 Remove (t);//Remove thread T4nunstartedthreads++;//thread +1 not started5         }6     }7 8 Private voidRemove (Thread t) {9         synchronized( This) {Ten             if(destroyed) { One                 return; A             } -              for(inti = 0; i < nthreads; i++) { -                 if(Threads[i] = =t) { theSystem.arraycopy (threads, i + 1, threads, I,--nthreads-i); -                     //Zap dangling reference to the dead thread so -                     //The garbage collector would collect it. -Threads[nthreads] =NULL; +                      Break; -                 } +             } A         } at}

From the Thread.Start () method to see the thread source code, multiple start a thread will do

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.