Add the first element to a ConcurrentLinkedQueue atomically
===

I want to use a [ConcurrentLinkedQueue][1] in an atomic [lock-free][2] manner:
 
Several concurrent threads push events into the queue and some other thread will process them. The queue is not bound and I don't want any thread to wait or get locked. The reading part however may notice that the queue got empty. In a lock free implementation the reading thread must not block but will just end its task and proceeds executing other tasks (i.e. as an ExecutorService). Thus the writer pushing the first new event into an empty queue must become aware about it and should restart the reader (i.e. by submitting a new Runnable to the ExecutorService) to process the queue. Any further threads submitting a second or third event won't care about, as they may assume some reader was already prepared/submitted.

Unfortunately the [add()][3] method of ConcurrentLinkedQueue always returns true. Asking the queue if isEmpty() before or after adding the event won't help, as it is not atomic. 
Should I use some additional AtomicInteger for monitoring the queue size() or is there some smarter solution for that?

Dieter.


  [1]: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html
  [2]: http://en.wikipedia.org/wiki/Non-blocking_algorithm
  [3]: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#add%28E%29