(六)如何停止线程?

如何停止线程?


一、停止线程思路

1、使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。 
2、使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。 
3、使用interrupt方法中断线程。

二、代码示例

1. 使用退出标志终止线程

当run方法执行完后,线程就会退出。 
但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。如果想让循环永远运行下去,可以使用while(true){……}来处理。但要想使while循环在某一特定条件下退出,最直接的方法就是设一个boolean类型的标志,并通过设置这个标志为true或false来控制while循环是否退出。下面给出了一个利用退出标志终止线程的例子。

停止线程

  • public class ThreadFlag extends Thread
    {
        public volatile boolean flag = false;
    
        @Override
        public void run()
        {
            while (!flag){
                System.out.println("线程运行");
            };
        }
        public static void main(String[] args) throws Exception
        {
            ThreadFlag thread = new ThreadFlag();
            thread.start();
            // 主线程延迟3秒
            sleep(3000);
            // 终止线程thread
            thread.flag = true;
            thread.join();
            System.out.println("线程退出!");
        }
    }
       thread2.start();
            int i = 0;
            while (true) {
                System.out.println("thread main..");
                if (i == 10) {
                    // stopThread1.stopThread();
                    thread1.interrupt();
                    thread2.interrupt();
                    break;
                }
                i++;
            }
        }
    }


在上面代码中定义了一个退出标志flag,当flag为true时,while循环退出,flag的默认值为false.在定义flag时,使用了一个Java关键字volatile,这个关键字的目的是使flag同步,也就是说在同一时刻只能由一个线程来修改flag的值,

运行结果:

这里写图片描述

2.使用stop方法终止线程

使用stop方法可以强行终止正在运行或挂起的线程。我们可以使用如下的代码来终止线程: 
thread.stop(); 
虽然使用上面的代码可以终止线程,但使用stop方法是很危险的,就像突然关闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果,因此,并不推荐使用stop方法来终止线程。

3.使用interrupt方法终止线程

使用interrupt方法来终端线程可分为两种情况: 
(1)线程处于阻塞状态,如使用了sleep方法。 
(2)使用while(!isInterrupted()){……}来判断线程是否被中断。 
在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出。下面的代码演示了在第一种情况下使用interrupt方法。

停止线程

  • class StopThread implements Runnable {
        private boolean flag = true;
    
        @Override
        public void run() {
            while (flag) {
                try {
                    sleep(1000);
                } catch (Exception e) {
                    //e.printStackTrace();
                    stopThread();
                }
                System.out.println("thread run..");
            }
        }
        /**
         * 停止线程
         */
        public void stopThread() {
            flag = false;
        }
    
    
    }


运行(停止线程demo)

  • public class StopThreadDemo {
        public static void main(String[] args) {
            StopThread stopThread1 = new StopThread();
            Thread thread1 = new Thread(stopThread1);
            Thread thread2 = new Thread(stopThread1);
            thread1.start();
            thread2.start();
            int i = 0;
            while (true) {
                System.out.println("thread main..");
                if (i == 10) {
                    // stopThread1.stopThread();
                    thread1.interrupt();
                    thread2.interrupt();
                    break;
                }
                i++;
            }
        }
    }


这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值