tal :
Building a multi thread program i ran into a problem: the ConcurrentLinkedDeque's remove method was called by two threads and threw an exception. i can fix the problem by sync this method the way i did in this code, but i'm looking for a solution without synchronization. I searched for other suitable structures but havn't find any that doesn't throw exception or wait for the queue to be filled again.
public void releaseVehicle(DeliveryVehicle vehicle) {
acquireTable.put(vehicle.getLicense(), true);
synchronized (futureQueue) {
if (!futureQueue.isEmpty())
futureQueue.remove().resolve(vehicle);
}
}
i'm curious, is there another way?
Slaw :
You can use poll
instead. Since ConcurrentLinkedDeque
doesn't allow null
elements, poll
returning null
means the deque is empty (at the time of calling).
SomeClass element;
while ((element = deque.poll()) != null) {
// do something with element
}