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
Regular Contributor
Posts: 64
Registered: ‎02-03-2010
My Device: Bold
My Carrier: Orange

Running out of threads and InvokeLater

Hi

 

I have alot of periodic threads in my function, which i call using 

InvokeLater, and cancelInvokeLater once i don't need them. I usually don't have more than 5-6 concurrent such threads - Problem is that sometimes ( rarely ), i get -1 in the invokeLater call. Checks led me to conclude that i can make at most 17 threads on my devices, (8900, 9000, 9700, 82**) - I am surely not using that many. 

 

Does anyone else have any idea regarding what could be causing me to have so many few threads sometimes? Specifically, is the 17 threads a global number, i.e. has to split between the different application, and thus a background application could be lowering my free thread num? Or maybe could some native process, such as networking, GPS, etc, be using threads from the thread pool without me calling invokeLater? 

 

BTW, I know i can use a single Timer and TimerTasks, but i would prefer having a different thread for each task, and also would like to understand why this problem happens.

 

Thanks,

Dan.

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Running out of threads and InvokeLater

the number of threads allowed on a device is not related to the event queue. if you use invokelater you are putting your code into the event queue.

 

you should not exectute any blocking code, like networking, gps etc, on the event thread.

use a separate thread for that.

----------------------------------------------------------
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: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Running out of threads and InvokeLater

AFAIK, the limit on the number of Threads for an Application is 16.  Given that things like pushModal require a Thread, I would try to limit the number of Threads that any Application uses to about 12. 

 

However, like Simon, from your description, I am not sure how you are using Threads.  Are you using invokeLater to schedule repeated activities?  Do these activities need to be running on the Event Thread?  Can you give us an example of what you are doing? 

Highlighted
Regular Contributor
Posts: 64
Registered: ‎02-03-2010
My Device: Bold
My Carrier: Orange

Re: Running out of threads and InvokeLater

 

Thanks for the reply simon and Peter. 
I am indeed calling this usually for periodic calls ( Such as periodic network calls, screen redraw calls, etc.. ). 
This is how i make the calls ( This is a wrapper function, that eventually calls a certain callback which is supposed to run periodically. You can ignore the parts which are in the
run() function )  : 
public static int NOPH_FreemapApp_invokeLater(final int index, final int interval, int repeat)
{
	net.rim.device.api.system.Application app = net.rim.device.api.system.Application.getApplication();
	return app.invokeLater(
		new Runnable() 
		{ 
			public void run()
			{
				try
				{
					if (c_on_invokeLater == 0) {
						c_on_invokeLater = CibylCallTable.getAddressByName("rim_on_invokeLater");
					}
				}
				catch (Throwable t)
				{
					System.out.println("Exception in run (invokeLater): " + t);
					t.printStackTrace();

					System.exit(0);
				}

				if (c_on_invokeLater != 0)
				{
					UIWorker.addUIEvent(c_on_invokeLater, index, 0, 0, 0, interval < 100 ? true : false);
				}
			}
		}, interval, repeat != 0);
}

 I do not use pushModal almost at all - I'm still pretty sure there's no way 

i am getting even to 12 threads. I have searched but haven't found a way to find out the amount of free threads ( do you know of one? ), too bad there isn't such a way, as it would very much help in debugging the situation. (Or maybe if i could somehow get a list of the threads which are being used and a unique id of them, that would have even been better ). 

 

Anyway, regarding Simon's reply - the usage is a bit more complicated, but basically i don't need the result to be on the event thread,i'm aware that using TimerTask would have probably been better ( but i did not write that code :\ )

 

 

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Running out of threads and InvokeLater

for one, timertask would not flood the event thread with stuff that is not required there.

and second: the event queue is NOT a threadpool!

networking and other blocking operations have to be done in an own thread.

invokelater does not create an own thread, it executes the run command on the event thread.

----------------------------------------------------------
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
Regular Contributor
Posts: 64
Registered: ‎02-03-2010
My Device: Bold
My Carrier: Orange

Re: Running out of threads and InvokeLater

I see what you're saying. In documentation of the invokeLater it says 

 

"Note that you should make sparing use of this this method, as each application has a limited number of timers. If this application currently has no timers available, this method returns -1".

 

So is the 16\17 limit is arbitrary and is set by the implementation of invokeLater ? Or would the limit of 16 timers would still be the same if i used TimerTask, and then i would still run into the same problems maybe?  ( I will run some checks later to see what is the behavior ). 

 

And regarding your comment on threading - I actually DON'T hog the event queue - each time an invokeLater run() function is called, i insert an event into another thread which performs all the blocking operations. ( And i have another network thread which is called by the second thread and does not block both the event thread and the second thread ). 

So i hog very little ( just a few lines of code, which i posted before ) of the event queue. Not optimal logically,  but then again pretty negligible...

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Running out of threads and InvokeLater

i never got an actual value about the things allowed on the event queue. you can test how many things you can queue until you get -1

----------------------------------------------------------
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: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Running out of threads and InvokeLater

[ Edited ]

While testing, how about tracking

Thread.activeCount();

 

Also I'm not sure about this statement Simon:

"invokelater does not create an own thread, it executes the run command on the event thread."

 

I suspect that behind the scenes, when you use invokeLater as is done here, it does actually create a Timer and Timertask, and as the documentation for Timer says:

"Corresponding to each Timer object is a single background thread"

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: Running out of threads and InvokeLater

The limit does not correspon to TimerTasks, but Timers. A Timer executes all of its tasks on the same thread.

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Running out of threads and InvokeLater

 


CMY wrote:

The limit does not correspon to TimerTasks, but Timers. A Timer executes all of its tasks on the same thread.


 

Precisely... but each invokeLater with 3 arguments (Runnable, long, boolean) creates its own Timer.  That's why its return value of -1 means that "there are no timers available".

 

To the original poster: if there are things you'd rather have on separate Timers, determine which ones can share one Timer, group them, create a Timer for each group at startup and schedule each activity on the appropriate Timer.  For example:

1.  Timer for networking (one such timer per server if you are using more than one).

2.  Timer for SDCard operations.

3.  Timer for user alerts (TimerTasks in that one will probably have invokeLater(Runnable) calls in their run() methods).

 

etc.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!