AFAIK, you can simple push your tasks (to the other side) in the queue yourself, ExecutorService will pop the queue automatically.
Tried that but that didn't seem to work.
Or extend ThreadPoolExecutor and override execute() or submit()?
That looked quite hard to get right by looking at the ThreadPoolExecutor source code.
I guess you could create a LIFO wrapper to the Deque.
This was the easiest option. Thanks.
If anyone else want's to use the hacked up wrapper code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| package util;
import java.util.Collection; import java.util.Iterator; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit;
public class LinkedBlockingStack<T> implements BlockingQueue<T>{ private final LinkedBlockingDeque<T> stack = new LinkedBlockingDeque<T>();
@Override public T remove() { return stack.remove(); }
@Override public T poll() { return stack.poll(); }
@Override public T element() { return stack.element(); }
@Override public T peek() { return stack.peek(); }
@Override public int size() { return stack.size(); }
@Override public boolean isEmpty() { return stack.isEmpty(); }
@Override public Iterator<T> iterator() { return stack.iterator(); }
@Override public Object[] toArray() { return stack.toArray(); }
@Override public <S> S[] toArray(final S[] a) { return stack.toArray(a); }
@Override public boolean containsAll(final Collection<?> c) { return stack.containsAll(c); }
@Override public boolean addAll(final Collection<? extends T> c) { return stack.addAll(c); }
@Override public boolean removeAll(final Collection<?> c) { return stack.removeAll(c); }
@Override public boolean retainAll(final Collection<?> c) { return stack.removeAll(c); }
@Override public void clear() { stack.clear(); }
@Override public boolean add(final T e) { return stack.offerFirst(e); }
@Override public boolean offer(final T e) { return stack.offerFirst(e); }
@Override public void put(final T e) throws InterruptedException { stack.put(e); }
@Override public boolean offer(final T e, final long timeout, final TimeUnit unit) throws InterruptedException { return stack.offerLast(e, timeout, unit); }
@Override public T take() throws InterruptedException { return stack.take(); }
@Override public T poll(final long timeout, final TimeUnit unit) throws InterruptedException { return stack.poll(); }
@Override public int remainingCapacity() { return stack.remainingCapacity(); }
@Override public boolean remove(final Object o) { return stack.remove(o); }
@Override public boolean contains(final Object o) { return stack.contains(o); }
@Override public int drainTo(final Collection<? super T> c) { return stack.drainTo(c); }
@Override public int drainTo(final Collection<? super T> c, final int maxElements) { return stack.drainTo(c, maxElements); } } |