solitaryclown

Interrupt()

2021-12-27
solitaryclown

1. interrupt

对于正常运行的线程t1,调用t1.interrupt()只会设置其打断标记为true,不会影响其继续执行。

如果想要线程被调用interrupt()后结束,可以通过isInterrupted()判断打断标记。

package com.huangbei.test;
/*
interrupt()

 */
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

@Slf4j(topic = "c.Test-interrupt")
public class TestInterrupt {


    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            log.debug("死循环...");
            while (!Thread.currentThread().isInterrupted()){

            }
            log.debug("线程结束.");
            /*try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
*/
        },"t1");

        t1.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t1.interrupt();
        log.debug("t1被打断了.");
        log.debug("{}",t1.isInterrupted());

    }
}

而对于处于sleep、wait状态的线程,调用interrupt()会抛出异常且打断标记不会被设置为真,所以,如果想要


下一篇 工作线程

Comments

Content