终止线程 中断标志 vs Interrupt() vs stop()

退出标志

import lombok.SneakyThrows;

import java.text.SimpleDateFormat;

public class T {

    static boolean flag = true;

    @SneakyThrows
    public static void main(String[] args) {

        Thread thread1 = new Thread(() -> {
            try {
                while (flag) {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + ":"
                            + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").
                            format(System.currentTimeMillis()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "线程1");

        thread1.start();

        Thread.sleep(6000);
        flag = false;
    }
}

interrupt()

打断阻塞的线程(sleep/wait/joing),线程会抛出InterruptException异常
打断正常的线程,可以根据打断状态标记是否退出线程

阻塞线程

wait()

/*
java.lang.IllegalMonitorStateException
**/
public class T {

    @SneakyThrows
    public static void main(String[] args) {

        Object o = new Object();

        Thread thread1 = new Thread(() -> {
            try {
                while (true) {
                    synchronized (o) {
                        //wait(timeout) 不管timeout设置多大,
                        //都会InterruptedException
                        o.wait();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "线程1");

        thread1.start();
        thread1.interrupt();
        Thread.sleep(1000);
    }
}

sleep()

/*
线程1:2024-08-14 07:50:48 110
线程1:2024-08-14 07:50:48 312
java.lang.InterruptedException: sleep interrupted
**/
public class T {

    @SneakyThrows
    public static void main(String[] args) {

        Thread thread1 = new Thread(() -> {
            try {
                while (true) {
                    Thread.sleep(200);
                    System.out.println(Thread.currentThread().getName() + ":"
                            + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").
                            format(System.currentTimeMillis()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "线程1");

        thread1.start();

        Thread.sleep(500);
        thread1.interrupt();
    }
}

join()

/*
java.lang.InterruptedException
**/
public class T {

    @Test
    public void m() throws Exception {

        Thread thread1 = new Thread() {
            @Override
            public void run() {
                try {
                    //保证执行到主线程thread2.interrupt();
                    //此线程还未执行完
                    this.join(6000);
                } catch (InterruptedException e) {

                }
            }
        };

        Thread thread2 = new Thread(() -> {
            try {
                while (true) {
                    thread1.join();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "线程2");


        thread1.start();
        thread2.start();

        Thread.sleep(1000);//保证线程1、2 先于 main线程执行
        thread2.interrupt();
    }
}

非阻塞线程

/*
正常退出
**/
public class T {

    static boolean flag = true;

    @SneakyThrows
    public static void main(String[] args) {

        Thread thread1 = new Thread(() -> {
            try {
                while (true) {
                    System.out.println(Thread.currentThread().getName() + ":"
                            + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").
                            format(System.currentTimeMillis()));
                    Thread currentThread = Thread.currentThread();
                    boolean interrupted = currentThread.isInterrupted();
                    if (interrupted) {
                        System.out.println("打断状态" + interrupted);
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "线程1");

        thread1.start();

        Thread.sleep(1000);
        thread1.interrupt();
    }
}

stop()

stop方法的作用是强制结束线程,就和kill -9一个效果,如果用stop来停止线程会直接结束线程,无视线程中对数据逻辑操作的完整性,这样是不安全。

import lombok.SneakyThrows;

import java.text.SimpleDateFormat;

public class T {
    @SneakyThrows
    public static void main(String[] args) {

        Thread thread1 = new Thread(() -> {
            try {
                while (true) {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + ":"
                            + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").
                            format(System.currentTimeMillis()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "线程1");

        thread1.start();

        Thread.sleep(7000);
        thread1.stop();
    }
}
posted @ 2024-08-14 12:13  干饭达人GoodLucy  阅读(14)  评论(0)    收藏  举报