views:

328

answers:

3

I am facing problem in blackberry development. In my application I have to get images from the server, so i have to create a separate connection thread for each image i load from the server..but in doing so i am getting TooManyThreadsException..Any ideas regarding controlling the threads...

In blackberry an application can have maximum of 16 threads running concurrently...but i have to display more than 16 images at a time...

+5  A: 

Reuse the threads, queue up all the images, and run just a couple of threads, each processing one image at a time and then moving on to the next.

There is a reason the unit only allows 16 per process, and it has to do with resources. You basically have to make do with fewer threads.

Lasse V. Karlsen
But the real problem i am facing is that the threads are not getting destroyed when i press the back button...i want these threads to stop execution as soon i press back button...these threads continue running in the background even if exit the screen...
tek3
@NGA, use a thread pool. Then you won't have to keep creating / destroying threads. Use some kind of inter thread messaging system to signal your threads when the back button is pressed, so that they can stop what they're doing.
Glen
+1  A: 

You run into a similar limitation with older browsers like IE6 which limit you to 2 concurrent connections per domain.

Re-use the threads and download the images serially on fewer concurrent threads (perhaps using 2-3 threads to speed things up).

Just speculating here, but tying up all 16 threads seems like it could lead to an unstable environment since the BB OS is doing other things while your program is running.

Chris Ballance
+1  A: 

I have been taught that creating threads dynamically is almost never an good approach, I would have created 1-2 worker threads with an queue that would do the job for me.

NPehrsson