how do I put a while loop into a thread, using try with resources?

2.2k views Asked by At

How do I put the while loop into the thread?

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);
                }
            }
        }
    }
}

when I put the while loop into the thread, it just throws java.net.SocketException: Socket closed because, presumably, of how try with resources works. However, I don't know how to use a thread with this kind of try.

Because there will be multiple threads, one for reading the local InputStream and one for the remote OutputStream, it seems necessary to put the threads into the try and not vice versa..?

2

There are 2 answers

8
Ross Drew On BEST ANSWER

Put the entire try with resources statement into the thread

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();
3
Tim B On

Something like (will probably need tidying up):

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();
        }
    }
}

If you need to separate threads then you will want something like a BlockingQueue, have one thread read and push to the queue - the other read pull from the queue and write.

I don't see any reason why you would want to have two threads to do this though.