09-23-2008 06:56 PM
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.
09-23-2008 07:02 PM
Christian, meet the Knowledge Base. Knowledge Base, this is Christian.
09-23-2008 07:36 PM
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
09-24-2008 06:05 AM
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.
09-24-2008 08:09 AM
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/
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).
09-24-2008 10:17 AM
Here is a KB article on the subject:
09-24-2008 11:35 AM
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
09-24-2008 11:54 AM
http://java.sun.com/javame/reference/apis/jsr139/j
If that doesn't fix it, look for any other suspicious references to your zombie-wannabe-dead-thread.
09-26-2008 10:10 AM
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.
09-26-2008 12:18 PM
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.