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

public static void main(String[] args) throws Exception {
    Lock myLock = new ReentrantLock();
    Random random = new Random();

    myLock.lock();
    int number = random.nextInt(5);
    int result = 100 / number;
    System.out.println("A result is " + result);
    FileOutputStream file = new FileOutputStream("file.out");
    file.write(result);/*from  w  ww.  j  av a  2s .  co  m*/
    file.close();
}

From source file:Test.java

public static void main(String[] args) {
    final Lock firstLock = new ReentrantLock();
    final Lock secondLock = new ReentrantLock();
    firstLock.lock();
    Thread secondThread = new Thread(new Runnable() {
        public void run() {
            secondLock.lock();//  w  w w  . ja  va2 s.c  o  m
            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 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();
        runningThreads[0]++;/*from   w w w  .j ava  2  s.c  o  m*/
        myLock.unlock();
        new Thread("T") {
            public void run() {
                myLock.lock();
                taskcount[0]--;
                runningThreads[0]--;
                myLock.unlock();
            }
        }.start();
    }
}

From source file:Main.java

public static void performLocked(Lock lock, Runnable action) {
    lock.lock();
    try {// w  ww.  j a v  a 2 s.  c o  m
        action.run();
    } finally {
        lock.unlock();
    }
}

From source file:Main.java

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

From source file:Main.java

/**
 * This methods blocks until the worker is done and returns the result value of the worker.
 * If the worker was canceled an exception will be thrown.
 *
 * @param worker The worker/* w w w  .  ja va2 s .c  om*/
 * @param <T> result type of the worker
 * @return the result
 * @throws InterruptedException if the worker was canceled
 */
public static <T> T waitFor(Worker<T> worker) throws InterruptedException {
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    lock.lock();
    try {
        ReadOnlyBooleanProperty doneProperty = createIsDoneProperty(worker);
        if (doneProperty.get()) {
            return worker.getValue();
        } else {
            doneProperty.addListener(e -> {
                boolean locked = lock.tryLock();
                if (locked) {
                    try {
                        condition.signal();
                    } finally {
                        lock.unlock();
                    }
                } else {
                    throw new RuntimeException("Concurreny Error");
                }
            });
            condition.await();
            return worker.getValue();
        }
    } finally {
        lock.unlock();
    }
}

From source file:com.cloudera.oryx.als.serving.generation.Generation.java

private static Solver recomputeSolver(LongObjectMap<float[]> M, Lock readLock) {
    readLock.lock();
    try {/*from ww  w .  j  ava2 s .  c o m*/
        if (M == null || M.isEmpty()) {
            return null;
        }
        RealMatrix MTM = MatrixUtils.transposeTimesSelf(M);
        double infNorm = MTM.getNorm();
        if (infNorm < 1.0) {
            log.warn("X'*X or Y'*Y has small inf norm ({}); try decreasing model.lambda", infNorm);
            throw new IllConditionedSolverException("infNorm: " + infNorm);
        }
        return MatrixUtils.getSolver(MTM);
    } finally {
        readLock.unlock();
    }
}

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

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

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

public static void addInfo(JSONArray bar) {

    Lock l = rwLock2.writeLock();
    l.lock();
    try {//from   www  .  j a v a2 s. c o  m
        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();
    try {//w w w.  ja  v a 2s  .  c  o m
        registryOffers(bar);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
}