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
peter_strange
Posts: 19,512
Registered: ‎07-14-2008

Re: Too Many Thread Exception

A queue of requests serviced by a HTTP processing Thread is how I would process this sort of requirement, unless performance or other considerations meant you had to use a different model.  In one situation I use two threads, one for small requests and other for large (slow) requests - which means that the small requests can get through while a large request is receiving or sending data.

 

Using one of the RIM collections, a queue is easy to create in persistent store so that it will survive device reboot. 

 

Note that most RIM supplied collections (e.g. Vector) are NOT Thread safe, so you should do some sort of synchronization to make sure that your adds and deletes don't mess up the collection.

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008

Re: Too Many Thread Exception

http://www.google.com/search?hl=en&q=site%3Asun.com+concurrency+&btnG=Google+Search&aq=f&oq=

 

 

You probably only need one or two connections at a time so you can queue up requests in 

one or two objects that serialize asynchronous requests. Also note that your Runnable is not Thread,

sometimes these do get confused.

 

 

 

Please use plain text.
Contributor
omgimanoob
Posts: 26
Registered: ‎06-10-2011
My Carrier: Fledge Simulated Network

Re: Too Many Thread Exception

Hi Peter,

 

My program consists of many MainScreens that each makes two http requests - two to get images and another is a POST method.

 

I am also using a Please Wait Screen, which:

1. Shows a series of animated bitmaps when a http request is made.

2. Pops when http request is complete.

 

To use the the Please Wait Screen, my http connection class extends Thread. So whenever a MainScreen is pushed, three threads are created. After several pushes (about 15 to 18), Eclipse's console shows these messages:

[0.0] VM:TMTPv=64
[0.0] VM:THMNx=246,r=0x17DEB000,t=java.util.TaskQueue
[0.0] VM:THMNx=14,r=0x167EC800,t=java.util.TaskQueue
[0.0] VM:THMNx=255,r=0x1A7ED400,t=java.util.TaskQueue
[0.0] VM:THMNx=32,r=0x1660F800,t=java.util.TaskQueue
[0.0] VM:THMNx=153,r=0x15E32C00,t=java.util.TaskQueue
[0.0] VM:THMNx=245,r=0x17AD3C00,t=java.util.TaskQueue
[0.0] VM:THMNx=236,r=0x14A15800,t=net.rim.device.cldc.io.devicehttp.ClientProtocol
[0.0] VM:THMNx=191,r=0x15D56000,t=java.util.TaskQueue
[0.0] VM:THMNx=173,r=0x1705A400,t=java.util.TaskQueue
[0.0] VM:THMNx=11,r=0x19CBE400,t=java.lang.Object
[0.0] VM:THDRr=too_many_threads
[0.0] VM:THDLv=0

 

as the line VM:THDRr=too_many_threads suggests, I believe too many threads are created.

 

Further attempts to push new screen will generate VM:TMTPv=64 message on Console.

 

In your post you mentioned that it is possible to use a single Thread to process many HTTP requests; in your opinion, is it doable for my situation? I'd greatly appreciate if you could tell me how it should be structured.

 

Thanks in advance :smileyhappy:

 

Nick

 

Please use plain text.
Developer
peter_strange
Posts: 19,512
Registered: ‎07-14-2008

Re: Too Many Thread Exception

Regarding the "problem" of the multiple Threads for each screen, instead of having 2 Threads per screen why not have one that does both downloads, one after the other. 

 

But this http processing should terminate and the Threads should be killed, so even though you are using more Threads than perhaps you need, these Threads should be terminating and so not contributing to the total. 

 

I'm guessing you have another problem.

 

Are you pushing your MainScreens using pushModal.  This will tie up a Thread and not free it until you pop the Screen.  So as you get further down the hierarchy, you will have more Threads tied up managing the modal screens.

 

If you are not doing this, then I strongly suspect a problem with your Thread processing not terminating.  You can check the active Thread count, (Thread.activeCount()).  Monitor this, and check which of your Threads is not terminating.  You do not have to dereference a Thread, and dereferencing it does not terminate it.  Each Thread has to run to the end of its run() method to terminate.  So if you note that the active count is going up, and you don't know why, put 'start and stop debugging statements in all your Threads, one at the start of the run(), one at the end, and see which ones start and don't end. 

 

Please use plain text.
Contributor
omgimanoob
Posts: 26
Registered: ‎06-10-2011
My Carrier: Fledge Simulated Network

Re: Too Many Thread Exception

Hi peter,

 

You are right, the problem is not with caused by http threads, its the timer and timer task. Added these codes solved my problem.

 

 

_GetDataThread = null;
loadingTimer.cancel();
loadingTimer = null;
loadingTask = null;

 

Thanks :smileyhappy:

 

Please use plain text.