本帖最后由 杜加璇 于 2013-4-5 18:06 编辑
class Printer {
private int flag = 1;
private ReentrantLock lock = new ReentrantLock();
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
private Condition c3 = lock.newCondition();
public void print1() throws Exception {
lock.lock();
if (flag != 1)
c1.await();
System.out.print("传");
System.out.print("智");
System.out.print("播");
System.out.print("客");
System.out.print("\r\n");
flag = 2;
c2.signal();
lock.unlock();
}
public void print2() throws Exception {
lock.lock();
if (flag != 2)
c2.await();
System.out.print("黑");
System.out.print("马");
System.out.print("程");
System.out.print("序");
System.out.print("员");
System.out.print("\r\n");
flag = 3;
c3.signal();
lock.unlock();
}
public void print3() throws Exception {
lock.lock();
if (flag != 3)
c3.await();
System.out.print("i");
System.out.print("t");
System.out.print("c");
System.out.print("a");
System.out.print("s");
System.out.print("t");
System.out.print("\r\n");
flag = 1;
c1.signal();
lock.unlock();
}
}这个程序里都是指定唤醒线程1唤醒2 线程2唤醒3 线程3唤醒1 那这个if()判断不就没意义了吗为什么还要加上呢 |