我正在使用一些多线程应用程序进行java swing。情况是这样的,我需要一个接一个地执行一个线程,即一个输出与第二个相关。我做了以下。
// Class one
public class Samp implements Runnable{
public Samp(String name) {
this.setName = name;
}
public void run (){
// Here I have a file extraction method called
extract(this.getName);
}
// Extract Method
public Boolean extract(String filename){
// Some extraction Operation.
// I want to update the jTextField with the current filename under extraction.
}
}
// GUI类
public class Welcome extends javax.swing.JFrame {
SwingWorker<Boolean, Void>worker = new SwingWorker<Boolean, Void>() {
@Override
protected Boolean doInBackground() throws Exception {
Thread t1 = new Thread(new ZipUtility(fileName));
t1.start();
return true;
}
// Can safely update the GUI from this method.
@Override
protected void done() {
// Here when this worker is done I wanted to Run worker 2
}
};
SwingWorker<Boolean, Void>worker2 = .........
// Again when worker2 is done I wanted to run worker 3 and so on.
}
我的问题是:
1)如何以完美的顺序运行所有工人。即当完成worker1然后启动worker 2,当worker2完成后再启动worker 3,依此类推。
2)如何使用Samp类中的值更新Welcome类中的文本字段。
提前致谢。
答案 0 :(得分:3)
1)如何以完美的顺序运行所有工人。即当完成worker1然后启动worker 2,当worker2完成后再启动worker 3,依此类推。
要记住一个小技巧,您可以将SwingWorker
放入ExecutorService
所以,使用像...这样的东西。
public static class Worker extends SwingWorker {
private int sequence;
public Worker(int sequence) {
this.sequence = sequence;
}
@Override
protected Object doInBackground() throws Exception {
System.out.println(sequence + "...");
Thread.sleep(500);
return null;
}
}
作为测试,您可以使用类似......
的内容ExecutorService es = Executors.newFixedThreadPool(1);
for (int index = 0; index < 10; index++) {
es.submit(new Worker(index));
}
es.shutdown();
(您不需要调用shutdown
,但我的测试从未允许JVM终止;))
将按照提交的顺序运行工作人员。
现在,如果您想将值从一个SwingWorker
提供给另一个{/ 1}},您可以执行以下操作...
public abstract class ChainedWorker<T, V> extends SwingWorker<T, V> {
private ChainedWorker<T, ?> next;
private T previousValue;
public ChainedWorker(ChainedWorker<T, ?> next) {
this.next = next;
}
public void setPreviousValue(T previousValue) {
this.previousValue = previousValue;
}
@Override
protected void done() {
try {
T previous = get();
if (next != null) {
next.setPreviousValue(previous);
next.execute();
}
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
}
}
这只是一个SwingWorker
,它允许你在链中提供一个链接(下一个要调用的工作者),它传递了这个SwingWorker
生成的值...或者某些类似的< / p>
现在,如果你真的疯了,想要自己扮演角色,可能就像......
public class ChainedWorkerBuilder {
private List<SwingWorker> workers;
private SwingWorker current;
public ChainedWorkerBuilder() {
workers = new ArrayList<>(25);
}
public ChainedWorkerBuilder add(SwingWorker worker) {
workers.add(worker);
return this;
}
public void execute() {
if (!workers.isEmpty()) {
SwingWorker worker = workers.remove(0);
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("state".equals(evt.getPropertyName())) {
SwingWorker source = (SwingWorker)evt.getSource();
switch (source.getState()) {
case DONE:
source.removePropertyChangeListener(this);
execute();
break;
}
}
}
});
}
}
}
2)如何使用Samp类中的值更新Welcome类中的文本字段。
我不认为我直接关注您的代码,但是,如果您做了类似的事情......
SwingWorker<Boolean, Void>worker = new SwingWorker<Boolean, String>() {
protected Boolean doInBackground() throws Exception {
publish(fileName);
ZipUtility zu = new ZipUtility(fileName));
return zu.extract(fileName);
}
然后在SwingWorker
process
方法中,您可以安全地更新用户界面...
@Override
protected void process(List<String> chunks) {
// Grab the last element...
textfield.setText(chunks.get(chunks.size() - 1));
}
}
答案 1 :(得分:1)
只需致电execute()
完成worker2
上的worker1
:
final SwingWorker<Boolean, Void> worker2 = new SwingWorker<Boolean, Void>() {
...
}
final SwingWorker<Boolean, Void> worker1 = new SwingWorker<Boolean, Void>() {
@Override
protected Boolean doInBackground() throws Exception {
// Don't start a thread, do your work, otherwise the done will fire
// too early, also it's pointless to use worker and thread like that
return true;
}
// Can safely update the GUI from this method.
@Override
protected void done() {
// update some ui
// and start the second part
worker2.execute();
}
};