我尝试了一个简单的代码,使用SwingWorker递归扫描目录中的所有文件 但似乎输出并不是那么好 这是代码
public class BackupBackgroundProcess extends SwingWorker<Object, String> {
public BackupBackgroundProcess() {
}
@Override
protected void process(List<String> list) {
System.out.println("PROCESSING FILE = "+list.get(0));
}
@Override
protected void done() {
}
@Override
protected FileStatus doInBackground() throws Exception {
System.out.println("OK");
File[] root;
root = new File("/home/hilman/Pictures/err").listFiles();
for (File file : root) {
seekFiles(file, this);
}
return null;
}
private void seekFiles(File f, SwingWorker thread) {
if(f.isDirectory()){
File[] listedFiles = f.listFiles();
for (int i = 0; i < listedFiles.length; i++) {
File file = listedFiles[i];
seekFiles(file, thread);
}
} else {
System.out.println(" PUBLISHING FILE "+f.getAbsolutePath());
publish(f.getAbsolutePath());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BackupBackgroundProcess().execute();
}
});
}
}
输出预期为
OK
PUBLISHING FILE /home/hilman/Pictures/err/_1427612060421_1.userfile
PUBLISHING FILE /home/hilman/Pictures/err/pic2.png
PUBLISHING FILE /home/hilman/Pictures/err/pic1.png
PUBLISHING FILE /home/hilman/Pictures/err/1.png
PROCESSING FILE = /home/hilman/Pictures/err/_1427612060421_1.userfile
PROCESSING FILE = /home/hilman/Pictures/err/pic2.png
PROCESSING FILE = /home/hilman/Pictures/err/pic1.png
PROCESSING FILE = /home/hilman/Pictures/err/1.ong
但它似乎只出现
OK
PUBLISHING FILE /home/hilman/Pictures/err/_1427612060421_1.userfile
PUBLISHING FILE /home/hilman/Pictures/err/pic2.png
PUBLISHING FILE /home/hilman/Pictures/err/pic1.png
PUBLISHING FILE /home/hilman/Pictures/err/1.png
PROCESSING FILE = /home/hilman/Pictures/err/_1427612060421_1.userfile
无论进程是否完成,是swingworker doInBackground结束? 为什么它不能正常工作?
答案 0 :(得分:2)
基本上,根据我的观察,当发生这种情况时,这是因为doBackground
方法中发生的事情阻止了处理“发布队列”的能力。
尝试在处理循环中的某处添加对Thread.yield
或Thread.sleep
的调用,以允许“发布”线程时间来处理“发布队列”
import java.io.File;
import java.util.List;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class BackupBackgroundProcess extends SwingWorker<Object, String> {
public BackupBackgroundProcess() {
}
@Override
protected void process(List<String> list) {
System.out.println("PROCESSING " + list.size() + " files");
}
@Override
protected void done() {
}
@Override
protected Object doInBackground() throws Exception {
System.out.println("OK");
File[] root;
root = new File("/home/hilman/Pictures/err").listFiles();
for (File file : root) {
seekFiles(file, this);
}
return null;
}
private void seekFiles(File f, SwingWorker thread) {
if (f.isDirectory()) {
File[] listedFiles = f.listFiles();
for (int i = 0; i < listedFiles.length; i++) {
File file = listedFiles[i];
seekFiles(file, thread);
}
} else {
// System.out.println(" PUBLISHING FILE " + f.getAbsolutePath());
publish(f.getAbsolutePath());
Thread.yield();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BackupBackgroundProcess().execute();
}
});
}
}
由于您正在进行递归通话,因此我在调用Thread.yield
后拨打了publish
,但您可以轻松地将其放在seek
方法的末尾。
请记住,进程/发布进程会缓冲结果,因此在调用publish
时,您可能会获得许多文件,例如......
OK
PROCESSING 122 files
PROCESSING 127 files
PROCESSING 184 files
PROCESSING 144 files
PROCESSING 131 files
PROCESSING 147 files
PROCESSING 335 files
...
我倾向于使用Thread.sleep(1)
,但我建议用两者进行测试,看看你得到的结果......