线程休眠
sleep方法
方法名 | 作用 |
---|---|
static void sleep(long millis) | 让当前线程休眠millis秒 |
- 静态方法:Thread.sleep(1000);
- 参数是毫秒
- 作用: 让当前线程进入休眠,进入“
阻塞状态
”,放弃占有CPU时间片,让给其它线程使用。
这行代码出现在A线程中,A线程就会进入休眠。
这行代码出现在B线程中,B线程就会进入休眠。 - Thread.sleep()方法,可以做到这种效果:
间隔特定的时间,去执行一段特定的代码,每隔多久执行一次。
代码:
public class ThreadTest06 {
public static void main(String[] args) {
for(int i = 0; i < 10; i++){
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程中断
方法名 | 作用 |
---|---|
void interrupt() | 终止线程的睡眠 |
boolean interrupted() | 测试当前线程是否已经中断(静态方法)。如果连续调用该方法,第一次调用返回true,第二次调用将返回false。interrupted()方法具有清除状态的功能 |
boolean isInterrupted() | 判断当前线程是否中断(非静态方法)不会清楚状态 |
线程中断只对处于阻塞的线程有效,比如调用Thread.sleep()、join()、wait()方法后的线程进入阻塞状态
如果线程没有被阻塞,这时调用 interrupt()将不起作用
线程中断是抛出异常实现的,当线程处于阻塞状态时,调用线程中断方法,该线程内会抛出InterruptedException异常
interrupt
中断sleep()
public class ThreadTest06 {
public static void main(String[] args) {
Thread t=new Thread(){
@Override
public void run() {
System.out.println("start");
try {
Thread.sleep(1000*100);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
System.out.println("end");
}
};
t.start();
try {
Thread.sleep(1000*3);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
}
//输出:
start
interrupted
end
中断wait
只能中断位于synchronized
代码块中由于调用wait()
阻塞的线程
public class ThredTest09 {
public static void main(String[] args) {
Thread t = new Thread(){
private Object lock=new Object();
@Override
public void run() {
System.out.println("start");
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
System.out.println("end");
}
};
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
}
//运行
start
Interrupted
end
中断join
public class ThreadTest10 {
public static void main(String[] args) {
Thread t=new Thread(){
@Override
public void run() {
System.out.println("parent start");
Thread tt=new Thread(){
@Override
public void run() {
System.out.println("child start");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("child interrupted");
}
System.out.println("child end");
}
};
tt.start();
try {
tt.join();
} catch (InterruptedException e) {
System.out.println("parent interrupted");
}
System.out.println("parent end");
}
};
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
}
//输出
parent start
child start
parent interrupted
parent end
child end
isInterrupted
public class ThreadTest07 {
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run(){
System.out.println("start");
while(!isInterrupted()){
System.out.println("run...");
}
System.out.println("end:"+isInterrupted());
}
};
t.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
}
//输出
start
run...
run...
...
run...
run...
run...
end:true
interrupted
当调用为true后,会清除状态,再次调用interrupted或isInterrupted都会返回false
public class ThreadTest08 {
public static void main(String[] args) {
Thread thread=new Thread(){
@Override
public void run() {
System.out.println("interrupted:"+interrupted());
System.out.println("interrupted:"+interrupted());
while(!interrupted()){}
System.out.println("isInterrupted:"+isInterrupted());
System.out.println("interrupted:"+interrupted());
System.out.println("interrupted:"+interrupted());
}
};
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt();
}
}
//输出
interrupted:false
interrupted:false
isInterrupted:false
interrupted:false
interrupted:false
public class ThreadTest08 {
public static void main(String[] args) {
Thread thread=new Thread(){
@Override
public void run() {
while(!isInterrupted()){}
System.out.println("isInterrupted:"+isInterrupted());
System.out.println("interrupted:"+interrupted());
System.out.println("isInterrupted:"+isInterrupted());
System.out.println("interrupted:"+interrupted());
}
};
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt();
}
}
//输出
isInterrupted:true
interrupted:true
isInterrupted:false
interrupted:false
源码分析
查看interrupted
源码,发现该方法调用当前线程的isInterrupted
方法,并传入一个true
的布尔值参数
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
查看有参isInterrupted
方法源码,发现该方法有个ClearInterrupted
布尔类型的参数,用于控制是否清除中断状态,所以interrupted
方法在调用时默认会清除中断中断状态(true->false)
在调用无参的isInterrupted
方法时,isInterrupted
无参方法会调用isInterrupted(boolean ClearInterrupted)
方法,并传入参数false
,控制不去清除中断状态
/**
* Tests if some Thread has been interrupted. The interrupted state
* is reset or not based on the value of ClearInterrupted that is
* passed.
*/
private native boolean isInterrupted(boolean ClearInterrupted);
public boolean isInterrupted() {
return isInterrupted(false);
}