难道不是这样吗?
为什么要同步run方法呢

解决方案 »

  1.   

    应该是这样的 你run()里面的程序没有留时间给另外的线程运行 我猜你的目的可能是这样public class TestThread implements Runnable {     public synchronized void run() {
           for (int i = 0; i < 10; i++) {
             System.out.print(" " + i);
                   sleep(100);//给另外的线程留时间运行
           }
         }     public static void main(String[] args) {
           Runnable r1 = new ThreadTest();
           Runnable r2 = new ThreadTest();
           Thread t1 = new Thread(r1);
           Thread t2 = new Thread(r2);
           t1.start();
           t2.start();
         }
       }
      

  2.   

    你把循环次数改大点,因为循环一次的时间不够CPU的一个时间片的时间,所以还没轮到下一个时间片,程序就执行完了,所以就出现这样的结果
      

  3.   

    不好意思 上面写的有问题 应该如下public class TestThread implements Runnable {     public synchronized void run() {
          try{
         
            for (int i = 0; i < 10; i++) {
              System.out.print(" " + i);
                Thread.sleep(10);//给另外的线程留时间运行
           }
           }
           catch(InterruptedException e){
       }
         }     public static void main(String[] args) {
           Runnable r1 = new TestThread();
           Runnable r2 = new TestThread();
           Thread t1 = new Thread(r1);
           Thread t2 = new Thread(r2);
           t1.start();
           t2.start();
         }
       }这下没问题了