我想在另一个结束之后运行SwingWorker。我的2个线程位于同一类的不同方法内,我想按顺序从另一个类使用它们,如下所示:
int x = runn.runProcessStart();
int y = runn. runProcessEnd();
工人班
public class MyClass {
private int counter = 0;
public int runProcessStart() {
int result = 0;
SwingWorker<Integer, String> worker = new SwingWorker<Integer, String>() {
@Override
protected Integer doInBackground() throws Exception {
for (int i = 0; i < 10; i++) {
publish("start message number " + counter++);
Thread.sleep(500);
}
return 0;
}
@Override
protected void process(List<String> chunks) {
// this is called on the Swing event thread
for (String text : chunks) {
Sysem.out.println(text);
}
}
};
worker.execute();
if (worker.getState() == SwingWorker.StateValue.DONE) {
try {
result = worker.get();
} catch (ExecutionException | InterruptedException ex) {
Sysem.out.println(ex.getMEssage());
}
}
return result;
}
public int runProcessEnd() {
int result = 0;
SwingWorker<Integer, String> worker = new SwingWorker<Integer, String>() {
@Override
protected Integer doInBackground() throws Exception {
for (int i = 0; i < 10; i++) {
publish("end message number " + counter++);
Thread.sleep(500);
}
return 0;
}
@Override
protected void process(List<String> chunks) {
// this is called on the Swing event thread
for (String text : chunks) {
Sysem.out.println(text);
}
}
};
worker.execute();
if (worker.getState() == SwingWorker.StateValue.DONE) {
try {
result = worker.get();
} catch (ExecutionException | InterruptedException ex) {
Sysem.out.println(ex.getMEssage());
}
}
return result;
}
}
我该怎么做?
另一个问题是...我使用.get()检索doInBackground进程的值的方法正确吗?
答案 0 :(得分:0)
使用SwingWorker#done方法,如以下mcve所示:
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
public class MyClass {
private int counter = 0;
private JLabel showCounter;
private JButton start;
public MyClass() {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
start = new JButton("Start");
start.addActionListener(e -> runProcessStart());
window.add(start, BorderLayout.PAGE_START);
showCounter = new JLabel(String.valueOf("Click button to start" ));
window.add(showCounter, BorderLayout.PAGE_END);
window.pack();
window.setVisible(true);
}
public void runProcessStart() {
start.setEnabled(false);
SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
@Override
protected Void doInBackground() throws Exception {
for (int i = 0; i < 10; i++) {
publish(counter++);
Thread.sleep(2000);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
for (int i : chunks) {
showCounter.setText("Process Start running "+ i);
}
}
@Override
protected void done() {
showCounter.setText("Process Start finished");
runProcessEnd();
}
};
worker.execute();
}
public void runProcessEnd() {
SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
@Override
protected Void doInBackground() throws Exception {
for (int i = 0; i < 10; i++) {
Thread.sleep(2000);
publish(counter--);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
for (int i : chunks) {
showCounter.setText("Process End running "+ i);
}
}
@Override
protected void done() {
start.setEnabled(true);
showCounter.setText("All done");
}
};
worker.execute();
}
public static void main(String[] args) {
new MyClass();
}
}
请注意
返回的值int x = runn.runProcessStart();
int y = runn.runProcessEnd();
代码中的可能不是您期望的,因为runProcessStart()
和runProcessEnd()
在SwingWorker
结束之前返回。