日期:2014-05-20 浏览次数:20684 次
public class Test3 { public static void main(String[] args) { computer3 t = new computer3(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); } } class computer3 extends Thread { int i = 10; static Object obj = new Object(); public void print() { System.out.println(Thread.currentThread().getName() + ":" + i); i--; } public void run() { while (i > 0) { synchronized (obj) { print(); } try { sleep(100); } catch (Exception e) { } } } }
while (i > 0) { synchronized (obj) { if(i>0){ // 加上这个if吧,后面别忘了加上}