gpt4 book ai didi

java swingworker 无法正常工作,它发布但无法处理

转载 作者:行者123 更新时间:2023-12-01 22:22:49 25 4
gpt4 key购买 nike

我尝试了一个简单的代码,使用 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 方法都会完成吗?为什么不能正常工作?

最佳答案

基本上,根据我的观察,发生这种情况是因为 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();
}
});

}
}

由于您正在进行递归调用,因此我在调用 publish 之后调用了 Thread.yield,但您也可以轻松地将其放在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),但我建议对两者进行测试,看看会得到什么结果...

关于java swingworker 无法正常工作,它发布但无法处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29402525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com