1 sleep方法详解
作用:让线程在预期的时间执行,其他时候不要占用CPU资源(一旦执行了sleep之后,线程处于阻塞状态,不在占用CPU资源。直到下次被调度之后才会使用CPU资源)
应用场景:一个空调的程序需要每分钟检查一下空气温度器,如果温度过高则加大风速
特点:
-
不释放锁
- 包括synchronized和lock
- 和wait不同,他不会谦让锁,她睡觉时也拿着锁不释放
-
sleep可以响应中断
-
抛出InterruptedException
-
清除中断状态
/** * 每隔一秒钟输出当前时间,被中断,观察 * Thread.sleep() * TimeUnit.SECONDS.sleep() */ public class SleepInterrupted implements Runnable{ public static void main (String[] args) throws InterruptedException { Thread thread = new Thread(new SleepInterrupted()); thread.start(); Thread.sleep(6500); thread.interrupt(); } @Override public void run () { for (int i = 0; i < 10; i++) { System.out.println(new Date()); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { System.out.println("我被中断了"); e.printStackTrace(); } } } }
输出
Fri May 10 18:44:32 CST 2024 Fri May 10 18:44:33 CST 2024 Fri May 10 18:44:34 CST 2024 Fri May 10 18:44:35 CST 2024 Fri May 10 18:44:36 CST 2024 Fri May 10 18:44:37 CST 2024 Fri May 10 18:44:38 CST 2024 我被中断了 Fri May 10 18:44:38 CST 2024 java.lang.InterruptedException: sleep interrupted Fri May 10 18:44:39 CST 2024 Fri May 10 18:44:40 CST 2024
注意到上面的sleep写法
TimeUnit.SECONDS.sleep()
好处:- 可以指定参数单位HOURS、MINUTES、SECONDS
- 对负数进行异常处理
-
总结:sleep方法可以让线程进入Waiting状态,并且不占用CPU资源,但是不释放锁,直到规定时间后再执行,休眠期间如果被中断,会抛出异常并清除中断状态
面试题:
wait/notify和sleep方法的异同?(方法属于哪个对象?线程状态怎么切换?)
相同
- Wait和sleep方法都可以使线程阻塞,对应线程状态是Waiting或Time_Waiting。
- wait和sleep方法都可以响应中断Thread.interrupt()。
不同
- wait方法的执行必须在同步方法中进行,而sleep则不需要。(背后的原因说一下)
- 在同步方法里执行sleep方法时,不会释放monitor锁,但是wait方法会释放monitor锁。
- sleep方法短暂休眠之后会主动退出阻塞,而没有指定时间的 wait方法则需要被其他线程中断后才能退出阻塞。
- wait()和notify(),notifyAll()是Object类的方法,sleep()和yield()是Thread类的方法(背后的原因说一下)
2 join方法详解
作用:因为新的线程加入我们,所以我们要等他执行完再出发
用法:main等待thread1执行完毕,注意谁等谁
public class A implements Runnable{
public static void main (String[] args) throws InterruptedException {
Thread thread1 = new Thread(new A());
thread1.join();
}
}
2.1 join三个例子
普通用法
/**
* 演示join,注意语句输出顺序,会变化
*/
public class Join {
public static void main (String[] args) throws InterruptedException {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run () {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "执行完毕");
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run () {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "执行完毕");
}
});
thread1.start();
thread2.start();
System.out.println("开始等待子线程运行完毕");
thread1.join();
thread2.join();
System.out.println("所有子线程执行完毕");
}
}
输出
开始等待子线程运行完毕
Thread-1执行完毕
Thread-0执行完毕
所有子线程执行完毕
现在我们为了表明join方法的重要性,我们注释掉上面代码中的所有的join方法
输出
开始等待子线程运行完毕
所有子线程执行完毕
Thread-1执行完毕
Thread-0执行完毕
遇到中断
public class JoinInterrupt {
public static void main (String[] args) {
Thread main = Thread.currentThread();
Thread t = new Thread(new Runnable() {
@Override
public void run () {
try {
main.interrupt();
Thread.sleep(1000);
System.out.println("thread1 finish.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
System.out.println("等待子线程运行完毕");
try {
t.join();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName());
e.printStackTrace();
}
System.out.println("子线程已经运行完毕");
}
}
输出
主线程被中断了,但是,子线程还没有中断。这里涉及到线程中断的技巧:主线程中断子线程也要中断。
public class JoinInterrupt {
public static void main (String[] args) {
Thread main = Thread.currentThread();
Thread t = new Thread(new Runnable() {
@Override
public void run () {
try {
main.interrupt();
Thread.sleep(1000);
System.out.println("thread1 finish.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
System.out.println("等待子线程运行完毕");
try {
t.join();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"被中断了");
t.interrupt();//处理子线程中断问题
}
System.out.println("子线程已经运行完毕");
}
}
join期间,线程到底是什么状态?Waiting
public class JoinThreadState {
public static void main (String[] args) throws InterruptedException {
Thread main = Thread.currentThread();
Thread thread = new Thread(new Runnable() {
@Override
public void run () {
try {
Thread.sleep(3000);
System.out.println(main.getState());
System.out.println("Thread-0运行结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
System.out.println("等待子线程运行完毕");
thread.join();
System.out.println("子线程运行结束");
}
}
输出
等待子线程运行完毕
WAITING
Thread-0运行结束
子线程运行结束
2.2 join注意点
如果业务中出现了一个线程等待另一个线程,我们尽量不要使用join,尽量使用CountDownLatch或CyclicBarrier类。因为这些类更成熟更安全
2.3 join原理
public final void join() throws InterruptedException {
join(0);
}
public final synchronized void join(final long millis) throws InterruptedException {
if (millis > 0) {
if (isAlive()) {
final long startTime = System.nanoTime();
long delay = millis;
do {
wait(delay);
} while (isAlive() && (delay =
millis - TimeUnit.NANOSECONDS.toMillis(System.nanoTime() -startTime)) > 0);
}
} else if (millis == 0) {
while (isAlive()) {
wait(0);//参数0:表示它一直是等待直到被唤醒
}
} else {
throw new IllegalArgumentException("timeout value is negative");
}
}
当join方法参数是0时,我们一直休眠。请问谁来进行唤醒操作?
因为每一个Thread类,它在run方法运行结束后会自动地执行notify操作(这就是为什么我们不建议使用Thread类来调用wait方法 )
来看一下wait方法的C++源码:
static void ensure_join(JavaThread* thread){
Handle threadObj(thread,thread->threadObj());
ObjectLocker lock(threadObj,thread);
thread->clear_pending_exception();
java_lang_Thread::set_thread_status(threadObj(),java_lang_Thread::TERMINATED);
java_lang_Thread::set_thread(threadObj(),NULL);
lock.notify_all(thread);//这里执行了notify_all,进行了wait的唤醒
thread->clear_pending_exception();
}
join等价代码
public class JoinPrinciple {
public static void main (String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run () {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "执行完毕");
}
});
thread.start();
System.out.println("开始等待子线程运行完毕");
// thread.join();
synchronized (thread){
thread.wait();
}
System.out.println("所有子线程执行完毕");
}
}
2.4 面试题
在join期间,线程处于哪种状态?
子线程加入到main线程中,main线程处于waiting