Java 使用Netbeans调试器和不使用调试器执行runables之间的区别
两者的区别是什么。我有一个程序,它执行一系列的可运行文件,在调试模式下运行良好,但在实时模式下运行不好。我不确定是否某些线程没有被启动,或者它是否可能是一个速度因素,在这个速度因素中,调试模式运行得比较慢,并且会产生一些影响 很难链接代码,因为代码跨越多个类。我认为问题在于下面的代码块Java 使用Netbeans调试器和不使用调试器执行runables之间的区别,java,netbeans,Java,Netbeans,两者的区别是什么。我有一个程序,它执行一系列的可运行文件,在调试模式下运行良好,但在实时模式下运行不好。我不确定是否某些线程没有被启动,或者它是否可能是一个速度因素,在这个速度因素中,调试模式运行得比较慢,并且会产生一些影响 很难链接代码,因为代码跨越多个类。我认为问题在于下面的代码块 /** * The class that is used to load the track points in a background thread. */ protected class Monitor
/**
* 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;
}
}
/**
*用于在背景线程中加载轨迹点的类。
*/
受保护类MonitorDirectory扩展SwingWorker{
公共布尔继续=真;
/**
*执行器服务线程池。
*/
私有executor服务executor=null;
/**
*报告已完成线程的完成服务。
*/
私有CompletionService CompletionService=null;
@凌驾
受保护的Void doInBackground()引发异常{
//这是一个测试
executor=Executors.newFixedThreadPool(1);
completionService=新的执行者completionService(执行者);
Path folder=Path.get(目录);
WatchService WatchService=FileSystems.getDefault().newWatchService();
文件夹。注册(watchService、StandardWatchEventKinds.ENTRY\u CREATE);
布尔有效=真;
做{
WatchKey WatchKey=watchService.poll();
if(watchKey!=null){
for(WatchEvent事件:watchKey.pollEvents()){
如果(继续==false){
返回null;
}
WatchEvent.Kind-Kind=event.Kind();
if(StandardWatchEventTypes.ENTRY_CREATE.equals(event.kind())){
字符串文件名=event.context().toString();
File File=新文件(directory+File.separator+fileName);
completionService.submit(Executors.callable(newreadtrack(file,true));
tracksModel.fireStateChanged(tracksModel.CHANGE\u事件\u轨迹);
timeModel.setLoadingData(LiveTracksProvider.this.hashCode(),false);
}
}
valid=watchKey.reset();
}
}
while(有效&继续);
返回null;
}
}
我在这里尝试的是监视一个文件夹中的新文件,然后将它们传递到一个可运行并可读取的文件夹 文件监视器正在查看文件,并试图在完成写入之前读取它。在调试模式下,因为它的速度较慢,所以它给了程序在读取文件之前完成写入的时间。我只是放了一个威胁。在它看到文件后再睡觉(5),让它有时间处理它。根据约阿希姆·罗德的评论,我很荣幸地找到了我的奥恩瑟。听起来像是海森堡()。祝你好运。实际上读到这篇文章让我想到使用注释并输出它们来查看代码挂起的位置或未挂起的位置。你可以尝试一下,但要注意,在控制台上打印一些内容(/logging/whatever)也可能会影响执行速度,这可能会导致与调试相同的效果(阅读:您的程序可能会工作)。但是试一下。如果它能帮助您找到这样的原因,那么您就很幸运了。事实上,非常感谢您,我是通过注释发现问题的。文件监视器正在查看文件并试图在完成写入之前读取它。在调试模式下,由于速度较慢,它让程序有时间在读取文件之前完成写入。I j必须在看到文件后放入一个threat.sleep(5),让它有时间处理它。