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
New Developer
cristianjss
Posts: 12
Registered: ‎09-23-2008
My Device: Dev Alpha B

tooManyThreads Error

Hi, I´m working with Threads, the application works fine at the beginning, but when I leave my application working for a long time it start to send me a TooManyThread Error, since I´m trying to create a new thread, I´m wondering if any one can give me any advice, so I can come up with me problem or if there exist an instruction to see all threads created by my application. I´m trying to use System.gc(), but I seems to do nothing.

 

Thanks, Cristian.

Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: tooManyThreads Error

Christian, meet the Knowledge Base.  Knowledge Base, this is Christian.

New Developer
cristianjss
Posts: 12
Registered: ‎09-23-2008
My Device: Dev Alpha B

Re: tooManyThreads Error

Thanks richard, I have had read that paper before write my question, but I´m looking for some ideas about how can I handle this error, or how can I know the number of threads created by my application, and the number of thread on the device at some point.

 

 

Cristian

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: tooManyThreads Error

Some random thoughts:

 

"how can I handle this error" - don't start so many threads, or make sure that you stop the ones that you are no longer using.  I'm not being silly here, I'm saying you need to think about this when designing your application.  An application that creates new Threads need to think about what happens to the ones it has previously started.

 

"know the number of threads created by my application" - count them as you create them?  Alternatively you can try Thread.activeCount().  But there is little difference between counting them and getting the Exception - either way you have to process a situation where you can't start a Thread, so the design principles are the same. 

 

"the number of thread on the device" - I can't find anything in the API that will help with this, and to be honest, I wouldn't expect to.

Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: tooManyThreads Error

It is not unreasonable to want to enumerate and find all the objects that can consume system resources-

be it threads, IO, or any GUI/screen hardware( certainly on Windoze I've had a problem figuring out who

is taking all my GDI objects for example).

 

On J2Se, there is a concept of a ThreadGroup, but some of the "obvious" features have been deprecated in any case,  

 

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ThreadGroup.html

 

The threads you start yourself you need to track. There are only two ways to create threads- Thread ctor and subclasses.

You should be able to grep your code for threads and store your objects in a vector or other container for later interrogation.

I suppose you could be launching other people's code ( system or library things ) that create private threads but you can probably

find a short list of suspects if this is the case.

 

So far in the one app I have, I have many places where I launch threads and haven't run into a limit on the 7130. When you write your own, you end up with something like

 

void run ()

{

 while ( something)

{

 

}

}

 

You need to make sure that "something" has a finite lifetime and that no one in the loop can hang- IO, wait, sleep can hang unless

aborted. Adding an abort flag in "something" helps too. If you see my other post, things like "synchronized" can hang and you need to check for things like deadlock ( do you have synchronized everwhere just to be safe? Brain surgery doesn't become safe just because you do it real slow, LOL).

 

 

 

 

 

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: tooManyThreads Error

New Developer
cristianjss
Posts: 12
Registered: ‎09-23-2008
My Device: Dev Alpha B

Re: tooManyThreads Error

Thanks, to everyone, but I´m still having issues, since the threads I´m creating are not destroyed by the JVM, when I ask if the thread isAlive() it returns FALSE, buy when I try the reference from the same object it is not null, so I try to use the System.gc() function, but it doesn´t seems to work as it should be..

 

 

Cristian

Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: tooManyThreads Error

http://java.sun.com/javame/reference/apis/jsr139/java/lang/ref/WeakReference.html

 

If that doesn't fix it, look for any other suspicious references to your zombie-wannabe-dead-thread.

 

 

Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: tooManyThreads Error

Did you ever find out what this is? I'm kind of curious. While it is probably just something simple, maybe including only

your one strong reference,  it may be

good for everyone to know about it as it sounds like a spurious retained resource, not a problem with an infinite

loop likely to be specific to your code.

 

Thanks.

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: tooManyThreads Error

Just done some testing (on 4.2.1 Simulator) as follows:

1)  started and stopped a number of Threads, but retained a reference to them.  So isAlive returned false, but he Threads still existed.  It didn't matter how many I tried, this did not cause the exception.

2) started but did not stop each thread.  After starting 14, clicking the menu button caused a "too many threads Exception" while processing a menu.show() which pushes a modal screen. 

 

So it would seem to me that Threads must be alive to contribute to the Exception.

 

BTW, Thread.activeCount() gave me a count that was 1 more than the number of Threads that I had started and hadn't stopped.  This was a simple screen application.

 

Hope this helps.