首页 > 代码库 > 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(); }} */
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。