gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:01 26 4
gpt4 key购买 nike

我有两个线程在运行,userInputThread 等待来自命令行的用户输入,interrupterThread 尝试在启动后 1 秒中断 userInputThread。显然,您不能中断被 System.in 阻塞的线程。另一个答案建议在中断线程之前使用 System.in.close() 关闭 System.in。但是当我运行以下代码时,userInputThread 永远不会被中断,应用程序只是挂起而没有关闭。

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();
}
}

已经有一个类似的question ,但没有任何明确的答案。

最佳答案

这已经解决了问题:

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 以这种方式拆分时有效:

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

由于某些原因,当 readLine() 结构被写为单行代码时,中断似乎不起作用:

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

因此,虽然可以在拆分的 BufferedReader 结构中中断线程,但现在无法读取用户的输入。

如果有人可以提供一种方法来获取用户输入并在用户未及时提供任何输入时(中断器正在 hibernate 时)中断 UserInputThread,请这样做。

关于java - 中断等待用户输入的线程,然后退出应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20477978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com