看看这段代码 难道不是这样吗?为什么要同步run方法呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该是这样的 你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(); } } 你把循环次数改大点,因为循环一次的时间不够CPU的一个时间片的时间,所以还没轮到下一个时间片,程序就执行完了,所以就出现这样的结果 不好意思 上面写的有问题 应该如下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(); } }这下没问题了 如何将字节数组的内容分割成字符串数组? 自定义JTextField组件问题 一个String的replaceAll的奇怪问题 简单问题 求助关于JMF 请问 thinking in java(java编程思想) 这本书的市面最新版本是? AWT事件的问题?高分! 关于多线程中wait()和notify()的疑问 Java与xml 为什么覆盖override方法的可见性不可以降低,这样做有什么意义呢?java 菜鸟问题,请大家看一看 一个入门的问题.
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();
}
}
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();
}
}这下没问题了