zoukankan      html  css  js  c++  java
  • java多线程执行问题

    class Demo extends Thread{
        public Demo(String name){
    	    super(name);
    	}
    	 
    	public void run(){
    	    for(int i=0; i<6; ++i){
    		    System.out.println("i = " + i + "......Thread=" + Thread.currentThread().getName());
    			try{
    			    Thread.sleep(100);
    			}catch(InterruptedException e){
    			    System.out.println("进程被打断!");
    			}
    	    }
    	}
    }
    
    
    public class Test{
       public static void main(String[] args) throws InterruptedException{
             
    	    Demo d1 = new Demo("hjz");
    		Demo d2 = new Demo("chb");
    		d1.start();
    		//另外当某一个线程因为异常而终止,其他的线程照样执行,不会受到任何影响!
    		System.out.println(5/0);//throw new ArithmeticException()
    		for(int i=0; i<6; ++i){
    		    System.out.println("i = " + i + "......Thread=" + Thread.currentThread().getName());
    			Thread.sleep(200);
    		}
    		
    		
    		d2.start();//如果开启新线程之前,就因为异常而中止了线程,那么新线程将无法开启!
       }
    }
     
    /*  
     class Demo extends Thread{
        public Demo(String name){
    	    super(name);
    	}
    	//public Thread(String name) {
        //    init(null, null, name, 0);
        //} 也就是在创建线程对象的时候,通过构造函数该线程就有了名字了!
    	public void run(){
    	    for(int i=0; i<6; ++i){
    		    System.out.println("i = " + i + "......Thread=" + getName());
    			try{
    			    Thread.sleep(100);
    			}catch(InterruptedException e){
    			    System.out.println("进程被打断!");
    			}
    	    }
    	}
    }
    
    public class Test{
       public static void main(String[] args) throws InterruptedException{
    	    Demo d1 = new Demo("hjz");
    		Demo d2 = new Demo("chb");
    		
    		d1.run();
    		d2.run();	
       }
    }
     */
    

      

  • 相关阅读:
    汇编-MOV指令
    mfc 线程的优先级
    golang图片裁剪和缩略图生成
    libnsq编译、使用记录
    c日志宏
    golang 六宫格、九宫格头像生成
    使用httputil中ReverseProxy反向代理遇到的坑
    Nginx反向代理与Backend直接配置长连接
    openssl:AES CBC PKCS5 加解密 (C/GOLANG)
    纯C:AES256
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/3871895.html
Copyright © 2011-2022 走看看