如何创建LIFO执行器? [英] How to create LIFO executor?
问题描述
我想创建一个线程池,它将执行最近提交的任务。关于如何做到这一点的任何建议?
I would like to create a thread pool which will execute the most recently submitted task. Any advice on how to accomplish this?
谢谢
推荐答案
你可以实现自己的 BlockingQueue
将offer / poll映射到堆栈的包装器。然后将此作为 BlockingQueue
实现传递给 ThreadPoolExecutor
。我的建议是包装一个现有的 Deque
实现,例如 ArrayDeque
。
You could probably just implement your own BlockingQueue
wrapper that maps offer/poll to a stack. Then use this as the BlockingQueue
implementation you pass to a ThreadPoolExecutor
. My suggestion would be to wrap one of the existing Deque
implementations such as ArrayDeque
.
- 这是不同步的,所以你需要包装每个
BlockingQueue
使用同步器的方法(如果不是更奇特的东西)。 - 您还需要引入
等待
/通知
条件阻止操作。 - 最后,您需要映射一组
BlockingQueue
极性(put或take方面) )与另一个出列到同一端(将其视为堆栈)。
- This is not synchronized, so you'll need to wrap each of the
BlockingQueue
methods with a synchronizer (if not something more exotic). - You'll also need to introduce
wait
/notify
conditions for the blocking operations. - Finally, you'll need to map one set of the
BlockingQueue
polarities (either the "put" or the "take" side) to the same end of the dequeue as the other (to treat it like a stack).
请注意,在更快的并发上有一些工作(参见Herlihy关于多处理器编程艺术的书)堆栈,但我不认为JDK中有任何实现,我不确定Herlihy的实现是否提供阻塞风格。
Note that there is some work (see Herlihy's book on The Art of Multiprocessor Programming) on faster concurrent stacks, but I don't think there are any implementations in the JDK and I'm not sure if Herlihy's implementations offer blocking flavors.
我检查了 Android文档,这表明Deque在你身边,所以这是一个实现。在堆栈周围做包装也是一个相当容易的步骤,但Deque是首选。
I checked the Android documentation, which suggests that Deque is around for you, so here's an implementation. It's a fairly easy step to do a wrapper around a stack, too, but Deque is preferred.
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
public final class BlockingLifoQueue<T> implements BlockingQueue<T>
{
// we add and remove only from the end of the queue
private final BlockingDeque<T> deque;
public BlockingLifoQueue()
{ deque = new LinkedBlockingDeque<T>(); }
public boolean add(T e) {
deque.addLast(e);
return true;
}
public boolean contains(Object o)
{ return deque.contains(o); }
public int drainTo(Collection<? super T> c)
{ return deque.drainTo(c); }
public int drainTo(Collection<? super T> c, int maxElements)
{ return deque.drainTo(c,maxElements); }
public boolean offer(T e)
{ return deque.offerLast(e); }
public boolean offer(T e, long timeout, TimeUnit unit)
throws InterruptedException
{ return deque.offerLast(e,timeout,unit); }
public T poll(long timeout, TimeUnit unit) throws InterruptedException
{ return deque.pollLast(timeout, unit); }
public void put(T e) throws InterruptedException
{ deque.putLast(e); }
public int remainingCapacity()
{ return deque.size(); }
public boolean remove(Object o)
{ return deque.remove(o); }
public T take() throws InterruptedException
{ return deque.takeLast(); }
public T element()
{
if (deque.isEmpty()) {
throw new NoSuchElementException("empty stack");
}
return deque.pollLast();
}
public T peek()
{ return deque.peekLast(); }
public T poll()
{ return deque.pollLast(); } // deque.peekLast(); } -- fixed typo.
public T remove()
{
if (deque.isEmpty()) {
throw new NoSuchElementException("empty stack");
}
return deque.pollLast();
}
public boolean addAll(Collection<? extends T> c)
{
for (T e : c) { deque.add(e); }
return true;
}
public void clear()
{ deque.clear();}
public boolean containsAll(Collection<?> c)
{ return deque.containsAll(c); }
public boolean isEmpty()
{ return deque.isEmpty(); }
public Iterator<T> iterator()
{ return deque.descendingIterator(); }
public boolean removeAll(Collection<?> c)
{ return deque.removeAll(c); }
public boolean retainAll(Collection<?> c)
{ return deque.retainAll(c); }
public int size()
{ return deque.size(); }
public Object[] toArray()
{ return deque.toArray(); }
public <T> T[] toArray(T[] a)
{ return deque.toArray(a); }
}
这篇关于如何创建LIFO执行器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!