IllegalThreadStateException出现在程序内容之前

时间:2018-02-28 05:01:10

标签: java multithreading exception concurrency

我想知道为什么线程异常出现在实际程序的内容之前。据我所知,线程异常应该在程序的内容之后。

Output from Simple Thread Program

import java.lang.*;
public class temp extends Thread {
  private String Name; 
  private int N; 
  temp(String aIn, int aN){
      this.Name = aIn;
      this.N = aN;
  }
  public void run(){
    int lN = this.N;
    String lName = this.Name;
    for (int i = 0; i < lN; i++){
        System.out.println(String.format("Thread %s, i: %d", lName, i));
    }
  }
  public static void main(String args[]){
    temp ThreadA = new temp("A", 10); ThreadA.start();
    temp ThreadB = new temp("B", 10); ThreadB.start();
    ThreadA.start();
  }
}

2 个答案:

答案 0 :(得分:0)

它取决于VM到VM。检查变化请尝试一些时间,例如:10。它可能介于两者之间:)

答案 1 :(得分:0)

IllegalThreadStateException的第二次调用中抛出ThreadA.start();,但之前可能会显示堆栈跟踪,因为异常是通过与System.err不同的System.out流输出的。 System.err输出设计为及时显示输出,而System.out只是常规输出流。

相关问题