How do you post back to the main thread in Java?

How do you post back from another thread to the main UI thread in Java? I am using an Executor with a Runnable to do some work from the main UI thread and I have an interface so that the caller can be notified through it to the listener.

However, obviously without being sent back to the main thread, the callbacks come from the worker thread. How do I send / disable the callback back to the main thread so that when my listener is notified it can process the results on the main thread?

+3


source to share


1 answer


It depends on the UI structure you are using. In general, all UIs on all background threads publish events to an event queue that contains callbacks. The main loop in the UI will handle these events and call callbacks from the UI thread.



If you are using Swing take a look SwingUtils.invokeLater

+4


source







All Articles