2013-12-11 63 views
0

這兩者之間有什麼區別。我有一個執行一系列可運行程序的程序,它在調試模式下工作正常,但不能在實時模式下運行。我不確定是否某些線程沒有被啓動,或者它的調試模式運行速度較慢,並且會影響速度。使用Netbeans調試器與不使用調試器執行可運行程序之間的區別

由於代碼跨越多個類,因此很難鏈接代碼。但我認爲這個問題出現在下面的代碼塊中。

/** 
* 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; 
    } 
} 

我在這裏試圖監視一個文件夾的新文件,然後他們被傳遞給一個可運行和讀取。

+1

聽起來像是海森堡(http://en.wikipedia.org/wiki/Heisenbug)。祝你好運。 –

+0

實際上,讀這個只是讓我想到使用註釋並輸出它們來查看代碼在哪裏掛起或者哪裏沒有到達。 – Jeremy

+1

你可以嘗試一下,但要注意在控制檯上打印一些東西(/ logging/whatever)可能也會影響執行的速度,這可能會導致與調試(讀取:你的程序可能工作)相同的效果。但試試看吧。如果它能幫助你找到這樣的原因,那麼你是幸運的。 –

回答

0

文件監視器正在看到該文件並在完成寫入之前嘗試讀取它。在調試模式下,因爲它的速度較慢,所以程序在讀取文件之前先完成寫入操作。我只是在看到文件後給它一個時間來處理它,然後放入一個threat.sleep(5)。根據他的評論,我給了Joachim Rohde找到我的讚揚。

相關問題