Java没有这样的结构。 我希望这一习俗的实施符合你们的需要:
BlockingDeque<Object> deque = new LinkedBlockingDeque<Object>(32) {
public void push(Object e) {
final java.util.concurrent.locks.ReentrantLock lock;
try {
Field lockField = LinkedBlockingDeque.class.getDeclaredField("lock");
lockField.setAccessible(true);
lock = (ReentrantLock) lockField.get(this);
} catch (NoSuchFieldException e1) {
throw new RuntimeException(e1);
} catch (SecurityException e1) {
throw new RuntimeException(e1);
} catch (IllegalArgumentException e1) {
throw new RuntimeException(e1);
} catch (IllegalAccessException e1) {
throw new RuntimeException(e1);
}
lock.lock();
try {
if (!offerFirst(e)) {
pollLast();
offerFirst(e);
}
} finally {
lock.unlock();
}
}
};
然后,你将它当作一个圣克(取代旧的圣克阶级):
deque.push(new Object());
Object o = deque.pop();
Sorry for all the lock stuff, but it was needed to avoid inconsistencies.