如何使用try with resources将while循环放入线程?

时间:2013-12-12 09:41:58

标签: java multithreading sockets syntax try-with-resources

如何将while循环放入线程?

public class Weather {

    public static void main(String[] args) throws UnknownHostException, IOException {
        String host = "rainmaker.wunderground.com";
        int port = 3000;
        int byteOfData;
        {
            try (Socket socket = new Socket(host, port);
                    InputStream inputStream = socket.getInputStream();
                    OutputStream ouputStream = socket.getOutputStream();
                    PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
                    final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) {
                //also, a thread for reading from stdin
                Thread remoteOutputStream = new Thread() {
                    @Override
                    public void run() {
                        //while loop should go here
                        //   java.net.SocketException: Socket closed
                        //why does this error occur?
                    }
                };
                remoteOutputStream.start();
                while ((byteOfData = inputStream.read()) != -1) {  //put into thread
                    out.print((char) byteOfData);
                }
            }
        }
    }
}

当我将while循环放入线程时,它只会抛出java.net.SocketException: Socket closed,因为可能是try with resources works。但是,我不知道如何使用这种尝试的线程。

因为多个线程,一个用于读取本地InputStream,另一个用于远程OutputStream,所以似乎需要将线程放入try而不是反之亦然。 ?

2 个答案:

答案 0 :(得分:2)

将整个try with resources语句放入线程

final String host = "rainmaker.wunderground.com";
final int port = 3000;

{
  Thread remote = new Thread()
  {
    @Override public void run()
    {
      try (Socket socket = new Socket(host, port);
          InputStream inputStream = socket.getInputStream();
          OutputStream ouputStream = socket.getOutputStream();
          PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
          final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)))
      {
        int byteOfData;

        while ((byteOfData = inputStream.read()) != -1)
        { //put into thread
          out.print((char)byteOfData);
        }
      }
    }
  };
  remote.start();

答案 1 :(得分:2)

类似的东西(可能需要整理):

public static void main(String[] args) throws UnknownHostException, IOException {
    String host = "rainmaker.wunderground.com";
    int port = 3000;
    int byteOfData;
    {
            //also, a thread for reading from stdin
            Thread remoteOutputStream = new Thread() {
                @Override
                public void run() {
                    try (Socket socket = new Socket(host, port);
                        InputStream inputStream = socket.getInputStream();
                        OutputStream ouputStream = socket.getOutputStream();
                        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
                        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) {
                       while ((byteOfData = inputStream.read()) != -1) {  //put into thread
                           out.print((char) byteOfData);
                       }
                    } catch (CATCH EXCEPTIONS HERE) {
                       // Handle errors here
                    }
            };
            remoteOutputStream.start();
        }
    }
}

如果您需要分离线程,那么您将需要类似BlockingQueue的东西,读取一个线程并推入队列 - 另一个读取队列并写入。

我没有看到任何理由为什么你想要有两个线程来做到这一点。