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
Trusted Contributor
Posts: 154
Registered: ‎02-15-2011
My Device: Not Specified

difference between invokelater and implementing Runnable when creating a new Thread

When creating a new thread in order to not hold up the event dispatcher it is required to spawn threads (I could be corrected on this statement).

 

Is there a difference beween using invokeLater and using the pure "java" way of creating a new thread, i.e implementing Runnable interface.

 

Thanks

Retired
Posts: 172
Registered: ‎06-07-2010
My Device: BlackBerry 8520

Re: difference between invokelater and implementing Runnable when creating a new Thread

Hello Andrian,

 

How are you doing? I hope all is well.

 

I hope you have found the answers to your question, but if not:

 

The main difference between invokeLater(new Runnable) and new Runnable is that invokeLater puts an action into the event queue. So that whenever the main event tasks are completed, the new Runnable will get processed.

 

A new Runnable() is an interface that will be executed whenever its called regardless if its on the main event thread. You can create a new thread to put a runnable in so that a new thread can be spawned.

 

For example:

 

Thread th = new Thread(myRunnable);

th.start();

 

spawns off a new thread.

 

 

Let me know if you have any further questions or doubts, and I will be glad to answer them for you.

 

Sincerely,

 

Raihan Masroor
BlackBerry Development Advisor
Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: difference between invokelater and implementing Runnable when creating a new Thread

Just adding a little to the previous answer, which I hope helps.

 

invokeLater does not create a new Thread,  it just schedules a Runnable to be executed on the Event Thread.  This is the correct approach if you want to update the UI, since that needs to run on the Event Thread (or to be completely accurate, needs to run with the Event Lock). 

 

However if you have some long running background processing, or some blocking operation (like network interaction), then you really want to run this in your own Thread,- if you try to use the Event Thread, you will be blocking the User interactions.

 

So the two are not different ways to do the same thing.  You will choose one way or the other depending on what you are trying to achieve. 

 

Review this for more informaiton:

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

 

Do a search for Event Thread - there are other interesting KB articles too.