Interacting with UI threads in Java/J2ME

I'm writing a J2ME application. One of the pieces is something that polls the contents of a directory periodically, and, if there are any new things, paints them on the screen. I've done this by having the UI form launch a polling thread with a pointer back to itself, and when the polling thread finds something it calls back to the form and calls a syncrhonized method to update it's display. This seems to work fine.

The question I have is this. In C#/.NET I know it is not nice to have non-UI threads updating the UI, and the correct way to handle this is to delegate it up to the UI thread.

E.g. the following:

public void DoSomeUIThing()
    if (this.uiComponent.InvokeRequired)
        this.uiComponent.Text = "This is the update I want to happen";

Is there a J2ME equivalent for how to manage this process? How about Java? Or does Java/J2ME just play nicer in regard to this? If not, how is this done?

[EDIT] It appears that Swing supports what I'm asking about via the SwingUtilities.invokeLater() and invokeAndWait() methods. Is there an equivalent framework for J2ME?

Asked by: Emily578 | Posted: 21-01-2022

Answer 1

Regarding Java, what you are describing looks like a SwingWorker (worker thread).

When a Swing program needs to execute a long-running task, it usually uses one of the worker threads, also known as the background threads.

A Swing program includes the following kinds of threads:

  • Initial threads, the threads that execute initial application code.
  • The event dispatch thread, where all event-handling code is executed. Most code that interacts with the Swing framework must also execute on this thread.
  • Worker threads, also known as background threads, where time-consuming background tasks are executed.

Single-thread rule:
Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread.

When used in a J2EE context, you need to be careful when you are referencing a SwingWorker from an EJB.

Regarding J2ME, it depends if you are developing your application as a standard MIDlet that will run on any MIDP-enabled device, or for instance as a RIMlet, a CLDC-based application that uses BlackBerry-specific APIs and therefore will run only on BlackBerry devices.

Because unlike MIDP's UI classes, RIM's are similar to Swing in the sense that UI operations occur on the event thread, which is not thread-safe as in MIDP. To run code on the event thread, an application must obtain a lock on the event object, or use invokeLater() or invokeAndWait() รขโ‚ฌโ€œ extra work for the developer, but sophistication comes with a price tag.

But for LCDUI, you can access a form from multiple threads.

Answered by: Brooke995 | Posted: 22-02-2022

Answer 2

There are many profiles of Java ME. If you mean MIDP then Display.runSerially is what you want.

For AWT (Swing) you would use EventQueue.invokeLater (SwingUtilities.invokeLater is only necessary due to Java 1.1 not having the EventQueue method - 1.2 is about to celebrate its tenth birthday). For the Common DOM API, use DOMService.invokeLater.

No matter what claims a GUI API may make about thread-safety they are probably wrong (some of the claims of Swing are removed in JDK7 because they are not implementable). In any case, application code unlikely to be thread-safe.

Answered by: Elise725 | Posted: 22-02-2022

Answer 3

For j2me apps you probably want to keep it simple. The main thing is to touch UI components only in the event thread. The direct way of doing this is to use invokeLater or invokeAndWait. Depending on your libraries you won't have access to anything more than that. In general if these aren't provided in your platform it probably equates to there being no thread support and not being an issue. For example the blackberry does support it.

Answered by: Joyce649 | Posted: 22-02-2022

Answer 4

If you develop under SWT this is accomplished by means of asyncExec() method of Display object. You pass an object implementing Runnable so the UI thread executes the changes done in other thread.

This is an example borrowed from here

public void itemRemoved(final ModelEvent me)
   final TableViewer tableViewer = this.viewer;

   if (tableViewer != null)
      display.asyncExec(new Runnable()
         public void run()

Answered by: Fenton451 | Posted: 22-02-2022

Answer 5

I can attest that the MIDP UI toolkit is indeed thread-safe, as I have large MIDlets with complex GUI running on millions of phones made by almost every manufacturer, and I have never seen a problem in that regard.

Answered by: Walter228 | Posted: 22-02-2022

Similar questions

java - How can I run servlet without user interacting

I want to excute a servlet to get server name,server port and ContextPath from request. But I don't want to invoke servlet by user interacting. I want excute this servlet by Java code. I'm not sure it's possible. Please give me recommendation.

java - The buttons are working but they are interacting with the compiler not the gui?

This question already has an answer here:

java - Interacting with the hibernate Session during event calls

I want to write a history component that tracks changes to a particular object type and writes history rows based on the difference. Note this is not a general audit system it is specific for one object type. I figure I can hook into the hibernate event model and listen for events that tells me when things have changed, watch for any changes to instances of the object type I am interested in, and then have some cu...

Interacting with java code from C#

We've written a Java program which we are looking to use and interact with from C#. What are our options? Optimally it would be possible to compile the Java application as a library (.DLL) that we could reference from C# perhaps using P/Invoke. This, however, doesn't appear to be an option according to the first few searches online. We opt to be able to use ASP.NET to built a search engine powered by the Java code,...

java - Interacting with objects managed by DWR from JSP

I'm working with Direct Web Remoting (DWR) as part of a 'plain' dynamic Java project (hosted on a Tomcat 6.0 server with Java 6). I have a simple JSP page which interacts with session-level variables within my user's session. However, I also have a session-level object managed by DWR via the 'new' constructor. Is it possible to, from within my JSP, retrieve and interact with the object managed by DWR? This isn't ...

class - Java classes not interacting properly

I have two Java class files: primegen and primecheck sitting in the same directory. primegen calls a public static function from primecheck. primecheck compiles fine. However, I receive the following compilation error in primegen: cannot find symbol symbol : variable primecheck location: class primegen } while (!prime...

javascript - Displaying and interacting with HTML within Java

I have a problem which I am currently trying to wrap my head around, and any advice or nods in a good direction would be greatly appreciated. I want to display a Google Map within my Java Swing project, (the map will be a URL specified within an HTML document I think). I also want to be able to communicate and interact with the map using JavaScript, injected via buttons in java swing, etc. ...

java - Injecting a class into the JVM and interacting with existing classes

I want to inject my Java class into an existing Java application, on Windows. I found an article describing a method using CreateRemoteThread - java-code-injection-via-winapis But it's not clear if the injected class can 'connect' with the existing classes and call them. Does anybod...

java - 2 .jar files not interacting with each other

I have recently been adding an updater to my Java programm and all the code is working. The problem I am having however is that the initial program doesn't go on to run the second .jar file or even run the file in NetBeans and just shuts down instead. I believe this bit of code to be the thing that isn't working but I have done a tonne of System.out's and it just runs straight through the code without running the ...

Spring / Java, good method for remotely interacting with command line Java app on another server?

I am working on a Spring web application where I have a need to interact with a remotely based command-line java application to run a simple search query on this application and get back the results. I initially had integrated this into my Spring app but my app is, itself, needing a lot of memory (its an app that involves huge amounts of data) and I don't think they can coexist on one server anymore. I am running e...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)