本帖最后由 杨兴庭 于 2013-7-15 21:21 编辑
- <p>代码1:</p><p>public synchronized void set(String name)
- {
- while(flag)
- try
- {
- this.wait();
- }
- catch (InterruptedException e)
- {
- }
- this.name = name;
- count++;
- System.out.println(Thread.currentThread().getName()+name+"--生产--"+count);
- flag = true;
- this.notifyAll();
- }</p>
复制代码 代码2:- public void set(String name) throws InterruptedException
- {
- lock.lock();
-
- try
- { while(flag)
- continue_pro.await();
- this.name = name;
- count++;
- System.out.println(Thread.currentThread().getName()+name+"--生产--"+count);
- flag = true;
- continue_con.signal();
- }
- finally
- {
- lock.unlock();
- }
复制代码 问题:
代码1中的锁是什么时候释放的,为什么不放在finally里面?
|