多次执行SwingWorker

发布于 2024-09-26 15:17:12 字数 212 浏览 0 评论 0原文

我希望能够多次使用 SwingWorker 子类。这可能吗?

我读过java文档:

SwingWorker 设计为仅执行一次。多次执行 SwingWorker 不会导致调用 doInBackground 方法两次。

I want to be able to use SwingWorker subclass multiple times. Is this possible?

I have read in the java doc:

SwingWorker is only designed to be executed once. Executing a SwingWorker more than once will not result in invoking the doInBackground method twice.

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

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

发布评论

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

评论(4

扶醉桌前 2024-10-03 15:17:12

实现 SwingWorker 的类的一个实例确实只能运行一次。根据需要实例化任意数量的实例并运行它们没有限制。

One instance of a class implementing SwingWorker can be indeed ran only once. There are no limitations on instantiating as many instances as you need and running them.

俏︾媚 2024-10-03 15:17:12

您无法实例化所需数量的实例并执行它们。在 SwingWorker 类中存在 javax.swing.SwingWorker.MAX_WORKER_THREADS = 10。因此您最多可以执行 10 个实例。实例只有在空闲时间达到 10 分钟才会被释放。不要将 SwingWorker 实例用作 Thread 实例;它不是线程。

You cannot instantiate as many instances you need and execute them. In SwingWorker class exists javax.swing.SwingWorker.MAX_WORKER_THREADS = 10. So you can execute a maximum 10 instances. An instance is freed only it spend 10 minutes in idle time. Do not use SwingWorker instance as a Thread instance;it is not a thread.

各自安好 2024-10-03 15:17:12

尝试这样做:

  private void startTask() {// main method that creates the background task or class that implements the SwingWorker

    AppContext  appContext = AppContext.getAppContext();
    if(appContext!=null){
        appContext.remove(SwingWorker.class); 
    } 
    MassiveMigrationTask task = new MassiveMigrationTask();// class that implements the SwingWorker
    task.execute();// this process implicitly adds the SwingWorker.class to the appContext
    }

正如描述:“AppContext 是 ThreadGroup 引用的表,它存储应用程序服务实例。”

所以这个问题的发生基本上是因为 AppContext 正在保存名为 SwingWorker 的线程的名称...,所以如果您尝试创建该线程的另一个实例,您可能不会成功,因为它在执行新的线程之前会评估该线程名称一个或至少将 new 放入要执行的线程堆中,请随意查阅此处的代码:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/ awt/AppContext.java

PS:重要提示:“如果您不是在编写应用程序服务,或者不知道应用程序服务是什么,请不要使用此类”

Try doing this:

  private void startTask() {// main method that creates the background task or class that implements the SwingWorker

    AppContext  appContext = AppContext.getAppContext();
    if(appContext!=null){
        appContext.remove(SwingWorker.class); 
    } 
    MassiveMigrationTask task = new MassiveMigrationTask();// class that implements the SwingWorker
    task.execute();// this process implicitly adds the SwingWorker.class to the appContext
    }

As the description: "The AppContext is a table referenced by ThreadGroup which stores application service instances."

So this issue is happening basically because the AppContext is saving the name of the thread called SwingWorker..., so if you try creating another instance of the thread you'll probably have no success, because it evaluates that thread name before executing a new one or at least put the new in the heap of threads to be executed, feel free consulting the code here:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/awt/AppContext.java

PS: Important :"If you are not writing an application service, or don't know what one is, please do not use this class"

小巷里的女流氓 2024-10-03 15:17:12

您可以从 done() 方法再次调用您的 Swing Worker (取决于您的代码设计方式)

MySwingWorker worker = new MySwingWorker();
worker.setData(this.getData());
worker.execute();

考虑 setData()getData()

它是我只是使用简单的哈希图将输入数据传递给我的 Swing Worker,因此为了重新运行它,我只是将相同的输入传递给 Swing Worker 的新实例

You can just call again your swing worker from done() method (depend how your code desinged)

MySwingWorker worker = new MySwingWorker();
worker.setData(this.getData());
worker.execute();

Regard setData() and getData()

It's just simple hashmap i'm using to pass the inputs data to my swing worker, so for rerun it, i just passed the same inputs to the new instance of swing worker

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