线程中断后执行

问题描述

一个线程正在执行任务以打印从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上停止并继续执行的原因。
在控制台上看到输出时,看起来线程正在停止(通过查看异常跟踪)。

引用interrupt interrupted isinterrupted in Java

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...