- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用 SwingWorker
代替 Thread
或 Runnable
有什么优势?
最佳答案
我认为 SwingWorker 的文档还不错:
An abstract class to perform lengthy GUI-interacting tasks in a dedicated thread.
When writing a multi-threaded application using Swing, there are two constraints to keep in mind: (refer to How to Use Threads for more details):
- Time-consuming tasks should not be run on the Event Dispatch Thread. Otherwise the application becomes unresponsive.
- Swing components should be accessed on the Event Dispatch Thread only.
These constraints mean that a GUI application with time intensive computing needs at least two threads: 1) a thread to perform the lengthy task and 2) the Event Dispatch Thread (EDT) for all GUI-related activities. This involves inter-thread communication which can be tricky to implement.
SwingWorker is designed for situations where you need to have a long running task run in a background thread and provide updates to the UI either when done, or while processing. Subclasses of SwingWorker must implement the doInBackground() method to perform the background computation.
当然,您可以使用 Thread、Runtime 和 SwingUtilities (invokeLater
) 来完成此操作,但使用 SwingWorker 类更容易并且可能更不容易出错。
关于java - 为什么选择 SwingWorker?为什么不只是 Thread 或 Runnable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4518534/
我有一个 SwingWorker 如下: public class MainWorker extends SwingWorker(Void, MyObject) { : : } 我从
我正在尝试从另一个 SwingWorker (MainWorker) 执行 SwingWorker (SubWorker),然后我希望 MainWorker 等待 SubWorker 完成。同时,Ma
我试图在 SO 上找到我的问题的答案,但由于它们的丰富性和多样性,我有点困惑。这是我的问题:我的应用程序比较两个文件并在 Swing.JTextPane 中打印结果。我使用按钮调用处理文件的代码,为了
我使用一个扩展 SwingWorker 的类,如下所示: public class Analyzer extends SwingWorker { String source; public A
我有一个作为 Java SwingWorker 运行的套接字服务器。 SwingWorker 接收传入的连接,并在连接后将套接字移交给负责连接的执行器。 Executor 调用将通过 run() 方法
我需要运行两个 SwingWorker。其中一个只能在另一个完成后才能运行。我可以像这样运行它们吗? class TestWorker { private FirstWorker worker
我对SwingWorker有疑问它是done()方法。我有一个通过 SPI 支持插件的应用程序,所以我基本上无法更改插件的行为。这些插件必须实现的接口(interface)包含方法 List getC
jList 模型中包含的数据由 swingworker 更新。问题是我在独立的 *.java 文件中对 SwingWorker 类进行了子类化。所以我无法访问 GUI 组件(在本例中为模型和 jLis
我正在创建一个进度条来监控图像下载。图像下载不起作用 - 它会生成一个大小为 0 字节的文件。如果我将代码移至没有 SwingWorker 的独立类,则图像下载可以正常工作。我已经玩了一段时间了,但我
首先,我需要通知您,我正在尽最大努力学习如何使用 Java 编写代码。这有点困难,但是,我相信我做到了。我过去曾提交过几个关于 SwingWorkers 等的问题。每一个我都以为我拥有它,但后来发现我
假设一个应用程序读取数据文件,从用户获取一些输入(通过 GUI),从大型数据库获取附加数据并执行冗长的分析。 按照建议,我将耗时的部分(数据库管理和实际分析)删除到了 SwingWorker 实例中。
我有几个 SwingWorker 正在运行,它们都需要 OpenNLP 进行计算。OpenNLP 需要一些时间进行初始化,所以我想知道最好的方法是什么?我想在每个 SwingWorker 中启动一个
我有一个小 gui 程序,它在启动时从 Excel 文件中读取数据,其中一些数据需要转到相关的组合框。我知道如何通过为每个组合框使用单独的 SwingWorker 来做到这一点: public cla
有人能告诉我为什么在这个使用 SwingWorker 的简单演示中,屏幕闪烁,好像按钮不断跳跃一样? (关于改进多线程部分的反馈也值得赞赏)。 import java.awt.EventQueue;
在 Netbeans 中,我创建了一个 GUI 项目,该项目使用框架前端,我可以添加组件并双击以编辑其事件。我指的是具有“源”、“设计”和“历史”选项卡的窗口。 以下是组件和关系: 1- 打开文件选择
有没有办法在调用“swingWorker.execute”之前将数据(例如对象)传递给 swingWorker? 最佳答案 通常,我通过使 SwingWorker 的构造函数需要所需的对象来处理此问题
我有这样的代码 try { ServerSocket serverSocket = null; try { serverSocket = new
如何获取代码当前运行的SwingWorker?您可以使用 Thread.currentThread() 来获取 Thread 实例,但我需要 SwingWorker 实例。 代码来自评论 privat
我有一个扩展 SwingWorker 的类。在它的后台线程中,如果成功更改布局并向框架添加新面板,我应该登录。因此 Swing Worker 执行以下 3 项任务: 登录 更改布局 添加/删除面板。
我有一个 swingworker,我想用它从 doInBackground() swingworker 方法中调用另一个对象的大型且复杂的方法。 看起来像这样: public class HostGa
我是一名优秀的程序员,十分优秀!