views:

104

answers:

2

I'm just starting with J2ME and lcdui, and I'm looking at some sample code that calls methods on lcdui objects from a worker thread.

In my experience with desktop GUI toolkits, this is usually forbidden - is lcdui different? Is it really OK to do this?

(I've Googled for an answer to this question but not found anything - a link to a defintive answer in some official documentation would be excellent!)

+3  A: 

LCDUI is a bit of a funny one, what you can and can't do often depends on the implementation. I've written apps for BlackBerry that don't have a problem with accessing UI objects from a background thread (except the usual threading problems that you create yourself), but I'm pretty sure some other platforms will forbid this.

If you're concerned about this, or it's causing you issues, you might want to look at using javax.microedition.lcdui.Display.callSerially(Runnable). This executes the given Runnable object in the UI thread (if there is such a thing in LCDUI) and serializes it with other UI events and paint operations. You can read more about it in the J2ME API docs.

roryf
Thanks! `callSerially()` puts my mind at rest. :-)
RichieHindle
+1  A: 

Using the javax.microedition.lcdui classes, thread-safety is supposedly one of the goals of the UI classes according to the Concurrency section of this documentation. As Rory indicated, it is entirely possible that different vendors implemented this as more of a "suggestion" rather than a rule.

At one time, I was looking for similar information, but was also unable to find the magic phrasing to offer Google to get good results.

Best of luck!

monceaux
Thanks for the link to the documentation; I agree that I wouldn't necessarily trust it on all platforms.
RichieHindle