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