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.cloudera.oryx.ml.serving.als.model.ALSServingModel.java

public List<Pair<String, Double>> topN(final DoubleFunction<float[]> scoreFn, final int howMany,
        final Predicate<String> allowedPredicate) {

    List<Callable<Iterable<Pair<String, Double>>>> tasks = new ArrayList<>(Y.length);
    for (int partition = 0; partition < Y.length; partition++) {
        final int thePartition = partition;
        tasks.add(new LoggingCallable<Iterable<Pair<String, Double>>>() {
            @Override//from w ww .j a v  a  2  s  .com
            public Iterable<Pair<String, Double>> doCall() {
                Queue<Pair<String, Double>> topN = new PriorityQueue<>(howMany + 1,
                        PairComparators.<Double>bySecond());
                TopNConsumer topNProc = new TopNConsumer(topN, howMany, scoreFn, allowedPredicate);

                Lock lock = yLocks[thePartition].readLock();
                lock.lock();
                try {
                    Y[thePartition].forEach(topNProc);
                } finally {
                    lock.unlock();
                }
                // Ordering and excess items don't matter; will be merged and finally sorted later
                return topN;
            }
        });
    }

    List<Iterable<Pair<String, Double>>> iterables = new ArrayList<>();
    if (Y.length >= 2) {
        try {
            for (Future<Iterable<Pair<String, Double>>> future : executor.invokeAll(tasks)) {
                iterables.add(future.get());
            }
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        } catch (ExecutionException e) {
            throw new IllegalStateException(e.getCause());
        }
    } else {
        try {
            iterables.add(tasks.get(0).call());
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    return Ordering.from(PairComparators.<Double>bySecond()).greatestOf(Iterables.concat(iterables), howMany);
}

From source file:com.mastfrog.netty.http.client.CookieStore.java

public void store(OutputStream out) throws IOException {
    ObjectMapper om = new ObjectMapper();
    Lock readLock = lock.readLock();
    List<DateCookie> cks = new ArrayList<>();
    readLock.lock();/* www.jav a2  s.  c om*/
    try {
        cks.addAll(cookies);
    } finally {
        readLock.unlock();
    }
    List<Map<String, Object>> list = new LinkedList<>();
    for (DateCookie ck : cks) {
        Map<String, Object> m = new HashMap<>();
        m.put("domain", ck.getDomain());
        m.put("maxAge", ck.getMaxAge());
        m.put("timestamp", ck.getTimestamp().getMillis());
        m.put("path", ck.getPath());
        m.put("name", ck.getName());
        m.put("value", ck.getValue());
        m.put("httpOnly", ck.isHttpOnly());
        m.put("secure", ck.isSecure());
        if (ck.getComment() != null) {
            m.put("comment", ck.getComment());
        }
        if (ck.getCommentUrl() != null) {
            m.put("commentUrl", ck.getCommentUrl());
        }
        if (ck.getPorts() != null && !ck.getPorts().isEmpty()) {
            m.put("ports", ck.getPorts().toArray(new Integer[0]));
        }
        list.add(m);
    }
    om.writeValue(out, list);
}

From source file:com.mastfrog.netty.http.client.CookieStore.java

@SuppressWarnings("unchecked")
public void read(InputStream in) throws IOException {
    ObjectMapper om = new ObjectMapper();
    List<Map<String, Object>> l = om.readValue(in, List.class);
    List<DateCookie> cks = new ArrayList<>();
    for (Map<String, Object> m : l) {
        String domain = (String) m.get("domain");
        Number maxAge = (Number) m.get("maxAge");
        Number timestamp = (Number) m.get("timestamp");
        String path = (String) m.get("path");
        String name = (String) m.get("name");
        String value = (String) m.get("value");
        Boolean httpOnly = (Boolean) m.get("httpOnly");
        Boolean secure = (Boolean) m.get("secure");
        String comment = (String) m.get("comment");
        String commentUrl = (String) m.get("commentUrl");
        List<Integer> ports = (List<Integer>) m.get("ports");
        DateTime ts = timestamp == null ? DateTime.now() : new DateTime(timestamp.longValue());
        DateCookie cookie = new DateCookie(new DefaultCookie(name, value), ts);
        if (cookie.isExpired()) {
            continue;
        }/*from  www.  j av a  2  s  .  c  om*/
        if (domain != null) {
            cookie.setDomain(domain);
        }
        if (maxAge != null) {
            cookie.setMaxAge(maxAge.longValue());
        }
        if (path != null) {
            cookie.setPath(path);
        }
        if (httpOnly != null) {
            cookie.setHttpOnly(httpOnly);
        }
        if (secure != null) {
            cookie.setSecure(secure);
        }
        if (comment != null) {
            cookie.setComment(comment);
        }
        if (commentUrl != null) {
            cookie.setCommentUrl(commentUrl);
        }
        if (ports != null) {
            cookie.setPorts(ports);
        }
        cks.add(cookie);
    }
    if (!cks.isEmpty()) {
        Lock writeLock = lock.writeLock();
        try {
            writeLock.lock();
            cookies.addAll(cks);
        } finally {
            writeLock.unlock();
        }
    }
}

From source file:com.cloudera.oryx.ml.serving.als.model.ALSServingModel.java

void setItemVector(String item, float[] vector) {
    Preconditions.checkNotNull(vector);/*w  ww. j a  v a 2  s .c  o  m*/
    Preconditions.checkArgument(vector.length == features);
    int partition = partition(item);
    Lock lock = yLocks[partition].writeLock();
    lock.lock();
    try {
        if (Y[partition].put(item, vector) == null) {
            // Item was actually new
            recentNewItems[partition].add(item);
        }
    } finally {
        lock.unlock();
    }
}

From source file:DemandCache.java

/**
 * @return The total size of the data in this cache, according to
 *         {@link Qualitizer#size(Object)}. This value will return the same as {@link #size()}
 *         if the qualitizer was not set in the constructor.
 *//* www  .  jav a 2 s .  c o  m*/
public float getTotalSize() {
    Lock lock = theLock.readLock();
    lock.lock();
    try {
        float ret = 0;
        for (CacheValue value : theCache.values())
            ret += theQualitizer.size(value.value);
        return ret;
    } finally {
        lock.unlock();
    }
}

From source file:org.apache.bookkeeper.bookie.EntryLogManagerForEntryLogPerLedger.java

@Override
void createNewLog(long ledgerId) throws IOException {
    Lock lock = getLock(ledgerId);
    lock.lock();/* w  w w .j  a va2  s  . co m*/
    try {
        super.createNewLog(ledgerId);
    } finally {
        lock.unlock();
    }
}

From source file:DemandCache.java

/**
 * @see java.util.Map#get(java.lang.Object)
 *//* ww w .j av  a2s .co  m*/
public V get(Object key) {
    Lock lock = theLock.readLock();
    lock.lock();
    try {
        CacheValue value = theCache.get(key);
        if (value == null)
            return null;
        _access(value, ACCESS_GET);
        return value.value;
    } finally {
        lock.unlock();
    }
}

From source file:DemandCache.java

/**
 * @return The average quality of the values in this cache, according to
 *         {@link Qualitizer#quality(Object)}. This value will return 1 if the qualitizer was
 *         not set in the constructor./* w w  w . j av a2  s  .  co  m*/
 */
public float getOverallQuality() {
    Lock lock = theLock.readLock();
    lock.lock();
    try {
        float ret = 0;
        for (CacheValue value : theCache.values())
            ret += theQualitizer.quality(value.value);
        return ret / theCache.size();
    } finally {
        lock.unlock();
    }
}

From source file:com.cloudera.oryx.ml.serving.als.model.ALSServingModel.java

/**
 * Prunes the set of users in the model, by retaining only users that are expected to appear
 * in the upcoming model updates, or, that have arrived recently. This also clears the
 * recent known users data structure./*from  ww  w . ja  v  a  2 s .  c  o  m*/
 *
 * @param users users that should be retained, which are coming in the new model updates
 */
void pruneX(Collection<String> users) {
    // Keep all users in the new model, or, that have been added since last model
    Lock lock = xLock.writeLock();
    lock.lock();
    try {
        X.removeIf(new KeyOnlyBiPredicate<>(new AndPredicate<>(new NotContainsPredicate<>(users),
                new NotContainsPredicate<>(recentNewUsers))));
        recentNewUsers.clear();
    } finally {
        lock.unlock();
    }
}

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

/**
 * close current connection and establish new connection.
 * fails silently if back-off period is in effect.
 *//*from   w w w  .j  a v  a 2s  . c o m*/
protected void reconnect(boolean onerror) throws IOException, InterruptedException {
    // avoid deadlock situation caused by attempting
    // to acquire write lock while holding read lock.
    // there'd be no real dead-lock now that timeout on write lock is implemented,
    // but it's nice to know there's a bug in locking.
    if (tableUseLock.getReadHoldCount() > 0) {
        LOG.warn("avoiding deadlock: reconnect() called by thread with read lock.");
        return;
    }
    Lock writeLock = tableUseLock.writeLock();
    if (!writeLock.tryLock(TRY_WRITE_LOCK_TIMEOUT, TimeUnit.SECONDS)) {
        LOG.warn("reconnect() could not acquire write lock on tableUseLock for " + TRY_WRITE_LOCK_TIMEOUT
                + "s, giving up.");
        return;
    }
    try {
        closeTable(table, onerror);
        openTable();
    } finally {
        writeLock.unlock();
    }
}