public final void wait() throws InterruptedException {
wait(0);
}
public final native void wait(long timeout) throws InterruptedException;
1
2
3
4
和上一篇文章一样,咱们先看官方文档:
Causes the current thread to wait until another thread invokes the {@link java.lang.Object#notify()} method or the {@link java.lang.Object#notifyAll()} method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
The current thread must own this object’s monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object’s monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) {
while (<condition does not hold>)
obj.wait(timeout, nanos);
… //Perform action appropriate to condition
}
This method should only be called by a thread that is the owner of this object’s monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
核心内容归纳如下:
Wakes up a single thread that is waiting on this object’s monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object’s monitor by calling one of the wait methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object’s monitor. A thread becomes the owner of the object’s monitor in one of three ways:
By executing a synchronized instance method of that object.
By executing the body of a synchronized statement that synchronizes on the object.
For objects of type Class, by executing a synchronized static method of that class.
Only one thread at a time can own an object’s monitor.
核心内容解释如下:
//初始化生产者
Producer producer1 = new Producer(storage,30);
Producer producer2 = new Producer(storage,40);
Producer producer3 = new Producer(storage,50);
Producer producer4 = new Producer(storage,60);
Producer producer5 = new Producer(storage,70);
//初始化消费者
Consumer consumer1 = new Consumer(storage,30);
Consumer consumer2 = new Consumer(storage,40);
Consumer consumer3 = new Consumer(storage,50);
Consumer consumer4 = new Consumer(storage,60);
Consumer consumer5 = new Consumer(storage,70);
new Thread(producer1).start();
new Thread(producer2).start();
new Thread(producer3).start();
new Thread(producer4).start();
new Thread(producer5).start();
new Thread(consumer1).start();
new Thread(consumer2).start();
new Thread(consumer3).start();
new Thread(consumer4).start();
new Thread(consumer5).start();
}
/**
* Storage的实现类。采用Lock、多个Condition去实现同步
* @Author: cherry
* @Date: Created in 2018/9/27 19:44
*/
public class StorageConditionImpl implements Storage {
/**
* 队列最大容量
*/
public static final int MAX_SIZE = 100;
/**
* 仓库中,存放产品的容器
*/
private LinkedList<Object> queue = new LinkedList<>();
/**
* @Author: cherry
* @Date: Created in 2018/9/27 21:25
*/
public class StorageBlockQueueImpl implements Storage {
/**
* 队列最大容量
*/
public static final int MAX_SIZE = 100;
/**
* 阻塞队列
*/
private LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>(MAX_SIZE);
@Override
public void produce(int num) {
for (int i = 0; i < num; i++) {
try {
//自动阻塞
queue.put(new Object());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void consume(int num) {
for (int i = 0; i < num; i++) {
try {
//自动阻塞
queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
---------------------
【转载】
作者:cherry-peng
原文:https://blog.csdn.net/xsp_happyb ... 417?utm_source=copy