Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Buffer{
- private Mutex m1, m2;
- private int n;
- private in k;
- private ReentrantLock lock;
- object obj;
- public Buffer(int n, int k){
- lock = new ReentrantLock();
- m = new Mutex();
- this.n = n;
- this.k = k;
- obj = new Object();
- }
- public void await() {
- lock.lock();
- m2.lock();
- // atomic Substraction
- m1.lock();
- n--;
- m1.unlock();
- while(n > 0 && k > 0){
- m2.unlock();
- lock.unlock();
- obj.wait();
- lock.lock();
- m2.lock();
- }
- m2.unlock();
- // atomic substraction
- m2.lock();
- k--;
- if(k>0) obj.notify();
- m2.unlock();
- lock.unlock();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement