Replaceable work queue

There is a name for that, but I don’t know what it is, so it’s difficult for Google.

What I'm looking for is something in the java concurrency utilities, which are a couple of queues, a “waiting” queue used by the manufacturer, and a “processing” queue used by the consumer, where the consumer can automatically change the queues. If used in this way (1 producer stream, 1 consumer stream), the individual queues should not be thread safe, but simply links to them.

I know that I saw it somewhere earlier, and I may possibly bend something like this on my own, but if it exists, I would prefer to use it.

edit: I assume that the primitive I'm looking for is a pair of atomic links that can be atomically replaced. (& I can add the queues myself.)


edit 2: @ Alex Miller answered a glaring question about what I was thinking but couldn't remember. However, this does not solve my problem, since it is a flow barrier, and I want the producer not to block.

@Sfossin's value about queuing links is good; I wanted that at the moment when the consumer begins to extract and process the elements from the queue, all these elements of the queue should be complete, and the manufacturer cannot add any elements after that; Now the manufacturer must add items to another queue. Thus, a paired / replaced set of atomic links will not work.

(It seems that if there are 2 school buses, one of which is always waiting for passengers, and the other always delivers them to another place. As soon as the driver gets out, you need to get on another bus. Having links, manufacturers can access the bus, even if it already gone, which is not allowed.)

I think that instead I should use a single ConcurrentLinkedQueue and have a sentinel value that the consumer adds to the queue. This allows multiple manufacturers to use, not just 1. In order for the consumer to process batches of items in the queue, the consumer waits for the queue to queue there was at least 1 element, then inserts a guard board at the end of the queue and removes the elements until the watch is removed. Then the consumer does everything that he has to do between parties. This is the behavior that I wanted.

It does not have to be a guaranteed non-blocking approach (locking or synchronized methods are parameters), but if there is an easy way to archive it so that it is so preferable in my application.

+4
source share
3 answers
+2
source

A closed read / write queue is probably the best idea. Because if you would otherwise have to automatically copy to the local queue.

Since you will have a synchronization problem if you replaced links while you were reading / deleting or writing.

RWLock will allow multiple consumers / manufacturers.

Is there a reason why you want to stay away from RWLocks?

As with any fixation, the lock should be held for a minimum time to prevent starvation.

or use something similar for data exchange. With each thread containing its own queue.

  class LockedQueue { private final List<Data> q = new ArrayList<Data>(); private final Lock lock = new ReentrantLock(); public void get( List<Data> consumerQ ) { if( lock.tryLock() ) { try { consumerQ .addAll( q ); q.clear(); } finally { lock.unlock(); }} } public Data put( List<Data> producerQ ) { if( lock.tryLock() ) { try { return q.addAll( producerQ ); producerQ .clear(); } finally { lock.unlock(); }} } public void clear() { lock.lock(); try { q.clear(); } finally { lock.unlock(); } } } 

with a consumer call to get () when it is empty or at the end of each loop and a user call to put, after adding so many elements or time or ...

+2
source

There is no data structure with double queues as you describe, but parallel utilities have many queues to choose from.

The problem you are describing is very common, although I am expected to find it too. Maybe in Java 1.7.

0
source

All Articles