[concurrency-interest] LIFO ThreadPoolExecutor

Peter Veentjer alarmnummer at gmail.com
Tue Apr 28 16:16:21 EDT 2009


You could wrap a Stack or Deque in a Queue implementation so that it
behaves like a stack and inject it in the executor as the workpool to
use.

so something like this:

class FifoQueue implements BlockingQueue{

	Deque deque = new LinkedBlockingDeque();

	public void put(Object item){
		deque.addFirst(item);
	}

	public Object take(){
		return deque.removeFirst();
	}

       ....
}

The disadvantage of your approach is that your system could be
suffering from starvation; some requests are possibly never processed
if new requests keep coming in.

On Tue, Apr 28, 2009 at 9:43 PM, Paulo Levi <i30817 at gmail.com> wrote:
> I'm doing requests to a web service for thumbnails over a
> threadpoolexecutor, configured like this:
> http://www.kimchy.org/juc-executorservice-gotcha/
>
> As I'm using a web service I'd like to configure the Linked blocking queue
> the executors use to be able to be LIFO (according to the factory method),
> so that the relevant requests are processed first.
>
> I have these static factory methods :
>     /**
>      * A pool with exactly nThreads that don't timeout
>      * @param nThreads > 0
>      * @return
>      */
>     public static ChainExecutorService newFixedThreadPool(int nThreads)
> (using linkedBlockingQueue)
>
>     /**
>      * A pool that has no maximum number of threads
>      * and will kill the threads after a timeout after the last task
>      * @param secondsTimeOut > 0
>      * @return
>      */
>     public static ChainExecutorService newCachedThreadPool(long
> secondsTimeout)
> (using SynchronousQueue)
>
>     /**
>      * A pool that has a maximum number of threads
>      * and will kill the threads after a timeout after the last task
>      * @param maximumNThreads > 0
>      * @param secondsTimeOut > 0
>      * @return
>      */
>     public static ChainExecutorService newScalingThreadPool(int
> maximumNThreads, long secondsTimeOut)
> (using a equivalent class to ScalingQueue from the blog above - extends
> LinkedBlockingQueue)
>     /**
>      * A pool that has a minimum number of threads, a maximum number of
> threads
>      * and will kill maximumNThreads - minimumNThreads after a timeout after
> the last task
>      * @param minimumNThreads >= 0
>      * @param maximumNThreads > 0
>      * @param secondsTimeOut > 0
>      * @return
>      */
>         public static ChainExecutorService newScalingThreadPool(int
> minimumNThreads, int maximumNThreads, long secondsTimeOut)
> (using a equivalent class to ScalingQueue from the blog above - extends
> LinkedBlockingQueue)
>
> I'm asking the way to modify LinkedBlockingQueue and SynchronousQueue so
> that i can implement LIFO for the ThreadPoolExecutor.
>
> I know this is a complex question - but i'm really raw at implementing data
> structures like this, and i fear screwing it up.
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>



More information about the Concurrency-interest mailing list