[concurrency-interest] PriorityBlockingQueue question

David Walend david at walend.net
Mon Sep 25 16:19:35 EDT 2006


I'm imitating the PriorityBlockingQueue code to add Conditions  
dynamically.

I ran across a line of code I don't understand. The member variable  
lock is final. The offer() method sets up a final local variable  
named lock, set to the same value. Nothing can change the final  
(short of the hack used by Serialization). The Condition notEmpty and  
the PriorityQueue q don't get the same treatment.

Do I need to imitate that line of code, or can I skip it?

Thanks,

Dave

     private final PriorityQueue<E> q;
     private final ReentrantLock lock = new ReentrantLock(true);
     private final Condition notEmpty = lock.newCondition();

...

     public boolean offer(E o) {
         if (o == null) throw new NullPointerException(); //needs a  
message
         final ReentrantLock lock = this.lock; //why is a local  
variable needed?
         lock.lock();
         try {
             boolean ok = q.offer(o);
             assert ok;
             notEmpty.signal();
             return true;
         } finally {
             lock.unlock();
         }
     }


David Walend
david at walend.net




More information about the Concurrency-interest mailing list