[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  

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?



     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  
         final ReentrantLock lock = this.lock; //why is a local  
variable needed?
         try {
             boolean ok = q.offer(o);
             assert ok;
             return true;
         } finally {

David Walend
david at walend.net

More information about the Concurrency-interest mailing list