01-30-2012 05:55 AM
Im getting Two many threads while im trying to download the images more than 50. Im trying to download the image one by one ,while doing this I got realised that my Thread count is increasing. I thought that after downloading one thread I have to kill that downloaded thread and start a new one,this process has to go until the number of images.
how to do this can anyone guide me pls.
Thanks in advance.
Anil Kuma R
01-30-2012 05:58 AM
01-30-2012 06:04 AM
Agree with Simon that using a queue is a good option. There are a number of other options too. I would recommend that you review this forum and the internet in general on the concept of Thread pooling.
01-30-2012 06:16 AM
01-30-2012 07:26 AM
01-30-2012 08:25 AM - edited 01-30-2012 08:26 AM
I suspect there is a lot more searching you can do to find more ideas, especially on this forum.
I am a little lost regarding your comments about using TimerTasks for scheduling. That doesn't make any sense to me.
I suggest that the best way forward here is for you to describe a possible implementation approach that you have researched and we can comment on this.
I think it is important that you understand what you are coding and the reasons behind the choice of approach. If you just implement something that someone suggests here, I suspect you will not understand it or the implications of it and when it breaks you will have problems fixing it.
01-30-2012 05:32 PM - edited 01-30-2012 05:35 PM
Think about the Timer object as a single thread (unbounded) queue of TimerTask objects, meaning, only one timerTask can run at a time and the tasks are executed one after the other (let's ignore the schedule option for the moment).
If you would create a class which would extend the TimerTask and use to connect to your server, you can create multiple instances (preferably, create new instance on the on the end of the current instance run method) and execute them one after the other.
To use more than one thread, you would need to create another Timer object for each thread.
1. Extend TimerTask and use this class to connect to a URL.
2. Make sure to create new instance only at the end of the run method.
3. Have a (synchronized) vector and a static counter that would keep track of the URL already read.
4. At the end of the run function check if you reached the end of the URL list. If not, create another instance of your class (see #1) and schedule it to run immediately.
5. Create Timer instances per the number of threads you would like to use (see cons).
It might actually work and it uses a built-in API (less coding and usually better performance).
You are limited to the number Timers that you can create (I think 5 for an application) and it's a bit of an abuse to the Timer mechanism.
Hope that helps,
01-30-2012 05:43 PM
If you want to download that many images, it may make sense to zip them up on the server side and unzip them on the client side.
01-30-2012 06:11 PM
Not wishing to be too critical, but I don't really understand the implementation proposed by maadani. Why not just have a Thread, and Runnables (that the Thread executes) rather than a Timer and TimerTasks? Same job, same function, and no abuse of mechanisms...
But actually the point here for me is that I think the OP needs to review the options and design a solution that works in his/her environment. Only they fully understand the requirements. And they will be responsible for the code in the future so they had better understand it too.