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

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

Introduction

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

Prototype

lock

Source Link

Usage

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

void setUserVector(String user, float[] vector) {
    Preconditions.checkNotNull(vector);//from   w w w. j av  a2  s. c o m
    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:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//ww  w .  ja va 2  s.c o m
public void testReentrantLock() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey);
    for (int i = 0; i < 10; i++) {
        Lock lock1 = registry.obtain("foo");
        lock1.lock();
        try {
            Lock lock2 = registry.obtain("foo");
            assertSame(lock1, lock2);
            lock2.lock();
            try {
                // just get the lock
            } finally {
                lock2.unlock();
            }
        } finally {
            lock1.unlock();
        }
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

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

public Map<String, Integer> getItemCounts() {
    ObjIntMap<String> counts = HashObjIntMaps.newUpdatableMap();
    Lock lock = xLock.readLock();
    lock.lock();
    try {//from w  w  w  .j a va 2 s .  c o m
        for (Collection<String> ids : knownItems.values()) {
            synchronized (ids) {
                for (String id : ids) {
                    counts.addValue(id, 1);
                }
            }
        }
    } finally {
        lock.unlock();
    }
    return counts;
}

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

public float[] getItemVector(String item) {
    int partition = partition(item);
    Lock lock = yLocks[partition].readLock();
    lock.lock();
    try {/*from  w  w  w. ja v a 2  s.c o  m*/
        return Y[partition].get(item);
    } finally {
        lock.unlock();
    }
}

From source file:org.apache.lucene.gdata.search.index.IndexController.java

private void createNewIndexerTask(final ServerBaseEntry entry, final IndexAction action) {
    checkDestroyed();/*from w  w  w .j  ava2 s .  com*/
    checkInitialized();
    String serviceName = entry.getServiceConfig().getName();
    if (LOG.isInfoEnabled())
        LOG.info("New Indexer Task submitted - Action: " + action + " for service: " + serviceName);
    ServiceIndex bean = this.indexerMap.get(serviceName);
    if (bean == null)
        throw new RuntimeException("no indexer for service " + serviceName + " registered");
    /*
     * lock on service to synchronize the event order. This lock has
     * fairness parameter set to true. Grant access to the longest waiting
     * thread. Using fairness is slower but is acceptable in this context
     */
    Lock lock = bean.getLock();
    lock.lock();
    try {
        IndexSchema schema = bean.getSchema();
        boolean commitAfter = bean.incrementActionAndReset(schema.getCommitAfterDocuments());
        IndexDocumentBuilder<IndexDocument> callable = new IndexDocumentBuilderTask<IndexDocument>(entry,
                bean.getSchema(), action, commitAfter, bean.getOptimize(schema.getOptimizeAfterCommit()));
        sumbitTask(callable, bean.getIndexer());
    } finally {
        /*
         * make sure to unlock
         */
        lock.unlock();
    }

}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//  w ww.  j av a2s .  c  o  m
public void testThreadLocalListLeaks() {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey,
            100);
    registry.setUseWeakReferences(true);

    for (int i = 0; i < 10; i++) {
        registry.obtain("foo" + i);
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());

    for (int i = 0; i < 10; i++) {
        Lock lock = registry.obtain("foo" + i);
        lock.lock();
    }
    assertEquals(10,
            ((Collection<?>) TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get())
                    .size());
    assertNull(TestUtils.getPropertyValue(registry, "weakThreadLocks", ThreadLocal.class).get());

    for (int i = 0; i < 10; i++) {
        Lock lock = registry.obtain("foo" + i);
        assertNotNull(TestUtils.getPropertyValue(lock, "thread", Thread.class));
        lock.unlock();
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

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

void setItemVector(String item, float[] vector) {
    Preconditions.checkNotNull(vector);/*  w  w w  . j  av  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:com.cloudera.oryx.ml.serving.als.model.ALSServingModel.java

public Collection<String> getAllItemIDs() {
    Collection<String> itemsList = new ArrayList<>();
    for (int partition = 0; partition < Y.length; partition++) {
        Lock lock = yLocks[partition].readLock();
        lock.lock();
        try {/*from  ww w  .  j ava 2 s. com*/
            itemsList.addAll(Y[partition].keySet());
        } finally {
            lock.unlock();
        }
    }
    return itemsList;
}

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

private static FastIDSet getIDsFromKeys(FastByIDMap<float[]> map, Lock readLock, FastIDSet tagIDs) {
    readLock.lock();
    try {/*w w w.  ja  va2 s  .c  o  m*/
        FastIDSet ids = new FastIDSet(map.size());
        LongPrimitiveIterator it = map.keySetIterator();
        while (it.hasNext()) {
            long id = it.nextLong();
            if (!tagIDs.contains(id)) {
                ids.add(id);
            }
        }
        return ids;
    } finally {
        readLock.unlock();
    }
}

From source file:DemandCache.java

/**
 * @see java.util.Map#containsKey(java.lang.Object)
 *///from  w  w w .j  a  v a  2 s.c  om
public boolean containsKey(Object key) {
    Lock lock = theLock.readLock();
    lock.lock();
    try {
        return theCache.containsKey(key);
    } finally {
        lock.unlock();
    }
}