两者有什么区别。我有一个程序,它执行一系列可运行程序,并且在调试模式下工作正常,但在实时模式下却无法正常工作。我不确定某些线程是否开始启动,或者它是否可能是调试模式运行速度较慢的速度因素,并且会产生一些影响。
由于代码跨越多个类,因此很难链接代码。我认为问题出在以下代码块中。
/**
* 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的评论找到自己的遮阳篷。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句