两者之间有什么区别。我有一个执行一系列可运行程序的程序,它在调试模式下运行良好,但在实时模式下运行不好。我不确定某些线程是否没有被启动,或者这是否可能是调试模式运行较慢的速度因素,这会产生一些影响。
由于代码跨越多个类,因此很难链接代码。不过,我认为问题出在下面的代码块中。
/**
* 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;
}
}
我在这里尝试的是监视文件夹中的新文件,然后将它们传递给可运行文件并读取。
文件监视器正在查看该文件,并试图在完成写入之前读取该文件。在调试模式下,因为速度较慢,它给了程序在读取文件之前完成写入的时间。我只是发出了一个威胁。sleep(5)在它看到文件后进入,给它处理文件的时间。根据Joachim Rohde的评论,我把找到我的awnser归功于他。