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

      

  • 相关阅读:
    Java 学习使用常见的开源连接池
    Java 数据库操作
    Java 集合的简单理解
    windows中在vs code终端使用bash
    敏捷开发、DevOps相关书籍——书单
    使用Dockerfile来构建镜像
    Redis集群搭建
    使用redis限制ip访问次数
    NFS服务器搭建
    ssh 中 远程文件传输
  • 原文地址:https://www.cnblogs.com/childhooding/p/4626457.html
Copyright © 2020-2023  润新知