07-01-2010 03:39 PM
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.
07-07-2010 10:56 AM
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