简体   繁体   English

如何使用 BufferedReader (Java) 中断读取操作?

[英]How to interrupt a read operation using BufferedReader (Java)?

Hello although I read a lot of other answeres I do not understand how to close BufferedReader without sending input.您好,虽然我阅读了很多其他答案,但我不明白如何在不发送输入的情况下关闭 BufferedReader。 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.当我从主线程调用“endListening()”时,程序终止——但不是在用户输入任何内容之前。 I want to the program to terminate instantly - how to intterupt this BufferdReader (is created like BufferedReader(new InputStreamReader(System.in)) )?我想让程序立即终止——如何中断这个 BufferdReader(像 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.我试图在主线程中的这个线程上调用 stdIn.close() 和 thread.interrupt() ,但它不起作用。 I also tried to close the InputStreamReader in the main Thread.我还尝试关闭主线程中的 InputStreamReader。

You could check to see if there's data available before calling BufferedReader.readLine() , ie您可以在调用BufferedReader.readLine()之前检查是否有可用数据,即

while(true) {
    if (!listening) break;
    if (System.in.available() > 0) {
        try {
            fromUser = stdIn.readLine();
            // etc.

You must use thread.interrupt to signal the thread to interrupt its process.您必须使用thread.interrupt向线程发出信号以中断其进程。 Be ready to catch an InterruptedException launched by the thread.准备好捕获线程启动的InterruptedException

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

我想你可以使用 stdIn.close() 关闭你的 BufferedReader;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM