0

如何将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反之亦然..?

4

2 回答 2

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的东西,让一个线程读取并推送到队列 - 另一个从队列中读取并写入。

不过,我看不出有什么理由让你有两个线程来做这件事。

于 2013-12-12T09:53:06.113 回答
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();
于 2013-12-12T09:42:44.850 回答