package edu.nyu.cs9053.concurrency.chapter14;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * User: blangel
 * Date: 7/26/14
 * Time: 10:42 AM
 */
public class SynchronizedRewritten {

    public final Lock lock = new ReentrantLock();

    public final Condition condition = lock.newCondition();

    private int value;

    public void print() throws InterruptedException {
        lock.lock();
        try {
            while (value < 100) {
                condition.await();
            }
            System.out.printf("Value is %d%n", value);
        } finally {
            lock.unlock();
        }
    }

    public void process() {
        lock.lock();
        try {
            value += 1;
            if (value >= 100) {
                condition.signalAll();
            }
        } finally {
            lock.unlock();
        }
    }

}
