简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM