两名工人不会立刻跑步

时间:2015-03-04 08:21:26

标签: java swingworker

只有在我不打电话的情况下,SwingWorkers才能工作。

public class JavaApplication89
{
    public static void main(String[] args)
    {
        Worker1 w1=new Worker1();
        Worker2 w2=new Worker2();
        try
        {w1.execute(); w2.doInBackground();}
        catch(Exception e){}
    }
}

如果我在两者上调用execute或在两者上调用doInBackground,则没有任何反应。

这里是Worker1和Worker2的代码:

class Worker extends SwingWorker<Void,Void>
{
    protected Void doInBackground() throws Exception
    {
        for(int i=1;i<=1000;i++)
        {
            System.out.println(i);
            try
            {sleep(1000);}
            catch (InterruptedException ex){}
        }
        return null;
    }
}

如果我放弃Worker1和Worker2并只写一个名为Worker的类,输出不会同时出现,第二个输出等待第一个输出。 你能解释一下我的问题吗?

0 个答案:

没有答案
相关问题