调度Swingworker线程

调度Swingworker线程

本文介绍了调度Swingworker线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个进程在我的swing应用程序中执行,一个填充列表,一个对列表上的每个元素执行操作。我刚刚将2个进程移动到Swingworker线程来停止GUI锁定,而执行任务,并且因为我将需要对多个列表执行这组操作,因此并发性不会是一个坏主意在第一地点。但是,当我刚刚运行

I have a 2 processes to perform in my swing application, one to fill a list, and one to do operations on each element on the list. I've just moved the 2 processes into Swingworker threads to stop the GUI locking up while the tasks are performed, and because I will need to do this set of operations to several lists, so concurrency wouldn't be a bad idea in the first place. However, when I just ran

doStuffToList线程在空列表(duh ...)上运行。如何告诉第二个进程等待第一个进程完成?我想我可以在第一个进程结束时嵌套第二个进程,但是我dunno,这似乎是不好的做法。

the doStuffToList thread to ran on the empty list (duh...). How do I tell the second process to wait until the first one is done? I suppose I could just nest the second process at the end of the first one, but i dunno, it seems like bad practice.

推荐答案

p>这样的东西会吗?

Something like this would do it, I think?

boolean listIsFull=false;
class FillListWorker extends SwingWorker<Foo,Bar>
{
    ...
    protected void done()
    {
        synchronized (listYouveBeenFilling)
        {
            listIsFull=true;
            listYouveBeenFilling.notifyAll();
        }
    }
    ...
}

class DoStuffToListListWorker extends SwingWorker<Foo,Bar>
{
    ...
    protected Foo doInBackground()
    {
        synchronized (listYouveBeenFilling)
        {
            while (!listIsFull)
            {
                try
                {
                    listYouveBeenFilling.wait();
                }
                catch (InterruptedException ie)
                {
                    // Don't worry, we'll just wait again
                }
            }
        }
    }
    ...
}

这篇关于调度Swingworker线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:48