I’m designing an app where i need to download multiple images from time to time. However, I want that whenever i send a download request it should be queued to a single thread instead of spawning multiple threads.
I would really appreciate if you could provide me with a detailed solution. Thanks !
I would take a look at the
Executors.newFixedThreadPool()method. It creates a thread pool, which will only run as many threads at once, as big number you pass it in the function. If you pass 1, it will run only one thread at a time, so your downloads will be in a sequence, but on “a” different thread.After you created your
ExecutorServicewith this method, you only need to pass aRunnable/Callablewithin itssubmit()method, and it will execute it. Dont forget toshutdown()when you are finished.