• java中终止线程的三种方式


    在java中有三种方式可以终止线程。分别为:

      1.  使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。

         2.  使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。

           3.  使用interrupt方法中断线程。

    下面我们来详细的介绍这三种方法。

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

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

    package chapter2;  
      
    public class ThreadFlag extends Thread  
    {  
        public volatile boolean exit = false;  
      
        public void run()  
        {  
            while (!exit);  
        }  
        public static void main(String[] args) throws Exception  
        {  
            ThreadFlag thread = new ThreadFlag();  
            thread.start();  
            sleep(5000); // 主线程延迟5秒  
            thread.exit = true;  // 终止线程thread  
            thread.join();  
            System.out.println("线程退出!");  
        }  
    }  
    

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

    2. 使用stop方法终止线程

    使用stop方法可以强行终止正在运行或挂起的线程。我们可以使用如下的代码来终止线程:

    thread.stop();  
    

      虽然使用上面的代码可以终止线程,但使用stop方法是很危险的,就象突然关闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果,因此,并不推荐使用stop方法来终止线程。

    3. 使用interrupt方法终止线程

    使用interrupt方法来终端线程可分为两种情况:

        (1)线程处于阻塞状态,如使用了sleep方法。

        (2)使用while(!isInterrupted()){……}来判断线程是否被中断。

        在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出。下面的代码演示了在第一种情况下使用interrupt方法。

    package chapter2;  
      
    public class ThreadInterrupt extends Thread  
    {  
        public void run()  
        {  
            try  
            {  
                sleep(50000);  // 延迟50秒  
            }  
            catch (InterruptedException e)  
            {  
                System.out.println(e.getMessage());  
            }  
        }  
        public static void main(String[] args) throws Exception  
        {  
            Thread thread = new ThreadInterrupt();  
            thread.start();  
            System.out.println("在50秒之内按任意键中断线程!");  
            System.in.read();  
            thread.interrupt();  
            thread.join();  
            System.out.println("线程已经退出!");  
        }  
    }  
    

      上面代码的运行结果如下:

    在50秒之内按任意键中断线程!  
      
    sleep interrupted  
    线程已经退出!   
    

      

    在调用interrupt方法后, sleep方法抛出异常,然后输出错误信息:sleep interrupted.

        注意:在Thread类中有两个方法可以判断线程是否通过interrupt方法被终止。一个是静态的方法interrupted(),一个是非静态的方法isInterrupted(),这两个方法的区别是interrupted用来判断当前线是否被中断,而isInterrupted可以用来判断其他线程是否被中断。因此,while (!isInterrupted())也可以换成while (!Thread.interrupted())。

    文章出自 http://www.bitscn.com/pdb/java/200904/161228_3.html

  • 相关阅读:
    jQuery检测滚动条(scroll)是否到达底部
    sql group by
    hbm.xml 详解总结
    net.sf.json 时间格式的转化
    经典SQL语句大全
    HashTable
    in与exist , not in与not exist 的区别
    网页布局常用的一些命名规则和书写
    什么是SOA?
    sql之left join、right join、inner join的区别
  • 原文地址:https://www.cnblogs.com/of-fanruice/p/7522201.html
Copyright © 2020-2023  润新知