Java从运行中传递值

Java从运行中传递值,java,Java,在我下面的程序中,如何在doGet()类中访问SyncPipe的str值 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] command = { "zsh" }; Process p = Runtime.getRuntime().exec(comm

在我下面的程序中,如何在doGet()类中访问SyncPipe的str值

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{

String[] command =
    {
  "zsh"
      };
            Process p = Runtime.getRuntime().exec(command);
            new Thread(new SyncPipe(p.getErrorStream(), response.getOutputStream())).start();
            new Thread(new SyncPipe(p.getInputStream(), response.getOutputStream())).start();
            PrintWriter stdin = new PrintWriter(p.getOutputStream());
            stdin.println("source ./taxenv/bin/activate");
            stdin.println("python runner.py");
            stdin.close();
            int returnCode = 0;
            try {
                returnCode = p.waitFor();
            }
            catch (InterruptedException e) {
                e.printStackTrace();

            } System.out.println("Return code = " + returnCode);

}               
class SyncPipe implements Runnable
{
    String str="";
public SyncPipe(InputStream istrm, OutputStream ostrm) {
      istrm_ = istrm;
      ostrm_ = ostrm;
  }
  public void run() {
      try
      {
          final byte[] buffer = new byte[1024];
          for (@SuppressWarnings("unused")
        int length = 0; (length = istrm_.read(buffer)) != -1; )
          {
             // ostrm_.write(buffer, 0, length);
              str = str + IOUtils.toString(istrm_, "UTF-8");
              //((PrintStream) ostrm_).println();
          }
          System.out.println(str);
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  @SuppressWarnings("unused")
private final OutputStream ostrm_;
  private final InputStream istrm_;
}
最后,


我只想将str值从run()传递到我的doget(),我该怎么做?

您需要持有对SyncPipe的引用并使用public getter:

SyncPipe pipe = new SyncPipe(p.getInputStream(), response.getOutputStream())
Thread thread = new Thread(pipe);
thread.start();
thread.join();
pipe.getStr();

....

class SyncPipe implements Runnable{
    String str="";
    public String getStr(){
        return str;
    }
....

ExecutorService
下运行线程,这提供了一种捕获返回值的方法 执行
线程之后

ExecutorService#submit((java.lang.Runnable,T))
提交一个可运行的任务以供执行,并返回表示该任务的未来。Future的get方法在成功完成后将返回null

代码段:

ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future1 = executor.submit(new SyncPipe(p.getErrorStream(), response.getOutputStream()),String.class);
Future<String> future2 = executor.submit(new SyncPipe(p.getErrorStream(), response.getOutputStream()),String.class);

future1.get();// will block the execution for you and the Thread completion
future2.get();
ExecutorService executor=Executors.newFixedThreadPool(2);
Future future1=executor.submit(新的同步管道(p.getErrorStream(),response.getOutputStream()),String.class);
Future future2=executor.submit(新的SyncPipe(p.getErrorStream(),response.getOutputStream()),String.class);
future1.get();//将为您阻止执行和线程完成
未来2.获取();

run()是一个方法,str值属于SyncPipe的一个实例。如果您引用了SyncPipe的实例,那么只需使用public getterOkay即可。纠正了我的问题。知道怎么做吗?嘿,它正在为str打印空值,更新了我的代码。/Pl check。这会打印一些东西:
System.out.println(str)?是的。这正按预期进行打印。在调用getter之前,您确定正在线程上调用
join()
?我刚刚试过,效果和预期一样。当我使用join时,根本没有输出。