1

Hello although I read a lot of other answeres I do not understand how to close BufferedReader without sending input. This is the blocking part of my program:

public class AuctionClientUserListener extends Thread {

    private PrintWriter out;
    private BufferedReader stdIn;
    private int udpPort;
    private boolean running = true;
    private boolean listening = true;

    // needs server connection out
    public AuctionClientUserListener(BufferedReader stdIn,PrintWriter out,int udpPort) {
        this.stdIn = stdIn;
        this.out = out;
    }

    public void run() {
        String fromUser;

        while(true) {
            if(!listening) {
                break;
            }
            try {

                fromUser = stdIn.readLine(); // BLOCKS

                if (fromUser != null) {
                        System.out.println("Me: " + fromUser);

                        // check if to send udp port
                        String[] spinput = fromUser.split(" ");
                        if(spinput[0].equals("!login")) {
                            fromUser+=" "+udpPort;
                        }

                        out.println(fromUser);

                        if(fromUser.equals("!end")) {
                            listening = false;
                        }
                }
            } catch(Exception e) {
                listening = false;
            }
        }
        running = false;
    }

    public boolean running() {
        return running;
    }

    public void endListening() {

        this.listening =  false;
    }
}

When I call "endListening()" from the main thread, the program terminates - but NOT BEFORE the user inputs anything. I want to the program to terminate instantly - how to intterupt this BufferdReader (is created like BufferedReader(new InputStreamReader(System.in)) )?

I tried to call stdIn.close() and also thread.interrupt() on this Thread in the main Thread, but it ist not working. I also tried to close the InputStreamReader in the main Thread.

3
  • try using a Scanner instead of a BufferedReader. Commented Nov 23, 2012 at 14:40
  • Is there a possibility to solve it with Buffered Reader? Commented Nov 23, 2012 at 14:46
  • The people are being distracted by the title. I would advise a title like "Interrupt a read operation" or "Unblock a read operation". Commented Nov 23, 2012 at 14:59

3 Answers 3

1

You could check to see if there's data available before calling BufferedReader.readLine(), i.e.

while(true) {
    if (!listening) break;
    if (System.in.available() > 0) {
        try {
            fromUser = stdIn.readLine();
            // etc.
Sign up to request clarification or add additional context in comments.

2 Comments

If there is say, one character a in the inputsream, your check will pass but the readLine will still block waiting for the endline character.
Great, that solved the problem. Although I still wonder if there is another possibility.
1

You must use thread.interrupt to signal the thread to interrupt its process. Be ready to catch an InterruptedException launched by the thread.

http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

Comments

0

i think u can close your BufferedReader using stdIn.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.