package day11; class TestDemo extends Thread{ int count = 0; /*public void add(){ while(count<100){ count++; System.out.println(count); } }*/ public void run(){ while(count<100){ count++; System.out.println(count); } } } public class ThreadDemo { public static void main(String[] args) { TestDemo td = new TestDemo(); TestDemo td2 = new TestDemo(); //td.add(); /*td.start(); td.start(); java.lang.IllegalThreadStateException異常原因解析: 同一個Thread不能重復調用start方法,因為重復調用了,start方法,status就不是new了,那么threadStatus就不等於0了。 那么就會拋出new IllegalThreadStateException()異常, 此異常在方法中拋出的,那么誰調用此方法,一旦不滿足要求,調用此方法就會拋出異常。 源代碼: 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". 意思就是:start方法沒有被系統的主方法線程或者是被虛擬機創建的系統團體線程喚醒。 以后任何新的功能添加此方法,不得不把此功能添加都Vm中。 0這個狀態,等於‘New’這個狀態。 if (threadStatus != 0) throw new IllegalThreadStateException(); group.add(this); start0(); if (stopBeforeStart) { stop0(throwableFromStop); } } */ } }