[concurrency-interest] LinkedBlockedQueue

Kasper Nielsen kav@kav.dk
Sat, 27 Sep 2003 00:35:50 +0200


Hi,

I was looking at the toArray() methods in LinkedBlockingQueue 

1.
Any reason why we set first.item = null in extract() but not in
toArray()

2.
Looking at the code,
        fullyLock();
        try {
            int size = count.get();
            Object[] a = new Object[size];
            int k = 0;
            for (Node<E> p = head.next; p != null; p = p.next)
                a[k++] = p.item;
            return a;
        } finally {
            fullyUnlock();
        }

I was wondering why the lock was kept all the time why not just
something like

	  Object[] a;
        Node<E> oldhead;        
        fullyLock();
        try {
            int size = count.get();
            a = new Object[size];
            oldHead=head;
           last = head = new Node<E>(null);
        } finally {
            fullyUnlock();
        }
            int k = 0;
            for (Node<E> p = oldHead.next; p != null; p = p.next)
                a[k++] = p.item;
            return a;

3.
Would be nice with some methods
  poll(int numberOfElements)
  poll(int numberOfElements, long time,TimeUnit unit)
  pollAll (long time,TimeUnit unit) (same as toarray() but blocking)
These methods would be usefull when using it as a queue of events to be
processed. 
Sometimes I would need a large batching factor (higher cache locality &
higher throughput) and sometimes a low (lower response time) by being
able to specify how many elements I wanted to fetch I could accomplish
this.


4. Javadoc corrections
In all classes except Semaphore & Future its TimeUnit unit instead of
TimeUnit granularity

- Kasper