码迷,mamicode.com
首页 > 编程语言 > 详细

java线程中断和终止线程运行

时间:2015-09-16 00:51:03      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

ava中启动一个线程很容易,通常情况下我们都是等到任务运行结束后让线程自行停止。但有时需要在任务正在运行时取消他们,使得线程快速结束。对此Java并没有提供任何机制。但是我们可以通过Java提供的线程中断机制来实现。

首先来看Thread类三个和中断有关的方法:

public class Thread {
    // 发出一个中断请求,把标志位设定为中断状态,不会终止线程运行。
    // 其他线程试图调用该方法,会检测是否有权限中断该线程(正常情况
    // 下不会存在权限问题,这里可以忽略)
    public void interrupt() { ... }

    // 检测标志位是否为中断的状态
    public boolean isInterrupted() { ... }

    // 清除当前线程的标志位的中断状态,返回是否为中断状态
    public static boolean interrupted() { ... }

    ...
}

既然线程中断不会终止线程的运行,那么如何通过线程中断来实现终止线程运行呢?

我们知道一些阻塞线程的方法会抛出InterruptedException表示线程中断发生,在这种情况下就可以使用线程中断来终止线程的运行:

public class TestInterrupt {

    public static void main(String[] args)
    {
        BlockingQueue<Object> ObjectQueue = new LinkedBlockingQueue<Object>();
        Consumer consumer = new Consumer(ObjectQueue);

        Thread t = new Thread(consumer);
        t.start();

        // 等待线程的启动
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        // 中断线程
        t.interrupt();
    }

}

class Consumer implements Runnable
{
    private final BlockingQueue<Object> ObjectQueue;
    public Consumer(BlockingQueue<Object> ObjectQueue)
    {
        if (ObjectQueue == null)
        {
            throw new IllegalArgumentException("messageQueue cannot be null");
        }

        this.ObjectQueue = ObjectQueue;
    }

    @Override
    public void run()
    {
        boolean isRunning = true;
        while (isRunning)
        {
            try
            {
                // take方法阻塞时会因为线程中断抛出中断异常
                System.out.println(ObjectQueue.take());
            }
            catch (InterruptedException e)
            {
          // 一旦抛出中断异常,线程的中断状态就会被清除,这个时候调用
                // Thread的isInterrupted()方法返回的是false
                isRunning = false;
                System.out.println("Cancelled");
            }
        }
    }
}

很多任务执行的服务程序的逻辑和上面的例子很类似,都可以使用这种方法来终止线程的运行。

java线程中断和终止线程运行

标签:

原文地址:http://www.cnblogs.com/zhangxiaopeng/p/4811844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!