java线程学习之停止方法

本文介绍了三种停止Java线程的方法:使用已弃用的suspend和stop方法、设置线程结束标志以及利用中断机制。通过具体实例展示了每种方法的实现过程及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程停止方法一:

已经不推荐使用

学习源代码:

package thread8;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Run {
    public static void main(String[] args) throws InterruptedException {
        ThreadA threadA = new ThreadA();
        threadA.start();
        Thread.sleep(10000);
        System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) + "suspend 暂停");
        threadA.suspend();
        Thread.sleep(10000);
        System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) + "resume 恢复");
        threadA.resume();
        Thread.sleep(10000);
        System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) + "stop 停止");
        threadA.stop();
    }
}

class ThreadA extends Thread {

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) + "ThreadA 执行中……");
        }
    }
}

执行结果:

2022-01-17 15:39:53ThreadA 执行中……
2022-01-17 15:39:54ThreadA 执行中……
2022-01-17 15:39:55ThreadA 执行中……
2022-01-17 15:39:56ThreadA 执行中……
2022-01-17 15:39:57ThreadA 执行中……
2022-01-17 15:39:58ThreadA 执行中……
2022-01-17 15:39:59ThreadA 执行中……
2022-01-17 15:40:00ThreadA 执行中……
2022-01-17 15:40:01ThreadA 执行中……
2022-01-17 15:40:02suspend 暂停
2022-01-17 15:40:12resume 恢复
2022-01-17 15:40:12ThreadA 执行中……
2022-01-17 15:40:13ThreadA 执行中……
2022-01-17 15:40:14ThreadA 执行中……
2022-01-17 15:40:15ThreadA 执行中……
2022-01-17 15:40:16ThreadA 执行中……
2022-01-17 15:40:17ThreadA 执行中……
2022-01-17 15:40:18ThreadA 执行中……
2022-01-17 15:40:19ThreadA 执行中……
2022-01-17 15:40:20ThreadA 执行中……
2022-01-17 15:40:21ThreadA 执行中……
2022-01-17 15:40:22stop 停止

方式二:

设置线程结束标志

package thread3;

public class Run {
    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.start();
        Thread.sleep(1000);
        System.out.println("线程结束标志");
        myThread.setFlag(false);
        System.out.println("线程已结束");
    }
}

class MyThread extends Thread {
    //线程停止标志
    private volatile boolean flag = true;
    private volatile int count = 0;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        while (flag) {
            count++;
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("执行中,count:" + count);
        }
    }

}

执行结果:

执行中,count:1
执行中,count:2
执行中,count:3
线程结束标志
线程已结束
执行中,count:4

方法三:

中断方法

package interrupted;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Run {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Thread main 开始,时间:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
        ThreadA threadA = new ThreadA();
        threadA.start();
        Thread.sleep(3);
        threadA.interrupt();
        System.out.println("Thread main 结束,时间:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
    }
}

class ThreadA extends Thread {

    @Override
    public void run() {
        while (true) {
            if (this.isInterrupted()) {
                System.out.println("执行中ThreadA,中断结束了," + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
                break;
            }
            System.out.println("执行中ThreadA,时间:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
        }
    }

}

正常停止,执行结果:

Thread main 开始,时间:2022-01-17 16:59:40:199
执行中ThreadA,时间:2022-01-17 16:59:40:202
执行中ThreadA,时间:2022-01-17 16:59:40:203
执行中ThreadA,时间:2022-01-17 16:59:40:204
执行中ThreadA,时间:2022-01-17 16:59:40:204
执行中ThreadA,时间:2022-01-17 16:59:40:204
执行中ThreadA,时间:2022-01-17 16:59:40:204
执行中ThreadA,时间:2022-01-17 16:59:40:204
Thread main 结束,时间:2022-01-17 16:59:40:205
执行中ThreadA,时间:2022-01-17 16:59:40:205
执行中ThreadA,中断结束了,2022-01-17 16:59:40:205

修改代码:

class ThreadA extends Thread {

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                System.out.println("执行中ThreadA,中断异常结束了," + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
                e.printStackTrace();
                break;
            }
            if (this.isInterrupted()) {
                System.out.println("执行中ThreadA,中断结束了," + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
                break;
            }
            System.out.println("执行中ThreadA,时间:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
        }
    }
}

执行结果:

Thread main 开始,时间:2022-01-17 17:04:00:160
Thread main 结束,时间:2022-01-17 17:04:00:165
执行中ThreadA,中断异常结束了,2022-01-17 17:04:00:165
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at interrupted.ThreadA.run(Run.java:23)

总结:当线程被 sleep() 休眠时,如果被中断,这会就抛出这个异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值