I wrote a lazy image downloader for my app using an ExecutorService. It gives me great control about how many downloads are running in parallel at what time and so on.
Now, the only problem that I have is that if I submit a task it ends up at the tail of the queue (FIFO).
Does anyone know how to change this to LIFO?
You will need to specify the queue type that the ExecutorService is using.
Typically you might be retrieving an ExecutorService via the static methods in Executors. Instead you will need to instantiate one directly and pass in the Queue type that you want that provides LIFO.
EG, to create a LIFO thread pool executor, you could use the following constructor.
and pass in a LIFO queue as the final parameter.
There is no LIFO queue in the java collections that I am aware of (please correct me if wrong), but you could easily just create an anonymous inner class that extends LinkedBlockingQueue and overrides the appropriate methods.
For example, (untested)
UPDATE in response to comments.
We can use a blocking queue that wraps a priority queue. We have to wrap because the Executor expects runnables but we need timestamps too.