如何解决线程中断后执行
一个线程正在执行任务以打印从0到n的数字,并在每个打印语句之后休眠4000毫秒。中间线程中的某处被中断。现在,当同一线程开始执行时,它将从哪里开始,它将再次从0到n打印数字,或者从中断的地方打印数字。 在这两种情况下,其原因是什么以及如何处理?
public class Main {
public static void main(String[] args) throws InterruptedException {
SleepTest sleepTest = new Sleeptest();
Thread thread = new Thread(sleepTest);
thread.start();
thread.interrupt();
}
}
public class SleepTest implements Runnable{
static int sleep = 10;
public void run(){
for (int i =0; i<10; i++){
System.out.println(i);
try {
Thread.currentThread().interrupt();
Thread.sleep(4000);
} catch (InterruptedException exception) {
exception.printstacktrace();
}
System.out.println(Thread.interrupted());
}
}
解决方法
所有 for i in self.mergedVerticesList[v].iter() {
self.mergedVerticesList.get_mut(&merge_onto).unwrap().remove(i);
}
所做的就是将字段Thread.currentThread().interrupt()
的值更新为interrupted
。
让我们看看程序的流程以及如何为true
字段分配值:
interrupted
回答
从哪里开始,从0开始打印数字 再次返回n,否则它将从中断处打印数字。
调用中断不会导致它重新开始,因为在此调用时所做的所有操作都将值
public class Main {
public static void main(String[] args) throws InterruptedException {
SleepTest sleepTest = new SleepTest();
Thread thread = new Thread(sleepTest,"Sub Thread"); // Give a name to this thread
thread.start(); // main thread spawns the "Sub Thread". Value of "interrupted" - false
thread.interrupt(); // called by main thread. Value of "interrupted" - true
}
}
public class SleepTest implements Runnable{
static int sleep = 10;
public void run(){
System.out.println(Thread.currentThread().getName()+" "+Thread.interrupted()); // prints "Sub Thread true"
for (int i =0; i<10; i++){
System.out.println(i);
try {
Thread.currentThread().interrupt(); // no matter what value is for interrupted,it is assigned the value "true"
Thread.sleep(4000); // Can't call sleep with a "true" interrupted status. Exception is thrown. Note that,when the exception is thrown,the value of interrupted is "reset",i.e.,set to false
} catch (InterruptedException exception) {
exception.printStackTrace(); // whatever
}
System.out.println(Thread.interrupted()); // returns the value of interrupted and resets it to false
}
}
设置为false(并且未进行其他任何修改)。
在线程对象上调用apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2020-10-04 11:27:33 MDT; 6min ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 2413 ExecStart=/usr/sbin/apachectl start (code=exited,status=203/EXEC)
Oct 04 11:27:33 pidev.local systemd[1]: Starting The Apache HTTP Server...
Oct 04 11:27:33 pidev.local systemd[2413]: apache2.service: Failed to execute command: No such file or directory
Oct 04 11:27:33 pidev.local systemd[2413]: apache2.service: Failed at step EXEC spawning /usr/sbin/apachectl: No such file or directory
Oct 04 11:27:33 pidev.local systemd[1]: apache2.service: Control process exited,code=exited,status=203/EXEC
Oct 04 11:27:33 pidev.local systemd[1]: apache2.service: Failed with result 'exit-code'.
Oct 04 11:27:33 pidev.local systemd[1]: Failed to start The Apache HTTP Server.
只能建议线程停止。不保证线程将停止。
这完全取决于interrupt()
线程方法的实现。
在run()
中,您正在捕获run()
,并且正在打印异常跟踪,但未停止线程。这就是为什么线程永远不会在InterruptedException
上停止并继续执行的原因。
在控制台上看到输出时,看起来线程正在停止(通过查看异常跟踪)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。