- 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();//t1,t2
- 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();
- }
-
- }
- }
复制代码 在老师写的代码中这两句话 private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
怎么能判断condition_pro就是获取的是生产者的锁,condition_con就是获取的是消费者的锁呢?
这块不太明白,请大家帮我解释解释。 |