2010-07-25 80 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等待进入并冻结整个应用程序。