2016-02-15 89 views
-1

你好,大家好我有一個如何在背景中的JavaFXJavaFX中運行的後臺任務

運行任務的一個問題目前的情況是,我已經創造了JavaFX的複印功能,它是工作精絕,但如果我們有更多的文件,那麼它將進入無響應模式,直到過程完成,日誌也不會打印在我的textarea中,每個文件都被複制到相應的文件夾中,但問題是它會一直掛起,直到過程完成,

還有一個問題,如何永遠運行這個程序意味着每當一個新文件進入源目錄時,它就會自動進入目標目錄。

這裏是我的代碼

try 
     { 
      sourceFile = new File(sourcePath).listFiles(); 
      syslog.appendText("\nTotal Files in the Directory : " + sourceFile.length); 
      for(int i = 0; i<sourceFile.length;i++) 
      { 
       if(sourceFile[i].isFile()) 
       { 
        String file = sourceFile[i].getName(); 
        String extension = Files.getFileExtension(file); 
        if(!new File(destinationPath+"/"+extension.toUpperCase()).exists()) 
        { 
         if(new File(destinationPath+"/"+extension.toUpperCase()).mkdir()) 
         { 
          syslog.appendText("\nDirectory Created : " + destinationPath+"/"+extension.toUpperCase()); 
          try 
          { 
           if(!new File(destinationPath+"/"+extension.toUpperCase()+"/"+file).exists()) 
           { 
            syslog.appendText("\nFile "+file+" is processing to copy to "+destinationPath+"/"+extension.toUpperCase()); 
            copyFile(sourceFile[i],new File(destinationPath+"/"+extension.toUpperCase()+"/"+file)); 
            syslog.appendText("\nFile "+file+" is successfully copied to "+destinationPath+"/"+extension.toUpperCase()); 
            if(sourceFile[i].delete()) 
             syslog.appendText("\nFile "+file+" is successfully deleted from "+sourcePath); 
            else 
             syslog.appendText("\nError in deleting File "+file+" from "+sourcePath); 
           } 
          } 
          catch(Exception e) 
          { 
           e.printStackTrace(); 
           syslog.appendText("\nSome Error Occurred while copying the File : "+sourceFile[i]); 
          } 
         } 
        } 
        else 
        { 
         try 
         { 
          if(!new File(destinationPath+"/"+extension.toUpperCase()+"/"+file).exists()) 
          { 
           syslog.appendText("\nFile "+file+" is processing to copy to "+destinationPath+"/"+extension.toUpperCase()); 
           copyFile(sourceFile[i],new File(destinationPath+"/"+extension.toUpperCase()+"/"+file)); 
           syslog.appendText("\nFile "+file+" is successfully copied to "+destinationPath+"/"+extension.toUpperCase()); 
           if(sourceFile[i].delete()) 
            syslog.appendText("\nFile "+file+" is successfully deleted from "+sourcePath); 
           else 
            syslog.appendText("\nError in deleting File "+file+" from "+sourcePath); 
          } 
         } 
         catch(Exception e) 
         { 
          e.printStackTrace(); 
          syslog.appendText("\nSome Error Occurred while copying the File : "+sourceFile[i]); 
         } 
        } 
       } 
      } 
      syslog.appendText("\nFinished.........."); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

這是複印功能

private static void copyFile(File source, File destination) 
     throws IOException { 
    FileChannel inputChannel = null; 
    FileChannel outputChannel = null; 
    try { 
     inputChannel = new FileInputStream(source).getChannel(); 
     outputChannel = new FileOutputStream(destination).getChannel(); 
     outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); 
    } finally { 
     inputChannel.close(); 
     outputChannel.close(); 
    } 
} 

回答

0

你需要創建一個任務,並把它添加到一個新的線程。它看起來像這樣:

 Task<T> backgroundTask = new Task<T>() { 
     @Override 
     protected T call() throws Exception { 
      return null; 
     } 

     @Override 
     public void run() { 
      try { 
        copyFile(source,destination); //or any other operation you want to have in a thread. 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
       } 
     } 
    }; 
    Thread backgroundThread = new Thread(backgroundTask); 
    backgroundThread.setDaemon(true); //true if you want to have it running excuslivly when having your parent-threat running 

您可以打電話與

backgroundThread.run(); 

一次運行該線程Futher你可以

backgroundThread.state(); 

檢查線的狀態,這可能是,如果有幫助你想檢查例如如果你的線程仍在處理中。

考慮與您的javafx線程發生衝突。如果你想改變這是由訪問的對象JavaFX的線程需要執行一個

Platform.runLater(new Runnable() {/*your impact on javafx*/}); 
1

我會sugest使用一個任務,是這樣的:

public class CopyFileTask<Void> extends Task<Void> { 

    @Override 
    protected void succeeded() { 
     super.succeeded(); 
     // e.g. show "copy finished" dialog 
    } 

    @Override 
    protected void running() { 
     super.running(); 
     // e.g. change mouse courser 
    } 

    @Override 
    protected void failed() { 
     super.failed(); 
     // do stuff if call threw an excpetion 
    } 

    @Override 
    protected Void call() { 
     // do expensive the expensive stuff 
     copyStuff(source, destination) 
     return null ; 
    } 
} 

的便捷方法succeededrunningfailed在JavaFX GUI線程中執行,而call中的東西在另一個線程中執行。運行任務,我建議將其提交給ExecuterService

ExecutorService exService = Executors.newSingleThreadExecutor(); 
exService.submit(new CopyFileTask()); 
+1

記得撥打['exService.shutdown()'](https://docs.oracle.com/javase/8/docs/api /java/util/concurrent/ExecutorService.html#shutdown--)在某個階段(通常是應用程序的['stop()'](https://docs.oracle.com/javase/8/javafx/api/javafx/ application/Application.html#stop--)方法),以便在不再需要執行程序服務時關閉該程序,並允許程序正常退出。或者,或者使用提供守護程序線程的新線程工廠創建ExecutorService。 – jewelsea