Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 506
user3842231 Avatar asked Dec 03 '25 01:12

user3842231


1 Answers

That depends on the UI framework which you're using. In general, all UI frameworks all background threads to post events on the event queue which contain callbacks. The main loop in the UI will process these events and invoke the callbacks from the UI thread.

If you use Swing, then look at SwingUtils.invokeLater

like image 112
Aaron Digulla Avatar answered Dec 04 '25 18:12

Aaron Digulla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!