对不起昨晚后来就睡了,现在在公司,代码如下:
public class TestThread extends Thread{
String a;
TestThread(String str){
this.a=str;
}
public void run(){
for (int i=0;i<10;i++){
System.out.println(a+"---"+i);
}
}
public static void main(String[] args) {
TestThread aa=new TestThread("one");
TestThread bb=new TestThread("two");
aa.start();
bb.start();
}
}在ide里面的运行结果永远都是 one 完了才到two但是我在命令行(dos窗口)下,运行,就是很正常的,每次都不一样的杂乱交替请问这是什么原因呢?

解决方案 »

  1.   

    将aa  Sleep 一下试试!!
      

  2.   

    试试下面的代码:
    public class TestThread extends Thread{
            String a;
            TestThread(String str){
                    this.a=str;
            }
            public void run(){
                    for (int i=0;i<10;i++){
                            System.out.println(a+"---"+i);
                    }
            }
            public static void main(String[] args) {
                    TestThread aa=new TestThread("one");
                    TestThread bb=new TestThread("two");
                    try {
                      Thread.sleep(10);
                    }
                    catch (InterruptedException ex) {
                    }
                    aa.start();
                    bb.start();
            }
    }
      

  3.   

    for (int i=0;i<10;i++){
    System.out.println(a+"---"+i);
    }
    改成
    for (int i=0;i<100;i++){
    System.out.println(a+"---"+i);
    }
    或者加上sleep:
    for (int i=0;i<10;i++){
    System.out.println(a+"---"+i);
                               sleep(100);
    }
    不加sleep有些操作系统可能会让线程独占CPU的时间的
      

  4.   

    如果都sleep还能算是多线程吗?呵呵,用Runnable吧
      

  5.   

    输出对象不一样
    在IDE里面输出到IDE的窗口
    而在DOS下是输出到DOS的CONSOLE,两个处理字符流的机制不一样!
      

  6.   

    用了sleep还有什么意义呢?呵呵,我的目的就是要让两个线程同时运行……
    或许确实是如闲云散步 所说的原因吧,其实也不是什么原则性的大问题,只是我觉得实在是太奇怪了,所以想问个明白,呵呵。
    另外回楼上,也不是重复次数的原因。谢谢 闲云散步,那小弟就把分数奉上了,初来,分数不多,,只开了10分,还望大家不要见笑啊呵呵
      

  7.   

    把run函数修改如下试试:
    public void run(){
    for (int i=0;i<10;i++){
    System.out.println(a+"---"+i);
                                System.out.flush();
    }