且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

中断等待用户输入的线程,然后退出应用程序

更新时间:2021-11-25 08:52:02

这解决了这个问题:

class InputInterruptionExample {

    private UserInputThread userInputRunnable;
    private Thread userInputThread;
    private Thread interrupterThread;

    InputInterruptionExample() {
        this.userInputRunnable = new UserInputThread();
        this.userInputThread = new Thread(userInputRunnable);
        this.interrupterThread = new Thread(new InterrupterThread());
    }

    void startThreads() {
        this.userInputThread.start();
        this.interrupterThread.start();
    }
    private class UserInputThread implements Runnable {
        private InputStreamReader isr;
        private BufferedReader br;

        UserInputThread() {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
        }

        public void run() {
            try {
                System.out.println("enter your name: ");
                try{
                    String userInput = br.readLine();
                } catch(NullPointerException e) {}
            } catch (IOException e) {
                System.out.println("Oops..somethign went wrong.");
                System.exit(1);
            }
        }
        public void closeBufferdReader() {
            try {
                System.in.close();
            } catch (IOException e) {
                System.out.println("Oops..somethign went wrong in closeBufferdReader() method");
                System.exit(1);
            }
        }
    }
    private class InterrupterThread implements Runnable {
        public void run() {
            try {
                sleep(1000);
                userInputRunnable.closeBufferdReader();                 
                userInputThread.interrupt();
                userInputThread.join();
                System.out.println("Successfully interrupted");
            } catch (InterruptedException e) {}
        }
    }
    public static void main(String[] args) {
        InputInterruptionExample exampleApp = new InputInterruptionExample();
        exampleApp.startThreads();
    }
}

更新:当BufferedReader以这种方式拆分时工作:

Update: This only works when BufferedReader is split up this way:

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String userInput = br.readLine();

由于某种原因,当readLine()结构写为oneliner :

For some reason the interruption does not seem to work when the readLine() structure is written as a oneliner:

this.userInput = (new BufferedReader(new InputStreamReader(System.in))).readLine();

因此,虽然可以中断BufferedReader结构中的线程,以读取用户的输入。

So while it is possible to interrupt the thread in the split-up BufferedReader structure, it is now impossible to read user's input.

如果有人可以显示一种方式来获取用户输入,以及当用户没有及时提供任何输入时中断UserInputThread (当中断器正在休眠时),请执行。

If someone could show a way to be able to get user input as well as interrupt the UserInputThread when the user doesn't provide any input in time (while interrupter is sleeping), please do.