2010-07-25 79 views
0

我試着去實現使用線程的服務器上執行併發命令的情況。這些命令只是向服務器上運行的服務發送請求,並返回所需的輸出。Java線程問題

的命令基於讀取文件,其路徑用戶給出作爲輸入提供給應用程序生成。 基於每一行是在上述文件中,生成指令和在服務器上運行。 我通過在Linux機器上調用bash進程的一個實例來運行這些命令。 上面簡單的僞實現如下。

Class A { 
public static void main(blah blah) 
//take all user inputs, one of which is the location of the file that is to be read. 
try { 
      Runnable runnable =new bckwork(//pass the inputs taken from the user//); 
      thread = new Thread(runnable); 
      thread.start(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
} 

和執行實際工作的類 -

public class bckwork implements Runnable { 

bckwork(//takes in all input that the other class passes on) { 
     //assigns to local variables 
    } 
public void run() { 
     // TODO Auto-generated method stub 
    try{ 
     Process proc = null; 
     proc = Runtime.getRuntime().exec("/bin/bash", null, dir); 
     if (proc != null) { 
      String cmd=""; 
      BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); 

      PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true); 

      BufferedReader br = new BufferedReader(blah blah blah);//reads every line in the file that was passed onto this class. 
       String strLine; 
      while ((strLine = br.readLine()) != null) { 
       cmd=blah+blah+blah//the command is generated,whatever is read off the file is a part of the command. 
       out.println(cmd); 
      String line; 
       while ((line = in.readLine()).length()>1) { 
        line = in.readLine(); 
        System.out.println(line); 
        proc.waitFor(); 
        in.close(); 
        out.close(); 
        proc.destroy(); 
         } 
     } 
} 
}catch(Exception e){} 
} 
} 

與上面的代碼段的問題是,他們只運行該文件的第一行正被讀取,然後處決進入某種僵局。即時猜測,從該文件的下一行是永不read.My意圖是爲每一個正在讀或本質上就是正在建設中的每一個命令行踢一個線程。

這一錯誤如何可以糾正任何指針將不勝感激。

回答

1

while ((line = in.readLine()).length()>1) 

in.readLine等待進入並凍結整個應用程序。