Code example for Lock

Methods: lock, newCondition, unlock

0
    /** 
     * Creates a new {@code WaitableSignal} in the non-signaled state. 
     */ 
    public WaitableSignal() { 
        this.lock = new ReentrantLock();
        this.waitSignal = lock.newCondition();
        this.signaled = false;
    } 
 
    private static WaitableSignal newSignalingSignal() { 
        WaitableSignal result = new WaitableSignal();
        result.signal();
        return result;
    } 
 
    /** 
     * Sets the state of this {@code WaitableSignal} to the signaled state and 
     * allows the {@code waitSignal} or the {@code tryWaitSignal} method to 
     * return immediately. 
     * <P> 
     * This method is idempotent. That is, calling this method multiple times 
     * has no further effect. 
     */ 
    public void signal() { 
        if (!signaled) {
            signaled = true;
            lock.lock();
            try { 
                waitSignal.signalAll();
            } finally { 
                lock.unlock();
            } 
        } 
    } 
 
    /**