thread.start () 도대체 무슨 짓을 한 거야?
정답:java.Illegal Thread State Exception 스레드 상태의 불법 이상 계승 관계는 다음과 같습니다: > extends Illegal Argument Exception -> extends Runtime Exception 실행 중 이상입니다. 다음은 원본에서 start () 가 무엇을 했는지 철저하게 분석합니다.
1
/**
 2      * Causes this thread to begin execution; the Java Virtual Machine
 3      * calls the run method of this thread.
 4      *      ,JVM  run  
 5      * The result is that two threads are running concurrently: the
 6      * current thread (which returns from the call to the
 7      * start method) and the other thread (which executes its
 8      * run method).
 9      * 
10      * It is never legal to start a thread more than once.    start            
11      * In particular, a thread may not be restarted once it has completed
12      * execution.
13      *
14      * @exception  IllegalThreadStateException  if the thread was already         start,  
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)//      0:NEW     
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();//  native      run  
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();
greop.add (this), 현재 스레드를 스레드 그룹에 추가합니다. 원본 코드는 다음과 같습니다.
1
 /**
 2      * Adds the specified thread to this thread group.
 3      *
 4      *  Note: This method is called from both library code
 5      * and the Virtual Machine. It is called from VM to add
 6      * certain system threads to the system thread group.
 7      *
 8      * @param  t
 9      *         the Thread to be added
10      *
11      * @throws  IllegalThreadStateException
12      *          if the Thread group has been destroyed
13      */
14     void add(Thread t) {
15         synchronized (this) {
16             if (destroyed) {//       
17                 throw new IllegalThreadStateException();
18             }
19             if (threads == null) {
20                 threads = new Thread[4];//      4 Thread  
21             } else if (nthreads == threads.length) {//       2 
22                 threads = Arrays.copyOf(threads, nthreads * 2);
23             }
24             threads[nthreads] = t;//   t     
25 
26             // This is done last so it doesn't matter in case the
27             // thread is killed
28             nthreads++;//    1
29 
30             // The thread is now a fully fledged member of the group, even
31             // though it may, or may not, have been started yet. It will prevent
32             // the group from being destroyed so the unstarted Threads count is
33             // decremented.
34             nUnstartedThreads--;//      -1
35         }
36     }
시작 실패 후 그룹을 호출합니다.threadStartFailed(this)는 모두 잠금 방법으로 스레드 그룹에서 현재 스레드를 제거합니다. 원본 코드는 다음과 같습니다.
1 void threadStartFailed(Thread t) {
 2         synchronized(this) {
 3             remove(t);//    t
 4             nUnstartedThreads++;//     +1
 5         }
 6     }
 7 
 8 private void remove(Thread t) {
 9         synchronized (this) {
10             if (destroyed) {
11                 return;
12             }
13             for (int i = 0 ; i < nthreads ; i++) {
14                 if (threads[i] == t) {
15                     System.arraycopy(threads, i + 1, threads, i, --nthreads - i);
16                     // Zap dangling reference to the dead thread so that
17                     // the garbage collector will collect it.
18                     threads[nthreads] = null;
19                     break;
20                 }
21             }
22         }
23     }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 다중 스레드를 순차적으로 실행하는 몇 가지 방법 요약Java 다중 스레드를 순차적으로 실행하는 몇 가지 방법 요약 동료는 무심결에 이 문제를 제기하고 두 가지 방법을 직접 실천했다.물론 더 좋은 방법이 있을 거야. 방법 1 이런 방법은 비교적 흔히 볼 수 있는 해결 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.