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

public static void main(String[] args) {
    int[] runningThreads = { 0 };
    int[] taskcount = { 10 };
    Lock myLock = new ReentrantLock(true);
    int maxThreadQty = 3;
    while ((taskcount[0] > 0) && (runningThreads[0] < maxThreadQty)) {
        myLock.lock();//from  w w w.j  a va2  s. co  m
        runningThreads[0]++;
        myLock.unlock();
        new Thread("T") {
            public void run() {
                myLock.lock();
                taskcount[0]--;
                runningThreads[0]--;
                myLock.unlock();
            }
        }.start();
    }
}

From source file:Test.java

public static void main(String[] args) {
    final Lock firstLock = new ReentrantLock();
    final Lock secondLock = new ReentrantLock();
    firstLock.lock();//from   www  .ja  v  a  2  s .  c om
    Thread secondThread = new Thread(new Runnable() {
        public void run() {
            secondLock.lock();
            firstLock.lock();
        }
    });
    secondThread.start();
    try {
        Thread.sleep(250);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    secondLock.lock();

    secondLock.unlock();
    firstLock.unlock();
}

From source file:Main.java

public static void performLocked(Lock lock, Runnable action) {
    lock.lock();//from   w  w  w.j  a  v a2 s .  c om
    try {
        action.run();
    } finally {
        lock.unlock();
    }
}

From source file:com.yahoo.gondola.container.Utils.java

private static void unlock(Lock lock) {
    if (lock != null) {
        lock.unlock();
    }
}

From source file:Main.java

public static <T> T performLocked(Lock lock, Callable<T> action) {
    lock.lock();//ww w.  j a  v a2s . co  m
    try {
        return action.call();
    } catch (RuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    } finally {
        lock.unlock();
    }
}

From source file:com.piggate.sdk.PiggateInfo.java

public static void addInfo(JSONArray bar) {

    Lock l = rwLock2.writeLock();
    l.lock();/* w w w  .j a  v a  2s.  com*/
    try {
        registryInfo(bar);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
}

From source file:com.piggate.sdk.PiggateOffers.java

public static void addOffers(JSONArray bar) {

    Lock l = rwLock2.writeLock();
    l.lock();// w  w w .j  a va2 s . c  o m
    try {
        registryOffers(bar);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
}

From source file:com.piggate.sdk.PiggateInfo.java

public static ArrayList<PiggateInfo> getInfo() {
    Lock l = rwLock2.readLock();
    ArrayList<PiggateInfo> result = new ArrayList<PiggateInfo>();
    l.lock();/*from www .ja va  2  s.c o  m*/
    try {
        result.addAll(internal_info);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
    return result;
}

From source file:com.piggate.sdk.PiggateOffers.java

public static ArrayList<PiggateOffers> getOffers() {
    Lock l = rwLock2.readLock();
    ArrayList<PiggateOffers> result = new ArrayList<PiggateOffers>();
    l.lock();//  w  w w.j  a va  2  s.  c om
    try {
        result.addAll(internal_offers);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
    return result;
}

From source file:org.nuxeo.ecm.platform.web.common.requestcontroller.filter.NuxeoRequestControllerFilter.java

/**
 * Releases the {@link Lock} if present in the HttpSession.
 *///from w  w  w  .j a v a2 s. c  o m
public static boolean simpleReleaseSyncOnSession(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    if (session == null) {
        if (log.isDebugEnabled()) {
            log.debug(doFormatLogMessage(request,
                    "No more HttpSession: can not unlock !, HttpSession must have been invalidated"));
        }
        return false;
    }
    log.debug(
            "Trying to unlock on session " + session.getId() + " 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");
        return false;
    } else {
        lock.unlock();
        if (request.getAttribute(SYNCED_REQUEST_FLAG) != null) {
            request.removeAttribute(SYNCED_REQUEST_FLAG);
        }
        if (log.isDebugEnabled()) {
            log.debug("session unlocked on Thread ");
        }
        return true;
    }
}