java-中断线程以等待用户输入,然后退出应用程序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-中断线程以等待用户输入,然后退出应用程序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3521字,纯文字阅读大概需要6分钟。
内容图文
![java-中断线程以等待用户输入,然后退出应用程序](/upload/InfoBanner/zyjiaocheng/659/f75d162763344d809d4279b4f5ddc8cf.jpg)
我有两个正在运行的线程,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结构中的线程,但现在无法读取用户的输入.
如果有人能够显示出一种能够获取用户输入并在用户未及时提供任何输入(中断器处于睡眠状态时)中断UserInputThread的方法,请执行此操作.
内容总结
以上是互联网集市为您收集整理的java-中断线程以等待用户输入,然后退出应用程序全部内容,希望文章能够帮你解决java-中断线程以等待用户输入,然后退出应用程序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。