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:DemandCache.java

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

From source file:com.iternox.piggate.samples.PiggateLogin.Activity_Logged.java

public ArrayList<PiggateBeacon> getPendingBeacons() {
    Lock l = rwLock.readLock();
    ArrayList<PiggateBeacon> result = new ArrayList<PiggateBeacon>();
    l.lock();//from   w w  w. j ava 2 s. c  o  m
    try {
        result.addAll(this.pending);
        this.pending.clear();
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
    return result;
}

From source file:org.springframework.integration.jdbc.lock.JdbcLockRegistryDifferentClientTests.java

@Test
public void testBothLock() throws Exception {

    for (int i = 0; i < 100; i++) {

        final JdbcLockRegistry registry1 = this.registry;
        final JdbcLockRegistry registry2 = this.child.getBean(JdbcLockRegistry.class);
        final List<String> locked = new ArrayList<String>();
        final CountDownLatch latch = new CountDownLatch(2);
        ExecutorService pool = Executors.newFixedThreadPool(2);
        pool.execute(() -> {//from w w w. ja  va  2  s . co m
            Lock lock = registry1.obtain("foo");
            try {
                lock.lockInterruptibly();
                locked.add("1");
                latch.countDown();
            } catch (InterruptedException e1) {
                Thread.currentThread().interrupt();
            } finally {
                try {
                    lock.unlock();
                } catch (Exception e2) {
                    // ignore
                }
            }
        });

        pool.execute(() -> {
            Lock lock = registry2.obtain("foo");
            try {
                lock.lockInterruptibly();
                locked.add("2");
                latch.countDown();
            } catch (InterruptedException e1) {
                Thread.currentThread().interrupt();
            } finally {
                try {
                    lock.unlock();
                } catch (Exception e2) {
                    // ignore
                }
            }
        });

        assertTrue(latch.await(10, TimeUnit.SECONDS));
        // eventually they both get the lock and release it
        assertTrue(locked.contains("1"));
        assertTrue(locked.contains("2"));

    }

}

From source file:com.mcapanel.plugin.PluginConnector.java

public String sendMethodResponse(String method, String... params) {
    if (connected()) {
        //if (returns.containsKey(method))
        //returns.remove(method);

        long time = sendMethod(method, params);

        if (time != -1) {
            long start = System.currentTimeMillis();

            while (System.currentTimeMillis() - start < 2000) {
                final Lock lock = returnsLock.readLock();
                lock.lock();/*from  ww  w. ja v a 2  s.c  o m*/

                try {
                    if (returns.containsKey(time)) {
                        PluginReturn ret = returns.get(time);

                        if (ret != null)
                            return ret.getData();
                    }
                } finally {
                    lock.unlock();
                }
            }

            final Lock lock = returnsLock.readLock();
            lock.lock();

            try {
                for (Long key : returns.descendingKeySet()) {
                    PluginReturn pr = returns.get(key);

                    if (pr.getMethod().equals(method)) {
                        return pr.getData();
                    }
                }
            } finally {
                lock.unlock();
            }
        }
    }

    return null;
}

From source file:org.springframework.integration.jdbc.lock.JdbcLockRegistryDifferentClientTests.java

@Test
public void testSecondThreadLoses() throws Exception {

    for (int i = 0; i < 100; i++) {

        final JdbcLockRegistry registry1 = this.registry;
        final JdbcLockRegistry registry2 = this.child.getBean(JdbcLockRegistry.class);
        final Lock lock1 = registry1.obtain("foo");
        final AtomicBoolean locked = new AtomicBoolean();
        final CountDownLatch latch1 = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);
        final CountDownLatch latch3 = new CountDownLatch(1);
        lock1.lockInterruptibly();//  ww w  . jav a  2 s.c  o  m
        Executors.newSingleThreadExecutor().execute(() -> {
            Lock lock2 = registry2.obtain("foo");
            try {
                latch1.countDown();
                lock2.lockInterruptibly();
                latch2.await(10, TimeUnit.SECONDS);
                locked.set(true);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock2.unlock();
                latch3.countDown();
            }
        });
        assertTrue(latch1.await(10, TimeUnit.SECONDS));
        assertFalse(locked.get());
        lock1.unlock();
        latch2.countDown();
        assertTrue(latch3.await(10, TimeUnit.SECONDS));
        assertTrue(locked.get());

    }

}

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();/*  ww  w  . j av  a  2 s. c  om*/
        try {
            itemsList.addAll(Y[partition].keySet());
        } finally {
            lock.unlock();
        }
    }
    return itemsList;
}

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

public int size() {
    Lock readLock = lock.readLock();
    try {/*from www  .ja va2s. c  om*/
        readLock.lock();
        return cookies.size();
    } finally {
        readLock.unlock();
    }
}

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

public boolean isEmpty() {
    Lock readLock = lock.readLock();
    try {// www . j ava 2s .  c  o m
        readLock.lock();
        return cookies.isEmpty();
    } finally {
        readLock.unlock();
    }
}

From source file:org.eclipse.gyrex.context.internal.registry.ContextRegistryImpl.java

public void close() throws Exception {
    // set closed
    closed.set(true);/*  w  w w  . j a  va2  s .c o  m*/

    // remove preference listener
    getContextFlushNode().removePreferenceChangeListener(flushListener);

    // dispose active contexts
    GyrexContextImpl[] activeContexts;
    final Lock lock = contextRegistryLock.writeLock();
    lock.lock();
    try {
        activeContexts = contexts.values().toArray(new GyrexContextImpl[contexts.size()]);
        contexts.clear();
    } finally {
        lock.unlock();
    }

    // dispose all the active contexts
    for (final GyrexContextImpl context : activeContexts) {
        context.dispose();
    }

    // clear handles
    handles.clear();
}