public static void main(String[] args) throws InterruptedException {
QueueThreadWaitNotify a = new QueueThreadWaitNotify();
//很多个线程因为要访问lock对象阻塞,
//排成队列,等待资源的释放。
for (int i = 1; i <=16; i++) {
new Thread(a, String.valueOf(i)).start();
}
//先将main主线程休眠一段时间
Thread.sleep(5000);
//开启对阻塞队列的唤醒。
new Thread(a, "WakeUp").start();
}
public void run() {
String name = Thread.currentThread().getName();
if (name.equals("WakeUp")) {
System.out.println("WakeUp get object before ---" + name);
synchronized (lock) {
System.out.println("WakeUp get object after ---" + name);
System.out.println("WakeUp get object" + name);