Java有内置的“反转”吗?摇摆工人

发布于 2024-12-10 05:21:40 字数 641 浏览 0 评论 0原文

SwingWorker 允许您在后台Thread 中准备一些数据,然后在 EDT 中使用它。我正在寻找一个执行相反操作的实用程序:在 EDT 中准备数据,然后将其传递给后台线程。

如果您好奇,用例是将 JTable 的状态保存到磁盘(列顺序、大小等)。我需要在 EDT 中与其模型对话,但不想从此线程写入磁盘。

类似于:

void saveCurrentState(final Table table) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            TableColumns state = dumpState(table);
            saveToDisk(table.getTableKey(), state);
        }
    });
}

dumpState() 需要在 EDT 中运行。 saveToDisk() 不应在 EDT 中运行。整个事情被故意包装在 invokeLater() 中,我无法用 invokeAndWait() 替换它。

SwingWorker lets you prepare some data in a background Thread and then use it in EDT. I am looking for a utility that does the opposite: Prepare data in EDT, and then pass it to a background Thread.

If you're curious, the use case is saving state of a JTable to disk (column order, size, etc.). I need to talk to its model in EDT, but don't want to write to disk from this thread.

Something like:

void saveCurrentState(final Table table) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            TableColumns state = dumpState(table);
            saveToDisk(table.getTableKey(), state);
        }
    });
}

dumpState() needs to run in EDT. saveToDisk() should NOT run in EDT. The whole thing is deliberately wrapped in invokeLater(), and I cannot replace it with invokeAndWait().

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

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

发布评论

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

评论(1

不乱于心 2024-12-17 05:21:40

您可能正在寻找 ExecutorService< /代码>。使用 Executors.newCachedThreadPool() – 或其他 newXXX() 方法,但该方法对于大多数用途来说应该没问题。

也就是说,我相信这也是您可以使用 SwingWorker 完成的事情。只需在创建 SwingWorker 之前在事件处理程序中准备好数据即可 - 您已经在 EDT 中了。如果您需要从非 EDT 线程启动此操作,请使用 SwingUtilities.invokeAndWait() 准备数据,然后分离另一个线程来执行写入操作(或在同一个非 EDT 线程中执行此操作) )

SwingWorkerExecutorService 提供相同的基本服务 - 将一些工作分拆到后台线程中,而无需显式管理其生命周期。 SwingWorker 有一种与 EDT 通信的便捷方法,无需在不需要使用的地方使用 invokeLater(),并且 ExecutorService > 支持 futures 作为与异步任务通信的更通用(非 Swing 特定)方式。对于您的一劳永逸用例来说,两者都很好。

You're probably looking for ExecutorService. Get one using Executors.newCachedThreadPool() – or the other newXXX() methods, but that one should be okay for most uses.

That said, I believe this is something you can do with SwingWorker as well. Just prepare your data in the event handler before you create a SwingWorker – you're already on the EDT there. If you need to initiate this from a non-EDT thread, prepare the data using SwingUtilities.invokeAndWait(), and then spin off another thread to do the writing (or do it in the same non-EDT thread you started with.)

SwingWorker and ExecutorService provide the same basic service – spin off some work into a background thread without having to manage its lifecycle explicitly. SwingWorker has a convenient method of communicating with the EDT without using invokeLater() all over the place that you don't need to use, and ExecutorService supports futures as a more general (non-Swing-specific) way of communicating with asynchronous task. For a fire-and-forget use case as yours both are fine.

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