1

我想为一个类扩展 SwingWorker,但我已经为此扩展了另一个类。我想知道在线程中是否有某种解决方法(我们可以扩展或实现它),以便我可以在 SwingWorker 的 doInBackground 方法中使用。

我有一个需要对其执行操作的对象列表。这些对象的类型为 A、B、C。我创建了一个超类 S,A、B、C 是该超类的子类。我有一个 S 的对象,在运行时遍历列表时,我在列表中找到项目的类型并将 S 类型转换为 A、B 或 C 并执行操作。如果我让 S 成为 SwingWorker,我只能执行一次操作,因为 Swing 工作人员只能使用一次。我不能让 A、B、C 扩展 SwingWorker,因为它们已经在扩展 S。所以我需要为 A、B、C 实现 SwingWorker

我还有一个 A、B、C 将使用的 Connection 对象,并且我将其设置为 Singleton,因为我不想一次又一次地初始化。

4

1 回答 1

2

下面是一个示例组合示例。

预期输出:

收到部分更新列表 [Worker: start]

进度:20%...

进度:70%...

进度:100%...

接收到部分更新列表【Worker:唤醒,Worker:发送结果】

工人完成

启动器完成。

代码:

package com.stackoverflow;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.SwingWorker;

public class DelegatingSwingWorkerDemo {

    public static void main(String[] args) {
        new Launcher().launch();
    }
}

/**
 * Use this, to launch the worker.
 */
class DelegatingSwingWorker<I, O> extends SwingWorker<I, O> {

    SwingWorkerable<I, O> delegate;

    Publisher<O> publisher;

    public DelegatingSwingWorker(SwingWorkerable<I, O> delegate) {
        this.delegate = delegate;

        publisher = new Publisher<O>() {

            @Override
            public void publish(O... chunks) {
                DelegatingSwingWorker.this.publish(chunks);
            }

            @Override
            public void setProgress(int progress) {
                DelegatingSwingWorker.this.setProgress(progress);
            }
        };
    }

    @Override
    protected void process(List<O> chunks) {
        delegate.process(chunks);
    }

    @Override
    protected void done() {
        delegate.done();
    }

    @Override
    protected I doInBackground() throws Exception {
        return delegate.doInBackground(publisher);
    }

}

interface Publisher<O> {

    void publish(O... chunks);

    void setProgress(int progress);
}

/**
 * Make your class implement the interface.
 */
interface SwingWorkerable<I, O> {

    void process(List<O> chunks);

    I doInBackground(Publisher<O> publisher);

    void done();

}

/**
 * Let's say this is your super class:
 */

class MySuperClass {

}

/**
 * Use your super class, but implement the SwingWorkerable. Then
 * launch using a DelegatingSwingWorker
 */
class SleepingDemoSwingWorkerable
    extends MySuperClass
    implements SwingWorkerable<String, String> {

    @Override
    public void process(List<String> chunks) {
        System.out.println("Received partial update list " + chunks);
    }

    @Override
    public String doInBackground(Publisher<String> publisher) {
        publisher.publish("Worker: start");
        try {
            publisher.setProgress(0);
            Thread.sleep(200);
            publisher.setProgress(20);
            Thread.sleep(500);
            publisher.setProgress(70);
            Thread.sleep(300);
            publisher.setProgress(100);
            publisher.publish("Worker: woken up");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        publisher.publish("Worker: sending the result");
        return "Second passed.";
    }

    @Override
    public void done() {
        System.out.println("Worker Done");
    }

}

final class ConsoleProgressListener implements PropertyChangeListener {

    @Override
    public void propertyChange(PropertyChangeEvent event) {
        switch (event.getPropertyName()) {
        case "progress":
            System.out.println("Progress: " + event.getNewValue() + "%...");
        }
    }
}

/**
 * Launch
 */
class Launcher {

    public void launch() {
        SleepingDemoSwingWorkerable workerable = new SleepingDemoSwingWorkerable();
        DelegatingSwingWorker<String, String> delegatingSwingWorker =
                new DelegatingSwingWorker<String, String>(workerable);
        delegatingSwingWorker.addPropertyChangeListener(new ConsoleProgressListener());
        delegatingSwingWorker.execute();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Launcher done.");
    }
}
于 2013-10-01T07:14:35.513 回答