1.sleep(millis)是Thread的静态方法,wait()是Object类里的方法;
2.sleep(millis)不需要monitor,而wait()需要monitor;
3.sleep(millis)不会释放monitor,而wait()会释放锁,加到monitor的等待队列中(这里指的是线程未结束前,线程结束后的monitor肯定是被释放的);
4.sleep(millis)的线程不需要被唤醒,wait()的线程需要被唤醒(wait(millis)加时间参数的话不需要);
package com.wangzerui.concurrency.chapter9;
import java.util.stream.Stream;
/**
* 1.sleep(millis)是Thread的静态方法,wait()是Object类里的方法;
* 2.sleep(millis)不需要monitor,而wait()需要monitor;
* 3.sleep(millis)不会释放monitor,而wait()会释放锁,加到monitor的等待队列中(这里指的是线程未结束前,线程结束后的monitor肯定是被释放的);
* 4.sleep(millis)的线程不需要被唤醒,wait()的线程需要被唤醒(wait(millis)加时间参数的话不需要);
* @author wangzerui
*/
public class DifferenceOfWaitAndSleep {
private static final Object LOCK = new Object();
public static void main(String[] args) {
// testSleep();
// testWaitDoNotHasMonitor();
// testWaitHasMonitor();
// testSleepReleaseMonitor();
// testWaitDoNotReleaseMonitorsAndDoNotNotify();
testWaitDoNotReleaseMonitorsAndNotify();
}
/**
* 场景:sleep(millis)不加monitor
* 运行结果:正常,程序正常结束
* 分析:
* sleep(millis)不需要monitor;
* sleep(millis)的线程不需要被唤醒,
*/
public static void testSleep() {
synchronized(LOCK) {
try {
System.out.println("testSleep方法开始");
Thread.sleep(1000);
System.out.println("testSleep方法结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 场景:wait不加monitor
* 运行结果:会报java.lang.IllegalMonitorStateException
* 分析:
* wait()需要monitor;
*/
public static void testWaitDoNotHasMonitor() {
try {
System.out.println("testWait方法开始");
LOCK.wait();
System.out.println("testWait方法结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 场景:wait加monitor
* 运行结果:正常,程序不会结束
* 分析:
* wait()需要monitor;
* wait()的线程需要被唤醒(wait(millis)加时间参数的话不需要);
*/
public static void testWaitHasMonitor() {
synchronized(LOCK) {
try {
System.out.println("testWait方法开始"+Thread.currentThread().getName());
LOCK.wait();
System.out.println("testWait方法结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 场景:多线程测试sleep()释放monitor
* 运行结果:
* testSleep方法开始
* testSleep方法结束
* testSleep方法开始
* testSleep方法结束
* 分析:
* sleep(millis)不会释放monitor
*/
public static void testSleepReleaseMonitor() {
Stream.of("T1","T2").forEach(name -> new Thread(name) {
@Override
public void run() {
testSleep();
};
}.start());
}
/**
* 场景:多线程测试wait()不释放monitor,不唤醒情况下
* 运行结果:正常,程序不会结束
* testWait方法开始
* testWait方法开始
* 分析:
* wait()的线程需要被唤醒(wait(millis)加时间参数的话不需要)
*/
public static void testWaitDoNotReleaseMonitorsAndDoNotNotify() {
Stream.of("T1","T2").forEach(name -> new Thread(name) {
@Override
public void run() {
testWaitHasMonitor();
};
}.start());
}
/**
* 场景:多线程测试wait()不释放monitor,唤醒情况下
* 运行结果:正常,程序不会结束
* testWait方法开始
* testWait方法开始
* 分析:
* wait()的线程需要被唤醒(wait(millis)加时间参数的话不需要)
* @throws InterruptedException
*/
public static void testWaitDoNotReleaseMonitorsAndNotify() {
Stream.of("T1","T2").forEach(name -> new Thread(name) {
@Override
public void run() {
testWaitHasMonitor();
};
}.start());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Stream.of("Notify1","Notify2").forEach(name -> new Thread(name) {
@Override
public void run() {
synchronized(LOCK) {
LOCK.notify();
}
};
}.start());
}
}