我有两个线程正在运行,userInputThread
等待用户从命令行输入并interrupterThread
试图打断userInputThread
启动后1秒。显然你不能中断被阻塞的线程System.in
。另一个答案建议关闭System.in
with System.in.close()
在中断线程之前。但是当我运行以下代码时,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 https://stackoverflow.com/questions/8997310/interrupt-a-thread-waiting-for-user-input,但没有任何明确的答案。
这已经解决了问题:
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();
}
}
Update:仅当 BufferedReader 以这种方式拆分时才有效:
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String userInput = br.readLine();
由于某种原因,当 readLine() 结构被写为 oneliner 时,中断似乎不起作用:
this.userInput = (new BufferedReader(new InputStreamReader(System.in))).readLine();
因此,虽然可以中断分裂的 BufferedReader 结构中的线程,但现在无法读取用户的输入。
如果有人可以展示一种方法,能够在用户没有及时提供任何输入时(当中断器正在睡眠时)获取用户输入以及中断 UserInputThread ,请这样做。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)