sleep和interrupt的关系

sleep和interrupt的关系。

* 因为interrupt在 java 内部实际上是设定了一个标志位叫 interrupt status ,可以中断任何阻塞状态,包括 sleep 在内,在 sleep 那行要阻塞的时候看到这个标志位后自己抛出了InterruptedException *

换句话说,先sleep,后才能interrupt。 sleep是阻塞,线程会暂时停在这里。interrupt 是打断。只有阻塞的线程被打断了,才会报这个异常。如:其他前程要stop这个线程。如系统直接exit了。才会发生通知。就是告诉你不要在等了,只有线程阻塞了(sleep或wait吧大概)才有可能被打断。所以系统才要你处理这个异常。

打断后你不处理异常,继续执行循环,线程重新继续跑,所以interrupt变成false了。

PS:可用TimeUnit.SECONDS.sleep(5); 代替Thread.sleep();前者具有更好的可读性。

TimeUnit 表示给定单元粒度的时间段,它提供在这些单元中进行跨单元转换和执行计时及延迟操作的实用工具方法。TimeUnit 不维护时间信息,但是有助于组织和使用可能跨各种上下文单独维护的时间表示形式。

枚举常量摘要
MICROSECONDS 微秒 一百万分之一秒(就是毫秒/1000)
MILLISECONDS 毫秒 千分之一秒
NANOSECONDS 毫微秒 十亿分之一秒(就是微秒/1000)
SECONDS 秒
MINUTES 分钟
HOURS 小时
DAYS 天


以下代码转自《Java7并发编程实战手册》

import java.util.concurrent.TimeUnit;
import com.packtpub.java7.concurrency.chapter1.recipe5.task.FileClock;

/**
 * Main class of the Example. Creates a FileClock runnable object
 * and a Thread to run it. Starts the Thread, waits five seconds
 * and interrupts it. 
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Creates a FileClock runnable object and a Thread
        // to run it
        FileClock clock=new FileClock();
        Thread thread=new Thread(clock);

        // Starts the Thread
        thread.start();
        try {
            // Waits five seconds
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        };
        // Interrupts the Thread
        thread.interrupt();
    }
}
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Class that writes the actual date to a file every second
 * 
 */
public class FileClock implements Runnable {

    /**
     * Main method of the class
     */
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.printf("%s: %s\n", Thread.currentThread().getName(), new Date());
            try {
                // Sleep during one second
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                System.out.println("The FileClock has been interrupted: "+Thread.currentThread().getName());
                //break; 如果不处理,线程会继续走下去
            }
        }
    }
}

如果加上break的话,结果为

Thread-0: Tue Jul 05 23:53:08 CST 2016
Thread-0: Tue Jul 05 23:53:09 CST 2016
Thread-0: Tue Jul 05 23:53:10 CST 2016
Thread-0: Tue Jul 05 23:53:11 CST 2016
Thread-0: Tue Jul 05 23:53:12 CST 2016
The FileClock has been interrupted: Thread-0

如果不加break,结果为

Thread-0: Tue Jul 05 23:56:48 CST 2016
Thread-0: Tue Jul 05 23:56:49 CST 2016
Thread-0: Tue Jul 05 23:56:50 CST 2016
Thread-0: Tue Jul 05 23:56:51 CST 2016
Thread-0: Tue Jul 05 23:56:52 CST 2016
The FileClock has been interrupted: Thread-0
Thread-0: Tue Jul 05 23:56:53 CST 2016
Thread-0: Tue Jul 05 23:56:54 CST 2016
Thread-0: Tue Jul 05 23:56:55 CST 2016
Thread-0: Tue Jul 05 23:56:56 CST 2016
Thread-0: Tue Jul 05 23:56:57 CST 2016

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值