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

      

  • 相关阅读:
    vm12序列号
    三星手机官方固件下载
    MSTP故障处理手册
    分享一个高清壁纸网站
    ThinkPad X220 完美黑苹果 Hackintosh OS X 10.11 El Capitan
    一句命令激活windows/office
    Win10+VMplayer12中U盘无法挂载解决
    记一次金士顿DT100 G3 32G修复
    飘雪代码2枚
    禁用安全模式
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/3871895.html
Copyright © 2011-2022 走看看