Example usage for java.util.concurrent.locks Lock unlock

List of usage examples for java.util.concurrent.locks Lock unlock

Introduction

In this page you can find the example usage for java.util.concurrent.locks Lock unlock.

Prototype

void unlock();

Source Link

Document

Releases the lock.

Usage

From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java

public void removeIgnore(SocialPerson memberPerson) {
    Lock lock = rwIgnoreLock.writeLock();
    lock.lock();/*  w  w  w  . j ava2s.  c  o  m*/
    try {
        String playerName = memberPerson.getName();
        person.getIgnoreList().remove(playerName);
        SocialNetworkPlugin.log("[" + person.getName() + "] is no longer ignoring " + playerName);
    } finally {
        lock.unlock();
    }
}

From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java

public void removeAllAlerts() {

    Lock lock = rwMessageQueueLock.writeLock();
    lock.lock();/*from   w  w  w .ja v  a2  s .  c  o m*/
    try {

        String className = Alert.class.getCanonicalName();
        if (person.getMessageQueue().containsKey(className)) {
            person.getMessageQueue().remove(className);
        }

    } finally {
        lock.unlock();
    }
}

From source file:org.apache.synapse.endpoints.algorithms.WeightedRoundRobin.java

public Endpoint getNextEndpoint(MessageContext synapseMessageContext, AlgorithmContext algorithmContext) {

    Lock readLock = lock.readLock();
    readLock.lock();//from  w  ww  .  j ava 2s  . c o m
    try {
        if (!isThreadLocal) {
            synchronized (this) {
                EndpointState state = endpointStates[endpointCursor];
                if (state.getCurrentWeight() == 0) {
                    // reset the current state
                    state.reset();

                    // go to the next endpoint
                    if (endpointCursor == endpointStates.length - 1) {
                        endpointCursor = 0;
                    } else {
                        ++endpointCursor;
                    }

                    state = endpointStates[endpointCursor];
                }

                // we are about to use this endpoint, so decrement its current count
                state.decrementCurrentWeight();

                // return the endpoint corresponding to the current position
                return endpoints.get(state.getEndpointPosition());
            }
        } else {
            if (threadedAlgorithm != null) {
                Algorithm algo = threadedAlgorithm.get();

                int position = algo.getNextEndpoint();

                return endpoints.get(position);
            } else {
                String msg = "Algorithm: WeightedRoundRobin algorithm not initialized properly";
                log.error(msg);
                throw new SynapseException(msg);
            }
        }
    } finally {
        readLock.unlock();
    }
}

From source file:jp.aegif.nemaki.cmis.service.impl.ObjectServiceImpl.java

@Override
public ContentStream getContentStream(CallContext callContext, String repositoryId, String objectId,
        String streamId, BigInteger offset, BigInteger length) {

    exceptionService.invalidArgumentRequired("objectId", objectId);

    Lock lock = threadLockService.getReadLock(repositoryId, objectId);
    try {//from   w w  w  . j av  a  2s  . c  o  m
        lock.lock();

        // //////////////////
        // General Exception
        // //////////////////
        Content content = contentService.getContent(repositoryId, objectId);
        exceptionService.objectNotFound(DomainType.OBJECT, content, objectId);
        exceptionService.permissionDenied(callContext, repositoryId,
                PermissionMapping.CAN_GET_PROPERTIES_OBJECT, content);

        // //////////////////
        // Body of the method
        // //////////////////
        if (streamId == null) {
            return getContentStreamInternal(repositoryId, content, offset, length);
        } else {
            return getRenditionStream(repositoryId, content, streamId);
        }
    } finally {
        lock.unlock();
    }
}

From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java

/**
 * Checks to see if the given player name is on the ignore list of the person.
 * @param playerName The name to check to see if they're on ignore by this person.
 * @return True if ignored, false otherwise.
 *//*from w w w .j a  v a  2s .c  o  m*/
public boolean isOnIgnore(SocialPerson memberPerson) {
    Lock lock = rwIgnoreLock.writeLock();
    lock.lock();
    try {
        String playerName = memberPerson.getName();
        boolean isOnIgnore = person.getIgnoreList().contains(playerName);
        SocialNetworkPlugin.log("[" + person.getName() + "] is ignoring " + playerName + ":" + isOnIgnore);
        return isOnIgnore;
    } finally {
        lock.unlock();
    }
}

From source file:net.myrrix.online.ServerRecommender.java

@Override
public List<RecommendedItem> recommendToMany(long[] userIDs, int howMany, boolean considerKnownItems,
        IDRescorer rescorer) throws NoSuchUserException, NotReadyException {

    Preconditions.checkArgument(howMany > 0, "howMany must be positive");

    Generation generation = getCurrentGeneration();
    FastByIDMap<float[]> X = generation.getX();

    Lock xLock = generation.getXLock().readLock();
    List<float[]> userFeatures = Lists.newArrayListWithCapacity(userIDs.length);
    xLock.lock();/*from   w w w  . jav  a  2  s . c o m*/
    try {
        for (long userID : userIDs) {
            float[] theUserFeatures = X.get(userID);
            if (theUserFeatures != null) {
                userFeatures.add(theUserFeatures);
            }
        }
    } finally {
        xLock.unlock();
    }
    if (userFeatures.isEmpty()) {
        throw new NoSuchUserException(Arrays.toString(userIDs));
    }

    FastByIDMap<FastIDSet> knownItemIDs = generation.getKnownItemIDs();
    if (knownItemIDs == null && !considerKnownItems) {
        throw new UnsupportedOperationException("Can't ignore known items because no known items available");
    }
    FastIDSet usersKnownItemIDs = null;
    if (!considerKnownItems) {
        Lock knownItemLock = generation.getKnownItemLock().readLock();
        knownItemLock.lock();
        try {
            for (long userID : userIDs) {
                FastIDSet theKnownItemIDs = knownItemIDs.get(userID);
                if (theKnownItemIDs == null) {
                    continue;
                }
                if (usersKnownItemIDs == null) {
                    usersKnownItemIDs = theKnownItemIDs;
                } else {
                    LongPrimitiveIterator it = usersKnownItemIDs.iterator();
                    while (it.hasNext()) {
                        if (!theKnownItemIDs.contains(it.nextLong())) {
                            it.remove();
                        }
                    }
                }
                if (usersKnownItemIDs.isEmpty()) {
                    break;
                }
            }
        } finally {
            knownItemLock.unlock();
        }
    }

    float[][] userFeaturesArray = userFeatures.toArray(new float[userFeatures.size()][]);
    Lock yLock = generation.getYLock().readLock();
    yLock.lock();
    try {
        return multithreadedTopN(userFeaturesArray, usersKnownItemIDs, generation.getUserTagIDs(), rescorer,
                howMany, generation.getCandidateFilter());
    } finally {
        yLock.unlock();
    }

}

From source file:net.myrrix.online.ServerRecommender.java

/**
 * One-argument version of {@link #mostSimilarItems(long[], int, Rescorer)}.
 */// w  ww.  jav a  2 s  .com
@Override
public List<RecommendedItem> mostSimilarItems(long itemID, int howMany, Rescorer<LongPair> rescorer)
        throws NoSuchItemException, NotReadyException {

    Preconditions.checkArgument(howMany > 0, "howMany must be positive");

    Generation generation = getCurrentGeneration();
    FastByIDMap<float[]> Y = generation.getY();

    Lock yLock = generation.getYLock().readLock();
    yLock.lock();
    try {

        float[] itemFeatures = Y.get(itemID);
        if (itemFeatures == null) {
            throw new NoSuchItemException(itemID);
        }

        return TopN.selectTopN(new MostSimilarItemIterator(Y.entrySet().iterator(), generation.getUserTagIDs(),
                new long[] { itemID }, new float[][] { itemFeatures }, rescorer), howMany);
    } finally {
        yLock.unlock();
    }

}

From source file:org.opendedup.collections.ProgressiveFileBasedCSMap.java

@Override
public boolean remove(ChunkData cm) throws IOException {
    if (this.isClosed()) {
        throw new IOException("hashtable [" + this.fileName + "] is close");
    }/*  w ww.jav  a2 s .  c o  m*/
    Lock l = gcLock.readLock();
    l.lock();
    try {
        if (!this.runningGC && !lbf.mightContain(cm.getHash()))
            return false;
        try {
            if (cm.getHash().length == 0)
                return true;
            AbstractShard m = this.getReadMap(cm.getHash());
            if (m == null)
                return false;
            if (!m.remove(cm.getHash())) {
                return false;
            } else {
                cm.setmDelete(true);
                if (this.isClosed()) {
                    throw new IOException("hashtable [" + this.fileName + "] is close");
                }
                try {
                    this.kSz.decrementAndGet();
                } catch (Exception e) {
                }

                return true;
            }
        } catch (Exception e) {
            SDFSLogger.getLog().fatal("error getting record", e);
            return false;
        }
    } finally {
        l.unlock();
    }
}

From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java

public Set<String> getMessagePlayers(Class<? extends IMessage> messageClass) {

    Lock lock = rwMessageQueueLock.readLock();
    lock.lock();/*from ww w.ja  v  a  2  s. c  o  m*/
    try {
        String className = messageClass.getCanonicalName();
        if (person.getMessageQueue().get(className) != null) {
            return Collections.unmodifiableSet(person.getMessageQueue().get(className).keySet());
        }

        // return an empty list
        return Collections.emptySet();

    } finally {
        lock.unlock();
    }
}

From source file:org.archive.modules.recrawl.hbase.SingleHBaseTable.java

public void put(Put p) throws IOException {
    putCount.incrementAndGet();//from ww  w  .jav  a2 s .c o  m
    // trigger reconnection if necessary. as table can be modified before
    // read lock is acquired, we don't read table variable here.
    getTable();
    boolean htableFailed = false;
    HTableInterface htable = null;
    Lock readLock = tableUseLock.readLock();
    try {
        if (!readLock.tryLock(TRY_READ_LOCK_TIMEOUT, TimeUnit.SECONDS)) {
            putSkipCount.incrementAndGet();
            throw new IOException("could not acquire read lock for HTable.");
        }
    } catch (InterruptedException ex) {
        throw new IOException("interrupted while acquiring read lock", ex);
    }
    try {
        htable = table;
        if (htable == null) {
            putSkipCount.incrementAndGet();
            throw new IOException("HBase connection is unvailable.");
        }
        // HTable.put() buffers Puts and access to the buffer is not
        // synchronized.
        synchronized (htable) {
            try {
                htable.put(p);
            } catch (NullPointerException ex) {
                // HTable.put() throws NullPointerException when connection is lost.
                // It is somewhat weird, so translate it to IOException.
                putErrorCount.incrementAndGet();
                htableFailed = true;
                throw new IOException("hbase connection is lost", ex);
            } catch (NotServingRegionException ex) {
                putErrorCount.incrementAndGet();
                // no need to close HTable.
                throw ex;
            } catch (IOException ex) {
                putErrorCount.incrementAndGet();
                htableFailed = true;
                throw ex;
            }
        }
    } finally {
        readLock.unlock();
        if (htableFailed) {
            closeTable(htable, true);
        }
    }
}