本帖最后由 罗广伟 于 2013-6-14 10:24 编辑
- class Resource
- {
- private String name;
- private int count = 1;
- private boolean flag = false;
- private Lock lock = new ReentrantLock();
- private Condition condition_pro = lock.newCondition();
- private Condition condition_con = lock.newCondition();
- public void set(String name)throws InterruptedException
- {
- lock.lock();
- try
- {
- while(flag)
- condition_pro.await();
- this.name = name+"--"+count++;
- System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
- flag = true;
- condition_con.signal();
- }
- finally
- {
- lock.unlock();//释放锁的动作一定要执行。
- }
- }
- public void out()throws InterruptedException
- {
- lock.lock();
- try
- {
- while(!flag)
- condition_con.await();
- System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
- flag = false;
- condition_pro.signal();
- }
- finally
- {
- lock.unlock();
- }
-
- }
- }
复制代码 问题位置:12天06,问题内容:condition_pro.signal();是如何被识别为是唤醒生产者线程而不是消费者线程的。
|