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

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

Re: Running out of threads and InvokeLater

BlackBerry devices have a limit of 16 threads per application and 128 threads per device. Each Timer uses a thread. Each call to invokeLater(Runnable, int, boolean) uses a Timer. (There's also a limit of 16 Timer objects per application, but I don't know if that's a separate limit or a consequence of the thread limit.)


I'd suggest replacing your calls to invokeLater(Runnable, int, boolean) with TimerTasks that are handled by a single Timer that you create. Each TimerTask can call invokeLater(Runnable) when the task itself runs. (Calls to invokeLater(Runnable) do not use a Timer.)


Looking at the example, unless UIWorker.addUIEvent() needs to execute on the event thread (and the name suggests that it does not), it appears (like Peter suggested earlier) that you are using invokeLater as a timing mechanism. Using your own Timer is a cleaner (and less risky) way to do that.


The only thing you need to watch out for with your own Timer is to explicitly call cancel() when it is no longer needed; otherwise, the timer thread can continue running indefinitely and prevent your application from shutting down.

Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Regular Contributor
Posts: 64
Registered: ‎02-03-2010
My Device: Bold
My Carrier: Orange

Re: Running out of threads and InvokeLater

I changed to TimerTask following your advices. 

Seems to be working okay so far, let's hope this fixes the crashes that used to happen once in a while.


Thanks again everyone