Cancel/Force 停止通过 SwingWorker 执行的输入和输出流
Cancel/Force Stop an Input and Output stream executed through SwingWorker
我想停止通过 SwingWorker
doInBackground
执行的输入和输出流。每当我取消任务时,它仍然会创建文件(请参见下面的代码)。任务很简单,"for every file name (a String
) specified, output this file with the given name"(差不多)。
我在一个单独的 package/class 中写了 IO 流。所以代码是这样的:
public class ResourceFileExtract(String outputFile) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = getClass().getResourceAsStream("/resources/someFile");
outputStream = new FileOutputStream(outputFile);
byte[] bytes = new byte[1024];
int numbers;
while ((numbers = inputStream.read(bytes)) > 1) {
outputStream.write(bytes, 0, length)
}
outputStream.flush();
outputStream.close();
inputStream.close();
} /* Other codes here */
}
SwingWorker 设置。
private class BackgroundTask extends SwingWorker<Integer, String> {
@Override
protected Integer doInBackground() {
/* Some codes here */
if (!isCancelled()) {
for (/* Loop statement */) {
try {
// Input and Output stream called from a class...
ResourceFileExtract fileExtract = new ResourceFileExtract(specifiedOutputName);
System.out.println("Done (file extracted successfully)");
} catch (IOException ex) {
System.out.println("Error (unable to read resource file)");
return 0;
}
}
} else {
// These texts are not printed in console...
System.out.println("Cancelled (I/O streaming stopped)");
return 0;
}
return 0;
}
}
SwingWorker 执行:
JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
BackgroundTask bgTask = new BackgroundTask();
bgTask.execute();
}
});
并取消:
JButton stop = new JButton("Stop");
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
bgTask.cancel(true);
}
});
调试程序时,控制台中未打印取消消息,这意味着每当我点击取消按钮时任务都不会取消,并且输出文件仍然创建并显示成功消息。我怎样才能阻止这个?有帮助吗?
基本上,您的 ResourceFileExtract
class 需要以某种方式取消,例如...
public class ResourceFileExtract {
private String outputFile;
private AtomicBoolean cancelled = new AtomicBoolean(false);
public ResourceFileExtract(String outputFile) {
this.outputFile = outputFile;
}
public void cancel() {
cancelled.set(true);
}
public void extract() throws IOException {
try (InputStream inputStream = getClass().getResourceAsStream("/resources/someFile");
OutputStream outputStream = new FileOutputStream(outputFile)) {
byte[] bytes = new byte[1024];
int numbers;
while (!cancelled.get() && (numbers = inputStream.read(bytes)) > 1) {
outputStream.write(bytes, 0, numbers);
}
} catch (IOException exp) {
throw exp;
}
}
}
然后在您的 SwingWorker
中,您需要提供某种您自己的 "stop" 方法(因为 cancel
是 final
:P),我可以'保证任何 属性 更改事件都会因为调用 cancel
本身而被触发。
工作人员需要维护对 doInBackground
方法创建的提取器的引用,以允许它成功触发提取器中的取消
我想停止通过 SwingWorker
doInBackground
执行的输入和输出流。每当我取消任务时,它仍然会创建文件(请参见下面的代码)。任务很简单,"for every file name (a String
) specified, output this file with the given name"(差不多)。
我在一个单独的 package/class 中写了 IO 流。所以代码是这样的:
public class ResourceFileExtract(String outputFile) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = getClass().getResourceAsStream("/resources/someFile");
outputStream = new FileOutputStream(outputFile);
byte[] bytes = new byte[1024];
int numbers;
while ((numbers = inputStream.read(bytes)) > 1) {
outputStream.write(bytes, 0, length)
}
outputStream.flush();
outputStream.close();
inputStream.close();
} /* Other codes here */
}
SwingWorker 设置。
private class BackgroundTask extends SwingWorker<Integer, String> {
@Override
protected Integer doInBackground() {
/* Some codes here */
if (!isCancelled()) {
for (/* Loop statement */) {
try {
// Input and Output stream called from a class...
ResourceFileExtract fileExtract = new ResourceFileExtract(specifiedOutputName);
System.out.println("Done (file extracted successfully)");
} catch (IOException ex) {
System.out.println("Error (unable to read resource file)");
return 0;
}
}
} else {
// These texts are not printed in console...
System.out.println("Cancelled (I/O streaming stopped)");
return 0;
}
return 0;
}
}
SwingWorker 执行:
JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
BackgroundTask bgTask = new BackgroundTask();
bgTask.execute();
}
});
并取消:
JButton stop = new JButton("Stop");
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
bgTask.cancel(true);
}
});
调试程序时,控制台中未打印取消消息,这意味着每当我点击取消按钮时任务都不会取消,并且输出文件仍然创建并显示成功消息。我怎样才能阻止这个?有帮助吗?
基本上,您的 ResourceFileExtract
class 需要以某种方式取消,例如...
public class ResourceFileExtract {
private String outputFile;
private AtomicBoolean cancelled = new AtomicBoolean(false);
public ResourceFileExtract(String outputFile) {
this.outputFile = outputFile;
}
public void cancel() {
cancelled.set(true);
}
public void extract() throws IOException {
try (InputStream inputStream = getClass().getResourceAsStream("/resources/someFile");
OutputStream outputStream = new FileOutputStream(outputFile)) {
byte[] bytes = new byte[1024];
int numbers;
while (!cancelled.get() && (numbers = inputStream.read(bytes)) > 1) {
outputStream.write(bytes, 0, numbers);
}
} catch (IOException exp) {
throw exp;
}
}
}
然后在您的 SwingWorker
中,您需要提供某种您自己的 "stop" 方法(因为 cancel
是 final
:P),我可以'保证任何 属性 更改事件都会因为调用 cancel
本身而被触发。
工作人员需要维护对 doInBackground
方法创建的提取器的引用,以允许它成功触发提取器中的取消