两者有什么区别。我有一个执行一系列可运行程序的程序,它在调试模式下工作正常,但在实时模式下没有。我不确定某些线程是否会被启动,或者它是否可能是调试模式运行速度较慢并且有一定影响的速度因素。
由于代码跨越多个类,因此难以链接代码。我认为问题出在下面的代码块中。
/**
* The class that is used to load the track points in a background thread.
*/
protected class MonitorDirectory extends SwingWorker<Void, Void> {
public boolean continuing = true;
/**
* The executor service thread pool.
*/
private ExecutorService executor = null;
/**
* The completion service that reports the completed threads.
*/
private CompletionService<Object> completionService = null;
@Override
protected Void doInBackground() throws Exception {
//This is a test
executor = Executors.newFixedThreadPool(1);
completionService = new ExecutorCompletionService<>(executor);
Path folder = Paths.get(directory);
WatchService watchService = FileSystems.getDefault().newWatchService();
folder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
boolean valid = true;
do {
WatchKey watchKey = watchService.poll();
if (watchKey != null) {
for (WatchEvent<?> event : watchKey.pollEvents()) {
if (continuing == false) {
return null;
}
WatchEvent.Kind kind = event.kind();
if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
String fileName = event.context().toString();
File file = new File(directory + File.separator + fileName);
completionService.submit(Executors.callable(new ReadTrack(file, true)));
tracksModel.fireStateChanged(TracksModel.CHANGE_EVENT_TRACKS);
timeModel.setLoadingData(LiveTracksProvider.this.hashCode(), false);
}
}
valid = watchKey.reset();
}
}
while (valid && continuing);
return null;
}
}
我在这里尝试的是监视新文件的文件夹,然后将它们传递给runnable并读取。
答案 0 :(得分:0)
文件监视器正在查看该文件,并在完成编写之前尝试读取该文件。在调试模式下,因为它较慢,它使程序有时间在读取之前完成文件的写入。我只是在看到文件之后放了一个threat.sleep(5)来给它时间来处理它。我很荣幸能够根据他的评论向Joachim Rohde寻找我的灵感。