简体   繁体   English

事件调度线程执行

[英]Event Dispatching Thread execution

With Java Swing, is it possible to pause the current thread Runnable and give room to the Event Dispatching Thread to update the gui? 使用Java Swing,是否可以暂停当前线程Runnable并为Event Dispatching Thread提供空间来更新gui?

I know it is possible with multi-threading ( SwingWorker class ) but I was wondering if there's an easier way to achieve this for single threaded programs (aka: all my code is in the gui's Run() ). 我知道有可能使用多线程( SwingWorker class ),但我想知道是否有一种更简单的方法来实现单线程程序(也就是说:我的所有代码都在gui的Run() )。

Eg Matlab has the very convenient drawnow; 例如Matlab有非常方便的drawnow; method 方法

If not: how can I split the updating task to a second thread without having to rewrite anything? 如果不是:如何将更新任务拆分为第二个线程而不必重写任何内容? Would that be the Updating the gui from a running thread from this link ? 那是从这个链接中 运行的线程更新gui吗?

The short answer is no. 最简洁的答案是不。 If you pause the current thread (which according to you is the EDT) then you pause the EDT... 如果您暂停当前线程(根据您是EDT),那么您暂停EDT ...

You can make requests that the UI be updated using methods like repaint , but this also assumes you are not blocking the EDT with things like loops and pauses, as the EDT will need time to process these requests. 您可以使用repaint方法请求更新UI,但这也假设您没有使用循环和暂停等操作阻止EDT,因为EDT需要时间来处理这些请求。

And no, I wouldn't follow the link's advice, as it violates the single thread of Swing by updating the components outside of the of EDT 不,我不会按照链接的建议,因为它违反了Swing的单线程,通过更新EDT之外的组件

Depending on your needs you would either need to use a javax.swing.Timer or SwingWorker . 根据您的需要,您可能需要使用javax.swing.TimerSwingWorker Yes, you can use a Thread , you become responsible for ensuring that all updates to the UI are synced back to the EDT, which the other two suggest provide mechanisms for. 是的,您可以使用Thread ,您负责确保将UI的所有更新同步回EDT,其他两个建议提供机制。

Take a look at Concurrency in Swing for more details 有关更多详细信息,请参阅Swing中的Concurrency

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

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