简体   繁体   English

你如何发送回Java中的主线程?

[英]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? 如何从另一个线程发送回Java中的主UI线程? 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. 我正在使用带有Runnable的Executor来完成主UI线程的一些工作,并且我有一个接口,以便可以通过它的监听器通知调用者。

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. 这取决于您正在使用的UI框架。 In general, all UI frameworks all background threads to post events on the event queue which contain callbacks. 通常,所有UI框架都是后台线程,用于在事件队列上发布包含回调的事件。 The main loop in the UI will process these events and invoke the callbacks from the UI thread. UI中的主循环将处理这些事件并从UI线程调用回调。

If you use Swing, then look at SwingUtils.invokeLater 如果您使用Swing,请查看SwingUtils.invokeLater

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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