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:org.apache.hadoop.hbase.util.CoprocessorClassLoader.java

/**
 * Get a CoprocessorClassLoader for a coprocessor jar path from cache.
 * If not in cache, create one./*from  www  .j a  v a  2s  .  com*/
 *
 * @param path the path to the coprocessor jar file to load classes from
 * @param parent the parent class loader for exempted classes
 * @param pathPrefix a prefix used in temp path name to store the jar file locally
 * @param conf the configuration used to create the class loader, if needed
 * @return a CoprocessorClassLoader for the coprocessor jar path
 * @throws IOException
 */
public static CoprocessorClassLoader getClassLoader(final Path path, final ClassLoader parent,
        final String pathPrefix, final Configuration conf) throws IOException {
    CoprocessorClassLoader cl = getIfCached(path);
    String pathStr = path.toString();
    if (cl != null) {
        LOG.debug("Found classloader " + cl + " for " + pathStr);
        return cl;
    }

    if (!pathStr.endsWith(".jar")) {
        throw new IOException(pathStr + ": not a jar file?");
    }

    Lock lock = locker.acquireLock(pathStr);
    try {
        cl = getIfCached(path);
        if (cl != null) {
            LOG.debug("Found classloader " + cl + " for " + pathStr);
            return cl;
        }

        cl = AccessController.doPrivileged(new PrivilegedAction<CoprocessorClassLoader>() {
            @Override
            public CoprocessorClassLoader run() {
                return new CoprocessorClassLoader(parent);
            }
        });

        cl.init(path, pathPrefix, conf);

        // Cache class loader as a weak value, will be GC'ed when no reference left
        CoprocessorClassLoader prev = classLoadersCache.putIfAbsent(path, cl);
        if (prev != null) {
            // Lost update race, use already added class loader
            LOG.warn("THIS SHOULD NOT HAPPEN, a class loader" + " is already cached for " + pathStr);
            cl = prev;
        }
        return cl;
    } finally {
        lock.unlock();
    }
}

From source file:com.cloudera.oryx.als.serving.ServerRecommender.java

private static float[] getFeatures(long longID, LongObjectMap<float[]> matrix, ReadWriteLock lock) {
    float[] features;
    Lock readLock = lock.readLock();
    readLock.lock();/*  www  .  j  a  v a  2 s. co m*/
    try {
        features = matrix.get(longID);
        if (features == null) {
            int numFeatures = countFeatures(matrix);
            if (numFeatures > 0) {
                features = new float[numFeatures];
                Lock writeLock = lock.writeLock();
                readLock.unlock();
                writeLock.lock();
                try {
                    matrix.put(longID, features);
                } finally {
                    readLock.lock();
                    writeLock.unlock();
                }
            }
        }
    } finally {
        readLock.unlock();
    }
    return features;
}

From source file:info.pancancer.arch3.worker.CollectingLogOutputStream.java

/**
 * Get the last *n* lines in the log./*from  www  .  ja  v  a 2 s  .c  o  m*/
 * 
 * @param n
 *            - The number of lines to get.
 * @return A list of strings.
 */
public List<String> getLastNLines(int n) {
    Lock lock = new ReentrantLock();
    lock.lock();
    List<String> nlines = new ArrayList<String>(n);
    int start, end;
    end = this.lines.size();
    start = Math.max(0, this.lines.size() - n);
    if (end > start && start >= 0) {
        nlines = this.lines.subList(start, end);
    }
    lock.unlock();
    return nlines;
}

From source file:com.cloudera.oryx.ml.speed.als.ALSSpeedModel.java

public void setUserVector(String user, float[] vector) {
    Preconditions.checkNotNull(vector);/*from   w ww  .j ava2  s .com*/
    Preconditions.checkArgument(vector.length == features);
    Lock lock = xLock.writeLock();
    lock.lock();
    try {
        if (X.put(user, vector) == null) {
            // User was actually new
            recentNewUsers.add(user);
        }
    } finally {
        lock.unlock();
    }
}

From source file:com.cloudera.oryx.ml.speed.als.ALSSpeedModel.java

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

From source file:SoftReferenceCache.java

/**
 * @see java.util.Map#get(java.lang.Object)
 *//*ww w .ja va2  s .  co  m*/
public V get(Object key) {
    Lock lock = theLock.readLock();
    lock.lock();
    try {
        KeyedSoftReference<V> val = theCache.get(key);
        return val == null ? null : val.get();
    } finally {
        lock.unlock();
    }
}

From source file:SoftReferenceCache.java

/**
 * @see java.util.Map#containsKey(java.lang.Object)
 *///from  w  w  w.  j a v a2  s . co  m
public boolean containsKey(Object key) {
    Lock lock = theLock.readLock();
    lock.lock();
    try {
        KeyedSoftReference<V> val = theCache.get(key);
        return val == null || val.isEnqueued();
    } finally {
        lock.unlock();
    }
}

From source file:org.springframework.integration.metadata.PropertiesPersistingMetadataStore.java

@Override
public void put(String key, String value) {
    Assert.notNull(key, "'key' cannot be null");
    Assert.notNull(value, "'value' cannot be null");
    Lock lock = this.lockRegistry.obtain(key);
    lock.lock();/*from   w ww. j a  v  a  2  s. c o  m*/
    try {
        this.metadata.setProperty(key, value);
    } finally {
        this.dirty = true;
        lock.unlock();
    }
}

From source file:org.nuxeo.ecm.platform.wi.filter.WIRequestFilter.java

protected void simpleReleaseSyncOnSession(HttpServletRequest request) {
    /*//from w  ww.j a v  a  2s  .  c  o  m
     * HttpSession httpSession = request.getSession(false); if (httpSession
     * == null) { if (log.isDebugEnabled()) { log.debug(doFormatLogMessage(
     * request,
     * "No more HttpSession : can not unlock !, HttpSession must have been invalidated"
     * )); } return; }
     */

    WISession session = (WISession) request.getAttribute(SESSION_KEY);

    log.debug("Trying to unlock on httpSession key " + session.getKey() + " WISession:" + session.getKey()
            + " on Thread " + Thread.currentThread().getId());

    Lock lock = (Lock) session.getAttribute(SESSION_LOCK_KEY);
    if (lock == null) {
        log.error("Unable to find session lock, HttpSession may have been invalidated");
    } else {
        lock.unlock();
        if (log.isDebugEnabled()) {
            log.debug("session unlocked on Thread ");
            log.debug(doExecutionRequestLogMessage(request));
        }
    }
}

From source file:org.springframework.integration.metadata.PropertiesPersistingMetadataStore.java

@Override
public String remove(String key) {
    Assert.notNull(key, "'key' cannot be null");
    Lock lock = this.lockRegistry.obtain(key);
    lock.lock();//ww w  .  j a va 2 s. c  om
    try {
        return (String) this.metadata.remove(key);
    } finally {
        this.dirty = true;
        lock.unlock();
    }
}