How to download multiple files concurrently using intentservice in Android?

后端 未结 3 1683
面向向阳花
面向向阳花 2020-12-29 00:12

I want to create a service similar to this one, (reference from Here), to download multiple files asynchronously in Android.

public static class Downloading         


        
3条回答
  •  情歌与酒
    2020-12-29 00:50

    IntenService and AsyncTask provide a single worker thread, therefore simultaneous download of multiple images is not possible. However, i strongly suggest ThreadPoolExecutor for your requirements. It basically creates a pool of threads which are expended/shrink based on the number of tasks you apply or number of files you want to download. THreadPoolExecutor handles practically every aspect of threads management and its quite efficient. You create a single executor with the as in this code sample:

    ExecutorService tpExecutor=
            new ThreadPoolExecutor(
                    coreSize,      // the basic number of threads, when a task is added to the queue
                                   // a new thread is created if nrOfThreads < coreSize  
                    maxPoolSize,   // maximum number of threads created
                    keepAliveTime,
                    TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue()
                    ); 
    

    You can submit multiple downloading task by executing runnables:

    tpExecutor.execute(new Runnable());
    

提交回复
热议问题