Thread启动方式一(Thread.start):源码分析

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);
	  }
    }
		 
		*/
		
	}

}

  

原文地址:https://www.cnblogs.com/childhooding/p/4626457.html