java swing工作线程等待EDT

发布于 2024-09-08 06:41:38 字数 75 浏览 2 评论 0原文

我有一个工作线程,应该等待 EDT 更新 GUI,然后再继续执行。我已使用发布方法告诉 EDT 进行更改。我怎样才能让工人等待变化发生?

I've got a worker thread that should wait for EDT to update the GUI before continuing execution. I've used the publish method to tell EDT to change something. How can i make the worker wait for that change to take place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

跨年 2024-09-15 06:41:38

我假设您正在使用 SwingWorker 来发布您的结果。您可以使用布尔标志来指示该值已被处理。该标志在发布中间结果之前被清除,然后用于阻塞线程直到它被设置。 UI 线程在完成处理已发布数据时设置该标志。

class MyWorker extends SwingWorker<K,V>
{
    boolean processed = true;

    protected void doInBackground() {
        for (;;) {
            setProcessed(false);
            publish(results);
            waitProcessed(true);
        }
    }

    synchronized void waitProcessed(boolean b) {
        while (processed!=b) {
           wait();
        }
        // catch interrupted exception etc.
    }

    synchronized void setProcessed(boolean b) {
        processed = b;
        notifyAll();
    }


    protected void process(List<V> results) {
       doStuff();
       setProcessed(true);
    }
}

I'm assuming you are using SwingWorker to publish your results. You can use a boolean flag to indicate that the value has been processed. This flag is cleared before publishing intermediate results, and then used to block the thread until it is set. The UI thread sets the flag when it as completed processing the published data.

class MyWorker extends SwingWorker<K,V>
{
    boolean processed = true;

    protected void doInBackground() {
        for (;;) {
            setProcessed(false);
            publish(results);
            waitProcessed(true);
        }
    }

    synchronized void waitProcessed(boolean b) {
        while (processed!=b) {
           wait();
        }
        // catch interrupted exception etc.
    }

    synchronized void setProcessed(boolean b) {
        processed = b;
        notifyAll();
    }


    protected void process(List<V> results) {
       doStuff();
       setProcessed(true);
    }
}
水晶透心 2024-09-15 06:41:38

如果也是您启动 GUI 更改的同一个工作线程,则有一个现成的机制用于等待进行这些更改:

SwingUtilities.invokeAndWait()

应该很符合要求。

另一种选择是使用 SwingUtilities.invokeLater() 为 EDT 提供一些要运行的代码,一旦 EDT 空闲(即当它开始运行此任务时),该代码将唤醒您的线程。这将涉及立即触发 invokeLater(),然后执行 wait(),并希望在执行 之前不会发生 EDI 唤醒。等待()。不过,就时间安排而言,这并不是万无一失的。

If it is also your same worker thread that is initiating the GUI changes, then there's a ready-built mechanism for waiting for those changes to be made:

SwingUtilities.invokeAndWait()

should fit the bill nicely.

Another alternative would be to use SwingUtilities.invokeLater() to give the EDT some code to run which will wake your thread up once the EDT becomes idle, i.e. when it gets around to running this task. This would involve firing off invokeLater() immediately followed by a wait() and hoping that the wake-up from the EDI doesn't happen before you do the wait(). This isn't quite foolproof with respect to timing, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文