Przerwij wątek oczekujący na wprowadzenie danych przez użytkownika, a następnie zamknij aplikację

Mam dwa wątki uruchomione,userInputThread czeka na wprowadzenie danych przez użytkownika z linii poleceń iinterrupterThread próbuje przerwaćuserInputThread 1 s po uruchomieniu. Oczywiście nie możesz przerwać wątku zablokowanego przezSystem.in. Kolejna odpowiedź sugeruje zamknięcieSystem.in zSystem.in.close() przed przerwaniem wątku. Ale kiedy uruchomię następujący kod:userInputThread nigdy się nie przerywa, a aplikacja po prostu zawiesza się bez zamykania.

class InputInterruptionExample {

    private Thread userInputThread;
    private Thread interrupterThread;

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

    void startThreads() {
        this.userInputThread.start();
        this.interrupterThread.start();
    }
    private class UserInputThread implements Runnable {
        public void run() {
            try {
                System.out.println("enter your name: ");
                String userInput = (new BufferedReader(new InputStreamReader(System.in))).readLine();
            } catch (IOException e) {
                System.out.println("Oops..somethign went wrong.");
                System.exit(1);
            }
        }
    }
    private class InterrupterThread implements Runnable {
        public void run() {
            try {
                sleep(1000);
                System.out.println("about to interrupt UserInputThread");
                System.in.close();
                userInputThread.interrupt();
                userInputThread.join();
                System.out.println("Successfully interrupted");
            } catch (InterruptedException e) {
            } catch (IOException ex) {
                System.out.println("Oops..somethign went wrong.");
                System.exit(1);
            }
        }
    }
    public static void main(String[] args) {
        InputInterruptionExample exampleApp = new InputInterruptionExample();
        exampleApp.startThreads();
    }
}

Jest już podobnypytanie, ale nie ma żadnych konkretnych odpowiedzi.

questionAnswers(2)

yourAnswerToTheQuestion