码迷,mamicode.com
首页 > 编程语言 > 详细

java多线程执行问题

时间:2014-07-27 23:10:19      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:blog   java   io   for   art   问题   div   ar   

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

  

java多线程执行问题,布布扣,bubuko.com

java多线程执行问题

标签:blog   java   io   for   art   问题   div   ar   

原文地址:http://www.cnblogs.com/hujunzheng/p/3871895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!