Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
tklanilkumar
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Getting Too many Threads problem while dowloading Hundreds of images

Hi friends

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 

*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Getting Too many Threads problem while dowloading Hundreds of images

use a queue that is limited to a fixed amount of threads, for example 3.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting Too many Threads problem while dowloading Hundreds of images

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. 

Please use plain text.
Developer
tklanilkumar
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: Getting Too many Threads problem while dowloading Hundreds of images

hi simon thanks for the reply..actually I have to download for more than 50 images , as per your suggestion can the queue concept is useful..can you provide me the link/sample code for this queue concept. right now what im doung is in the for loop for each url im trying to donload blindly thats why getting Too many threads problem.suggest me.

Thanks in advance
Anil
*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Please use plain text.
Developer
swap_chau
Posts: 179
Registered: ‎06-15-2011
My Device: Storm 9550
My Carrier: Vodaphone

Re: Getting Too many Threads problem while dowloading Hundreds of images

Can you post your code here?

Please use plain text.
Developer
tklanilkumar
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: Getting Too many Threads problem while dowloading Hundreds of images

sorry by mistake accept as solution clicked other than quick reply.still I hangout for Thread polling. after go through some threads regarding pooling in google some suggested for TimerTask for scheduling ..If i want to donload 50 images with urls how can I achieve it with your suggested pooling/queue concept..

Thanks in advance
Anil
*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting Too many Threads problem while downloading Hundreds of images

[ Edited ]

I suspect there is a lot more searching you can do to find more ideas, especially on this forum.

 

For example:

http://supportforums.blackberry.com/t5/Java-Development/about-multi-threading-and-performance/m-p/14...

 

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. 

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Getting Too many Threads problem while dowloading Hundreds of images

[ Edited ]

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.

 

For summary:

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).

 

Pros:

It might actually work and it uses a built-in API (less coding and usually better performance).

 

Cons:

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,

 

E.

Please use plain text.
Developer
RileyGB
Posts: 282
Registered: ‎08-06-2010
My Device: Z10

Re: Getting Too many Threads problem while dowloading Hundreds of images

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.



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting Too many Threads problem while downloading Hundreds of images

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. 

Please use plain text.