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.piggate.sdk.PiggateInfo.java

public static ArrayList<PiggateInfo> getInfo() {
    Lock l = rwLock2.readLock();
    ArrayList<PiggateInfo> result = new ArrayList<PiggateInfo>();
    l.lock();
    try {/* ww w . ja  v a  2 s . co m*/
        result.addAll(internal_info);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
    return result;
}

From source file:net.myrrix.online.generation.Generation.java

private static Solver recomputeSolver(FastByIDMap<float[]> M, Lock readLock) {
    readLock.lock();
    try {//from w w  w  . j  a v a  2  s  . co 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.als.lambda", infNorm);
            throw new IllConditionedSolverException("infNorm: " + infNorm);
        }
        return MatrixUtils.getSolver(MTM);
    } finally {
        readLock.unlock();
    }
}

From source file:Main.java

/**
 * like Platform.runLater but waits until the thread has finished
 * based on: http://www.guigarage.com/2013/01/invokeandwait-for-javafx/
 * @param r the runnable to run in a JavaFX thread
 *///  w w w .  jav  a 2 s.co  m
public static void platformRunAndWait(final Runnable r) throws Throwable {
    if (Platform.isFxApplicationThread()) {
        try {
            r.run();
        } catch (Exception e) {
            throw new ExecutionException(e);
        }
    } else {
        final Lock lock = new ReentrantLock();
        final Condition condition = lock.newCondition();
        final boolean[] done = { false };
        // to get around the requirement for final
        final Throwable[] ex = { null };
        lock.lock();
        try {

            Platform.runLater(() -> {
                lock.lock();
                try {
                    r.run();
                } catch (Throwable e) {
                    ex[0] = e;
                } finally {
                    try {
                        done[0] = true;
                        condition.signal();
                    } finally {
                        lock.unlock();
                    }
                }
            });

            while (!done[0])
                condition.await();

            if (ex[0] != null) {
                // re-throw exception from the runLater thread
                throw ex[0];
            }
        } finally {
            lock.unlock();
        }
    }
}

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();
    try {/*from  w  ww .j ava  2  s  .c  o  m*/
        result.addAll(internal_offers);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
    return result;
}

From source file:och.util.Util.java

public static <T> T inLock(Lock lock, Callable<T> body) throws Exception {
    lock.lock();
    try {/*from w  w w  .j  ava 2s.c  o  m*/
        return body.call();
    } finally {
        lock.unlock();
    }
}

From source file:och.util.Util.java

public static void inLock(Lock lock, CallableVoid body) throws Exception {
    lock.lock();
    try {//from   ww  w  .  j a v  a  2s. c o  m
        body.call();
    } finally {
        lock.unlock();
    }
}

From source file:org.gridgain.grid.kernal.ggfs.hadoop.GridGgfsHadoopIpcIo.java

/**
 * Returns a started and valid instance of this class
 * for a given endpoint./*ww  w  .  j  a va  2 s.  c  o  m*/
 *
 * @param log Logger to use for new instance.
 * @param endpoint Endpoint string.
 * @return New or existing cached instance, which is started and operational.
 * @throws IOException If new instance was created but failed to start.
 */
public static GridGgfsHadoopIpcIo get(Log log, String endpoint) throws IOException {
    while (true) {
        GridGgfsHadoopIpcIo clientIo = ipcCache.get(endpoint);

        if (clientIo != null) {
            if (clientIo.acquire())
                return clientIo;
            else
                // If concurrent close.
                ipcCache.remove(endpoint, clientIo);
        } else {
            Lock lock = initLock.getLock(endpoint);

            lock.lock();

            try {
                clientIo = ipcCache.get(endpoint);

                if (clientIo != null) { // Perform double check.
                    if (clientIo.acquire())
                        return clientIo;
                    else
                        // If concurrent close.
                        ipcCache.remove(endpoint, clientIo);
                }

                // Otherwise try creating a new one.
                clientIo = new GridGgfsHadoopIpcIo(endpoint, new GridGgfsMarshaller(), log);

                try {
                    clientIo.start();
                } catch (GridException e) {
                    throw new IOException(e.getMessage(), e);
                }

                GridGgfsHadoopIpcIo old = ipcCache.putIfAbsent(endpoint, clientIo);

                // Put in exclusive lock.
                assert old == null;

                return clientIo;
            } finally {
                lock.unlock();
            }
        }
    }
}

From source file:org.apache.ignite.internal.processors.hadoop.igfs.HadoopIgfsIpcIo.java

/**
 * Returns a started and valid instance of this class
 * for a given endpoint.//  w w w . j a v  a  2s  .  c  o  m
 *
 * @param log Logger to use for new instance.
 * @param endpoint Endpoint string.
 * @return New or existing cached instance, which is started and operational.
 * @throws IOException If new instance was created but failed to start.
 */
public static HadoopIgfsIpcIo get(Log log, String endpoint) throws IOException {
    while (true) {
        HadoopIgfsIpcIo clientIo = ipcCache.get(endpoint);

        if (clientIo != null) {
            if (clientIo.acquire())
                return clientIo;
            else
                // If concurrent close.
                ipcCache.remove(endpoint, clientIo);
        } else {
            Lock lock = initLock.getLock(endpoint);

            lock.lock();

            try {
                clientIo = ipcCache.get(endpoint);

                if (clientIo != null) { // Perform double check.
                    if (clientIo.acquire())
                        return clientIo;
                    else
                        // If concurrent close.
                        ipcCache.remove(endpoint, clientIo);
                }

                // Otherwise try creating a new one.
                clientIo = new HadoopIgfsIpcIo(endpoint, new IgfsMarshaller(), log);

                try {
                    clientIo.start();
                } catch (IgniteCheckedException e) {
                    throw new IOException(e.getMessage(), e);
                }

                HadoopIgfsIpcIo old = ipcCache.putIfAbsent(endpoint, clientIo);

                // Put in exclusive lock.
                assert old == null;

                return clientIo;
            } 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();
    try {/*from   w  ww. j a  va  2s  . c o  m*/
        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

/**
 * Process a line./*from w  ww  . j  a  v a 2 s . c om*/
 * 
 * @param line
 *            - A line.
 * @param level
 *            - a logging level. Not used in this implementation.
 */
@Override
protected void processLine(String line, int level) {
    Lock lock = new ReentrantLock();
    lock.lock();
    lines.add(line);
    lock.unlock();
}