I’m doing a download application for android. The downloading part is now successfully implemented and its working.
What I need is to download the file parallel in segments. To be more clear, if the user specify 8 segments, I want to create 8 Threads and do the downloading.
So in what way will I be able to create 8 threads dynamically? Also as I’m doing this for an phone how will I be able to maintain the memory consumption at a minimum level?
I have not worked with threads before, so I hope you can help me with this. Thank you for your time! 🙂
I’m doing a download application for android. The downloading part is now successfully implemented
Share
The most efficient way to create a fixed number of threads is to use the ExecutorService:
It’s basically a fixed-size thread pool that takes a lot of the management burden from the developer.
Edit: So your flow should be something like this:
First, define your thread task class (each thread will execute the
callmethod of its own task):If you want to pass any parameters to the tasks, put some private fields in the class above and pass them through a constructor. Also, you can return any type from
call, just change the type in theimplements Callable<...>part.When you want to fire off the threads, create the pool and submit the tasks: