0

I am new to programming. Need just one program for my bachelors project.

I have a problem with running a thread in my program which uses buffered reader to save some data from an internet stream. When I run the thread for the first time everything works perfectly but when i finish it and run a new one, thread seems working but the cycle with buffered reader part somehow doesn't work. Can you help me?

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JTextField;


     volatile boolean stopRequested = false;
     private Socket socket = null;
     private BufferedReader in = null;


    public void runMe() {
        stopRequested = false;
        Thread vypocet = new Thread(mytask);
        vypocet.start();
        }
    public void stopMe() {
        stopRequested = true;
        }

        public Runnable mytask = () -> {

            try {
                socket = new Socket(Gui.iP.getText(), Integer.parseInt(Gui.port.getText()));
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection");
                System.exit(1);
            }

            try {
                for (String line = in.readLine(); stopRequested != true; line = in.readLine()) {

    //some calculation I need

                   } 
catch (IOException ex1) {
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex1);
                System.err.println("Error in loading receiver's data. Please turn the program off and on again.");
                System.exit(1);
            }

            System.out.println("End of the calculation");
     };
  • Do you any of your `println` calls get pushed out before the failure on the buffered reader? – Rick Burns Oct 14 '15 at 18:53
  • nope everything looks fine, only when I stop the thread the End of calculation println doesnt appear (probably because there is some kind of problem in the for cycle - with buffered reader so the cycle is not working when I start the thread for the second time. – Jirka Prow Oct 17 '15 at 11:24

0 Answers0