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
Highlighted
New Developer
Posts: 14
Registered: ‎08-14-2009
My Device: Not Specified

Kill all threads on application exit

My application creates many threads at various point of time.

Normally, in Nokia when application exits, it destroys all threads.

But I think BlackBerry doesn't. Thats because I am getting TooManyThreadsException if I exit software and start it again 5-6 times.

I wish to kill all threads on BlackBerry when my application exits.

At that time, threads might be waiting for some object, doing IO etc.

Please tell how to do this

 

Thanks in advance

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Kill all threads on application exit

Before exting the application stop all the runnung thread.

I mean something like:

 

thread1.stop(); thread2.stop(); ...

 

Regards

Bikas

 

 

New Developer
Posts: 14
Registered: ‎08-14-2009
My Device: Not Specified

Re: Kill all threads on application exit

But for that I have to keep track of all threads which are created.

Isn't there a single API to kill all threads?

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Kill all threads on application exit

If a thread is waiting on I/O (or deadlocked, or whatever) when you call exit(), I don't know what happens, but I'd guess that you're right--the thread just hangs around. The easiest way to end a waiting thread is to call interrupt(). If a thread is busy calculating pi to the last digit, it should be written to check a "please quit" variable somewhere that can be set externally. Unfortunately, there is no Thread.stop() method on BB.

 

Since there are no thread groups or daemon threads on BB devices, you might have to write your own thread manager to do all this. The manager would be told when threads are created, when they finish, and when the app is going to exit. For the last, it would go through the list of threads and calls interrupt() on each one that's still alive. This will work better if you write all your thread code to cooperate with the manager (for instance, to register itself with the manager and to notify it when the thread finishes).

 

If you have threads being spawned inside code you don't control, I don't have any good suggestions.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Kill all threads on application exit

[ Edited ]
What proof do you actually have that, on BlackBerry, threads aren't killed when an application is terminated? I recall seeing the following. When an application is terminated, its threads are terminated even without finally blocks in the Thread/Runnable.run method being invoked.
Message Edited by klyubin on 08-27-2009 09:07 AM
Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Kill all threads on application exit

i agree with klyubin. an application runs in its own JVM instance, system.exit terminates this instance.
maybe you have some strong references that hinder the application from being terminated correctly? or the toomanythreads error has another source.
----------------------------------------------------------
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
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Kill all threads on application exit

No, as far as I know, an application does not run its own JVM instance. If that were the case, applications wouldn't be able to share objects by reference.
New Developer
Posts: 14
Registered: ‎08-14-2009
My Device: Not Specified

Re: Kill all threads on application exit

I am not sure what is this concept of "background" threads in blackberry and its difference with normal threads but page 12 of this document says:

http://blizzard.cs.uwaterloo.ca/eaoliver/blackberry_tutorial/BlackBerry%20JDE%204.6.0%20Fundamentals...

Multithreading

The BlackBerry® operating system is a multithreaded operating system, which means that many applications and processes can
run actively on the BlackBerry device at the same time. For example, applications can use background threads to manage
processor-intensive tasks or network communications so that they do not affect the main thread. If an application creates
background threads, and a BlackBerry device user closes the application, the background threads can remain active.

 

If all threads are automatically destroyed when application is exited then may you answer:

1. Why I get TooManyThreadsException if I start my application and close it around 5 times.

2. If I OTA install my application it gets installed successfully. Now if OTA install again, it asks for replacement and gets installed successfully without any reboot. But in between successive installation, if I start and exit application then next installation demands reboot. Why?

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Kill all threads on application exit

New Developer
Posts: 14
Registered: ‎08-14-2009
My Device: Not Specified

Re: Kill all threads on application exit

[ Edited ]

I have found that if I don't use PushRegistry then everything works fine.

But if I use:

                String MIDletRegistered = PushRegistry.getMIDlet("sms://:16001");
                String RegisteredConnections[] = PushRegistry.listConnections(false);
                if(RegisteredConnections == null){
                    RegisteredConnections = new String[0];
                }
                if((MIDletRegistered == null) && (RegisteredConnections.length == 0)){
                    PushRegistry.registerConnection("sms://:16001", "MyApp1.MIDletClassMain", "*");
                }
and then if I start application & close say 5 times then I get TooManyThreadsException

Why so???

 

Thanks in advance

Message Edited by arpit2agrawal on 08-30-2009 08:13 PM