Java??????????????????????
???????????? ???????[ 2013/6/20 10:51:33 ] ????????
???????????????????
????????????wait??notify??notifyAll?????????????????????????е???????????????????????????????????????????????????????????У?????????????????????????????????????????????wait???????????????wait set?У?????wait set??????????С?
???????????????????????????????????????????????????????????檔????????????????????????????????????????????
???????е?????????????????????????????????С??????????????????????ν???????????????????ν?????????????????????????????Щ??????????????????????????????β???????ν????????????????????????????????????????ж????????????????
??????????????????????????????????е?Guarded Suspension Pattern??????????
<pre class="brush:java;toolbar:false;">
public synchronized void concreteWork()
{
while(!condition)
{
try{
wait();//???????????????this??????????????е??
}catch(InterruptedException e)
{ }
}
//????????????????ν????????е?
}
//?????????????wait?????wait??????????????ν?????
???????????????
????????????????????????????????????ν???????????????????????????????????Object??notifyAll??notify????????????notifyAll???????????????????????л???????????????????????е?????????й??????????????????
??????????????????????????????????????????????????????ν??????notify??????????????????????磬?????A???????????е??????ν??PA?????B???????????е??????ν??PB???????C?????????????PB????棬???????notify????????????A??A?????????PA???????棬????????????????B?????PB????棬??????????????????ε?????
???????notify???????????????“???е???????????????????ν??”??“??ζ??????????????????”??
????ReentrantLock??Lock?????????????????????????Condition??????????????????????С?????????????????????????????е???????????ν?????????????????й???????????????????ν?????ò???????????????????
??????????Condition?????Lock????????????????????????????????????й??????????????С??????Condition???????????????Lock?????Lock.newCondition?????????Lock??????????Condition??????Condition?У???Object??wait??notify??notifyAll??????????????await??signal??signalAll??
?????????Condition?????????????е????
???????????????????????У????????ù?????л???????????????????????????????????Condition??
????????????????????Condition??Lock???????н綹?棬??????????????????
package whut.usersynchrnoized;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//????Condition??Lock??????н綹??
public class ConditionBoundedBuffer<T> {
private final Lock lock = new ReentrantLock();
private final Condition full = lock.newCondition();
private final Condition empty = lock.newCondition();
private final T[] items;
private int tail?? head?? count;
public ConditionBoundedBuffer(int size) {
items = (T[]) new Object[size];
}
// ???????notfull
public void put(T x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
full.await();// ??????????full?????????е??
items[tail] = x;
if (++tail == items.length)
tail = 0;
++count;
empty.signal();// ??λ?????????
} finally {
lock.unlock();
}
}
// ???????notEmpty
public T take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
empty.await();// ??????????empty?????????е??
T x = items[tail];
items[tail] = null;
if (++head == items.length)
head = 0;
--count;
full.signal();// ??λ?????????
return x;
} finally {
lock.unlock();
}
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11