Example usage for java.util.concurrent.locks Condition signalAll

List of usage examples for java.util.concurrent.locks Condition signalAll

Introduction

In this page you can find the example usage for java.util.concurrent.locks Condition signalAll.

Prototype

void signalAll();

Source Link

Document

Wakes up all waiting threads.

Usage

From source file:Main.java

/**
 * signal all on a condition/*  w  ww  . j  a v  a  2 s . c om*/
 * @param lock
 * @param cond
 */
public static void signalAll(ReentrantLock lock, Condition cond) {
    lock.lock();
    try {
        cond.signalAll();
    } finally {
        lock.unlock();
    }
}

From source file:com.bigdata.dastor.db.Memtable.java

public void flushAndSignal(final Condition condition, ExecutorService sorter, final ExecutorService writer) {
    cfs.getMemtablesPendingFlush().add(this); // it's ok for the MT to briefly be both active and pendingFlush
    writer.submit(new WrappedRunnable() {
        public void runMayThrow() throws IOException {
            cfs.addSSTable(writeSortedContents());
            cfs.getMemtablesPendingFlush().remove(Memtable.this);
            condition.signalAll();
        }//from ww  w  .j  a v a 2 s .  co  m
    });
}

From source file:com.edgenius.core.repository.SimpleRepositoryServiceImpl.java

/**
 * release space level lock. /*  w w w .  ja va 2s.c  o m*/
 */
private void releaseLock(String spacename, String identifier, String nodeUuid) {
    log.debug("Request - Release lock on space {} identifier {} node {} in {}",
            new String[] { spacename, identifier, nodeUuid, Thread.currentThread().getName() });

    try {
        writeLock.lock();

        ConditionGroup cond = spaceLockMap.get(spacename);
        if (cond == null) {
            AuditLogger.error("Unexpected case. No lock to relase for space " + spacename);
        }
        //maybe it is performance bottleneck
        if (identifier == null && nodeUuid == null) {
            //space level lock
            if (cond.spaceCondition != null) {
                //could have multiple threads waiting for this signal... 
                cond.spaceCondition.signalAll();
                cond.spaceCondition = null;
            }
        } else if (nodeUuid == null) {
            //identifier level lock
            Condition idCond = cond.identifierConditionMap.get(identifier);
            if (idCond != null) {
                idCond.signalAll();
                cond.identifierConditionMap.remove(identifier);
            }
        } else {
            //node level lock
            Map<String, Condition> nodeMap = cond.nodeConditionMap.get(identifier);
            if (nodeMap != null) {
                Condition nodeCond = nodeMap.get(nodeUuid);
                if (nodeCond != null) {
                    nodeCond.signalAll();
                    nodeMap.remove(nodeUuid);
                    //no more node level lock, then clear this node identifier map.
                    if (nodeMap.size() == 0) {
                        cond.nodeConditionMap.remove(identifier);
                    }
                }
            }
        }
        log.debug("Repository space {} identifier {} node {} release write lock successfully in {}",
                new String[] { spacename, identifier, nodeUuid, Thread.currentThread().getName() });
    } finally {
        writeLock.unlock();
    }
}

From source file:org.apache.hadoop.hdfs.client.ShortCircuitCache.java

ClientMmap getOrCreateClientMmap(ShortCircuitReplica replica, boolean anchored) {
    Condition newCond;
    lock.lock();/*w w w.  j  a  v a 2s.c o  m*/
    try {
        while (replica.mmapData != null) {
            if (replica.mmapData instanceof MappedByteBuffer) {
                ref(replica);
                MappedByteBuffer mmap = (MappedByteBuffer) replica.mmapData;
                return new ClientMmap(replica, mmap, anchored);
            } else if (replica.mmapData instanceof Long) {
                long lastAttemptTimeMs = (Long) replica.mmapData;
                long delta = Time.monotonicNow() - lastAttemptTimeMs;
                if (delta < staleThresholdMs) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace(this + ": can't create client mmap for " + replica + " because we failed to "
                                + "create one just " + delta + "ms ago.");
                    }
                    return null;
                }
                if (LOG.isTraceEnabled()) {
                    LOG.trace(this + ": retrying client mmap for " + replica + ", " + delta
                            + " ms after the previous failure.");
                }
            } else if (replica.mmapData instanceof Condition) {
                Condition cond = (Condition) replica.mmapData;
                cond.awaitUninterruptibly();
            } else {
                Preconditions.checkState(false,
                        "invalid mmapData type " + replica.mmapData.getClass().getName());
            }
        }
        newCond = lock.newCondition();
        replica.mmapData = newCond;
    } finally {
        lock.unlock();
    }
    MappedByteBuffer map = replica.loadMmapInternal();
    lock.lock();
    try {
        if (map == null) {
            replica.mmapData = Long.valueOf(Time.monotonicNow());
            newCond.signalAll();
            return null;
        } else {
            outstandingMmapCount++;
            replica.mmapData = map;
            ref(replica);
            newCond.signalAll();
            return new ClientMmap(replica, map, anchored);
        }
    } finally {
        lock.unlock();
    }
}

From source file:org.apache.hadoop.hdfs.shortcircuit.ShortCircuitCache.java

ClientMmap getOrCreateClientMmap(ShortCircuitReplica replica, boolean anchored) {
    Condition newCond;
    lock.lock();//from   w ww .j  a v  a  2 s  .  c om
    try {
        while (replica.mmapData != null) {
            if (replica.mmapData instanceof MappedByteBuffer) {
                ref(replica);
                MappedByteBuffer mmap = (MappedByteBuffer) replica.mmapData;
                return new ClientMmap(replica, mmap, anchored);
            } else if (replica.mmapData instanceof Long) {
                long lastAttemptTimeMs = (Long) replica.mmapData;
                long delta = Time.monotonicNow() - lastAttemptTimeMs;
                if (delta < mmapRetryTimeoutMs) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace(this + ": can't create client mmap for " + replica + " because we failed to "
                                + "create one just " + delta + "ms ago.");
                    }
                    return null;
                }
                if (LOG.isTraceEnabled()) {
                    LOG.trace(this + ": retrying client mmap for " + replica + ", " + delta
                            + " ms after the previous failure.");
                }
            } else if (replica.mmapData instanceof Condition) {
                Condition cond = (Condition) replica.mmapData;
                cond.awaitUninterruptibly();
            } else {
                Preconditions.checkState(false, "invalid mmapData type %s",
                        replica.mmapData.getClass().getName());
            }
        }
        newCond = lock.newCondition();
        replica.mmapData = newCond;
    } finally {
        lock.unlock();
    }
    MappedByteBuffer map = replica.loadMmapInternal();
    lock.lock();
    try {
        if (map == null) {
            replica.mmapData = Long.valueOf(Time.monotonicNow());
            newCond.signalAll();
            return null;
        } else {
            outstandingMmapCount++;
            replica.mmapData = map;
            ref(replica);
            newCond.signalAll();
            return new ClientMmap(replica, map, anchored);
        }
    } finally {
        lock.unlock();
    }
}

From source file:org.apache.hadoop.hive.metastore.HiveMetaStore.java

private static void signalOtherThreadsToStart(final TServer server, final Lock startLock,
        final Condition startCondition, final AtomicBoolean startedServing) {
    // A simple thread to wait until the server has started and then signal the other threads to
    // begin//from   w  ww  .java 2 s.  co m
    Thread t = new Thread() {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    LOG.warn("Signalling thread was interuppted: " + e.getMessage());
                }
            } while (!server.isServing());
            startLock.lock();
            try {
                startedServing.set(true);
                startCondition.signalAll();
            } finally {
                startLock.unlock();
            }
        }
    };
    t.start();
}

From source file:org.jactr.core.production.action.SleepAction.java

/**
 * wait until the goal buffer isn't empty
 * /*from   www .  j  a  va 2  s.c  o m*/
 * @see org.jactr.core.production.action.IAction#fire(org.jactr.core.production.IInstantiation, double)
 */
public double fire(IInstantiation instantiation, double firingTime) {
    IActivationBuffer goalBuffer = instantiation.getModel().getActivationBuffer(IActivationBuffer.GOAL);

    if (goalBuffer.getSourceChunk() == null) {
        final Lock goalLock = new ReentrantLock();
        final Condition gotAGoal = goalLock.newCondition();

        /*
         * merely signal when the goal buffer gets something
         */
        IActivationBufferListener listener = new ActivationBufferListenerAdaptor() {
            @Override
            public void sourceChunkAdded(ActivationBufferEvent event) {
                try {
                    goalLock.lock();
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("Signaling goal insertion");
                    gotAGoal.signalAll();
                } finally {
                    goalLock.unlock();
                }
            }
        };

        /*
         * attach the listener with the inline executor - this ensures that
         * regardless of what thread adds the source chunk to the buffer we will
         * be notified
         */
        goalBuffer.addListener(listener, ExecutorServices.INLINE_EXECUTOR);

        try {
            goalLock.lock();
            while (goalBuffer.getSourceChunk() == null) {
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Waiting for goal");
                gotAGoal.await();
            }
        } catch (Exception e) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Could not wait for goal ", e);
        }

        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Resuming from wait");

        goalLock.unlock();

        /*
         * remove the listener
         */
        goalBuffer.removeListener(listener);
    } else if (LOGGER.isDebugEnabled())
        LOGGER.debug("Goal is already present, no need to sleep");

    return 0;
}

From source file:org.sparkbit.jsonrpc.JSONRPCController.java

@Subscribe
public void listen(SBEvent event) throws Exception {
    SBEventType t = event.getType();/*w ww . ja v  a2  s. c om*/
    if (t == SBEventType.TX_CONFIDENCE_CHANGED) {
        Transaction tx = (Transaction) event.getInfo();
        int count = tx.getConfidence().getBroadcastByCount();
        if (count > 0) {
            String txid = tx.getHashAsString();
            if (txBroadcastMap.containsKey(txid)) {
                Condition c = txBroadcastMap.remove(txid);
                txBroadcastLock.lock();
                try {
                    c.signalAll();
                    log.debug("Peer broadcast count " + count + " for txid " + txid);
                } catch (Exception e) {
                    log.error("Condition signalAll() failed: " + e);
                } finally {
                    txBroadcastLock.unlock();
                }
            }
        }
    }
}

From source file:org.unitime.timetable.onlinesectioning.MultiLock.java

public void unlockAll() {
    iLock.lock();/*ww  w. j  ava 2  s  .  c  o m*/
    try {
        iLog.debug("Unlocking all ...");
        Condition allLocked = iAllLocked;
        iAllLocked = null;
        allLocked.signalAll();
        iLog.debug("Unlocked: all");
    } finally {
        iLock.unlock();
    }
}

From source file:org.unitime.timetable.onlinesectioning.MultiLock.java

private void unlock(Collection<Long> ids) {
    iLock.lock();//w  w w .j  a  v  a 2  s.  co m
    try {
        if (ids == null || ids.isEmpty())
            return;
        iLog.debug("Unlocking " + ids + " ...");
        Condition myCondition = null;
        for (Long id : ids)
            myCondition = iIndividualLocks.remove(id);
        if (myCondition != null)
            myCondition.signalAll();
        iLog.debug("Unlocked: " + ids);
    } finally {
        iLock.unlock();
    }
}