java初学者 加入小组

294个成员 65个话题 创建时间:2015-02-03

JAVA开发实战经典中第9章275页中,为什么我运行后线程没有交替执行?具体如下

发表于2015-11-14 3816次查看

class MyThread extends Thread{//继承Thread类,作为线程的实现类
    private String name ;
 public MyThread(String name){
     this.name = name ;
 }
    public void run(){//覆写run()方法,作为线程的操作主体
     for (int i=0;i<10;i++ ){
   System.out.println(name + "运行,i = " +i);
     }
 }
}
public class ThreadDemo02{
 public static void main(String args[]){
     MyThread mt1 = new MyThread("线程A");//实例化对象
  MyThread mt2 = new MyThread("线程c");//实例化对象
  mt1.start();//调用线程主体
  mt2.start();
 }
}

----------------------------------------------------------------------------

运行结果:

D:\threaddemo>java ThreadDemo02
D:\threaddemo>java ThreadDemo02
线程A运行,i = 0
线程A运行,i = 1
线程A运行,i = 2
线程A运行,i = 3
线程A运行,i = 4
线程A运行,i = 5
线程A运行,i = 6
线程A运行,i = 7
线程A运行,i = 8
线程A运行,i = 9
线程c运行,i = 0
线程c运行,i = 1
线程c运行,i = 2
线程c运行,i = 3
线程c运行,i = 4
线程c运行,i = 5
线程c运行,i = 6
线程c运行,i = 7
线程c运行,i = 8
线程c运行,i = 9

 

1回复
发表回复
你还没有登录,请先 登录或 注册!