2011-12-12 150 views

回答

1
  1. 的代碼應該已經等待命令完成,因爲​​是一個阻塞方法。你可以簡單地通過在sleep 10println之前和之後運行示例main函數來測試它。
  2. 從運行函數獲取boolean結果應該非常簡單,您只需對運行函數進行小小的修改即可。

run功能:

def run(command:String) : Boolean = { 
    println("gonna runa a command: " + Thread.currentThread) 
    val args = command.split(" ") 
    val processBuilder = new ProcessBuilder(args: _*) 
    processBuilder.redirectErrorStream(true) 
    val proc = processBuilder.start() 

    //Send the proc to the actor, to extract the console output. 
    reader ! proc 

    //Receive the console output from the actor. 
    //+========== Begin Modified Section ==============+ 
    //Here, we'll store the result instead of printing it, storing a None 
    //if there was a timeout 

    var commandResult : Option[String] = None 
    receiveWithin(WAIT_TIME) { 
     case TIMEOUT => commandResult = None 
     case result:String => commandResult = Some(result) 
    } 

    //Here we interpret the result to return our boolean value 
    commandResult match { 
     case None => false 
     case Some(s) => //... You'll have to transform the result to a true/false 
      //however is most applicable to your use case 
    } 
} 
+0

哇,非常感謝您抽出寶貴時間來幫助我!非常感謝:)新年快樂 – jhdevuk