Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:25:41+00:00 2026-05-22T15:25:41+00:00

I wrote a lazy image downloader for my app using an ExecutorService. It gives

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:25:42+00:00Added an answer on May 22, 2026 at 3:25 pm

    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.

    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
    

    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)

    ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 16, 1, TimeUnit.MINUTES, new LinkedBlockingQueue() {
    
      @Override
      public void put(Object obj) { 
        // override to put objects at the front of the list
        super.addFirst(obj);
      }
    
    });
    

    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.

    // the class that will wrap the runnables
    static class Pair {
    
         long   timestamp;
        Runnable    runnable;
    
        Pair(Runnable r) {
            this.timestamp = System.currentTimeMillis();
            this.runnable = r;
        }
    }
    
    
        ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 16, 1, TimeUnit.MINUTES, new BlockingQueue<Runnable>() {
    
            private Comparator          comparator      = new Comparator<Pair>() {
    
                                                    @Override
                                                    public int compare(Pair arg0, Pair arg1) {
                                                        Long t1 = arg0.timestamp;
                                                        Long t2 = arg1.timestamp;
                                                        // compare in reverse to get oldest first. Could also do
                                                        // -t1.compareTo(t2);
                                                        return t2.compareTo(t1);
                                                    }
                                                };
    
            private PriorityBlockingQueue<Pair> backingQueue    = new PriorityBlockingQueue<Pair>(11, comparator);
    
            @Override
            public boolean add(Runnable r) {
                return backingQueue.add(new Pair(r));
            }
    
            @Override
            public boolean offer(Runnable r) {
                return backingQueue.offer(new Pair(r));
            }
    
            @Override
            public boolean offer(Runnable r, long timeout, TimeUnit unit) {
                return backingQueue.offer(new Pair(r), timeout, unit);
            }
    
            // implement / delegate rest of methods to the backing queue
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a process explorer using C with GUI interface. I want to add
I write iOS app and use imageStore library to lazy load images and cache
I'm looking to map a modestly-expensive function onto a large lazy seq in parallel.
Hey guys.. Quick question: I wrote a simple JS that opens lightBox for image
I want to display an image AND a String using Spring MVC 3. Both
I want to sort an array by using uasort() function. I wrote this piece
I wrote a file tree using JTree, and now I am trying to make
I wrote a little lazy vector class (or, delayed vector) which is supposed to
I have wrote an Application for a organisation and they had let the app
I want to know how many iteratons were done by LINQ. so i wrote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.