Example usage for java.util.concurrent.locks ReentrantLock ReentrantLock

List of usage examples for java.util.concurrent.locks ReentrantLock ReentrantLock

Introduction

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

Prototype

public ReentrantLock() 

Source Link

Document

Creates an instance of ReentrantLock .

Usage

From source file:info.pancancer.arch3.worker.CollectingLogOutputStream.java

/**
 * Get the last *n* lines in the log./*from w w w.j  ava  2s  .co m*/
 * 
 * @param n
 *            - The number of lines to get.
 * @return A list of strings.
 */
public List<String> getLastNLines(int n) {
    Lock lock = new ReentrantLock();
    lock.lock();
    List<String> nlines = new ArrayList<String>(n);
    int start, end;
    end = this.lines.size();
    start = Math.max(0, this.lines.size() - n);
    if (end > start && start >= 0) {
        nlines = this.lines.subList(start, end);
    }
    lock.unlock();
    return nlines;
}

From source file:net.ymate.platform.cache.support.CacheableProxy.java

public Object doProxy(IProxyChain proxyChain) throws Throwable {
    ICaches _caches = Caches.get(proxyChain.getProxyFactory().getOwner());
    ///*from   ww  w. j a  v  a  2s  .com*/
    Cacheable _anno = proxyChain.getTargetMethod().getAnnotation(Cacheable.class);
    if (_anno == null) {
        return proxyChain.doProxyChain();
    }
    //
    Object _cacheKey = StringUtils.trimToNull(_anno.key());
    if (_cacheKey == null) {
        _cacheKey = _caches.getModuleCfg().getKeyGenerator().generateKey(proxyChain.getTargetMethod(),
                proxyChain.getMethodParams());
    }
    ReentrantLock _locker = __LOCK_MAP.get(_cacheKey.toString());
    if (_locker == null) {
        _locker = new ReentrantLock();
        ReentrantLock _previous = __LOCK_MAP.putIfAbsent(_cacheKey.toString(), _locker);
        if (_previous != null) {
            _locker = _previous;
        }
    }
    _locker.lock();
    CacheElement _result = null;
    try {
        ICacheScopeProcessor _scopeProc = _caches.getModuleCfg().getCacheScopeProcessor();
        if (!_anno.scope().equals(ICaches.Scope.DEFAULT) && _scopeProc != null) {
            _result = _scopeProc.getFromCache(_caches, _anno.scope(), _anno.cacheName(), _cacheKey.toString());
        } else {
            _result = (CacheElement) _caches.get(_anno.cacheName(), _cacheKey);
        }
        boolean _flag = true;
        if (_result != null && !_result.isExpired()) {
            _flag = false;
        }
        if (_flag) {
            Object _cacheTarget = proxyChain.doProxyChain();
            if (_cacheTarget != null) {
                _result = new CacheElement(_cacheTarget);
                int _timeout = _anno.timeout() > 0 ? _anno.timeout()
                        : _caches.getModuleCfg().getDefaultCacheTimeout();
                if (_timeout > 0) {
                    _result.setTimeout(_timeout);
                }
                if (!_anno.scope().equals(ICaches.Scope.DEFAULT) && _scopeProc != null) {
                    _scopeProc.putInCache(_caches, _anno.scope(), _anno.cacheName(), _cacheKey.toString(),
                            _result);
                } else {
                    _caches.put(_anno.cacheName(), _cacheKey, _result);
                }
            }
        }
    } finally {
        _locker.unlock();
    }
    return _result != null ? _result.getObject() : null;
}

From source file:com.joyent.manta.client.multipart.EncryptionState.java

/**
 * Zero argument constructor used for serialization.
 *//* www  .  j  ava 2s  . com*/
private EncryptionState() {
    this.encryptionContext = null;
    this.lock = new ReentrantLock();
}

From source file:org.ops4j.pax.web.service.internal.Activator.java

public Activator() {
    m_lock = new ReentrantLock();
}

From source file:bq.jpa.demo.lock.service.LockService.java

public LockService() {
    this.lock = new ReentrantLock();
    readCondition = lock.newCondition();
    writeCondition = lock.newCondition();
    write1Condition = lock.newCondition();
    write2Condition = lock.newCondition();
}

From source file:org.apache.ode.bpel.engine.NStateLatch.java

/**
 * Constructor, the array of {@link Runnable}s defines the number of states and the transition
 * actions./*from   w  ww  . ja  v  a 2s .  c  o  m*/
 * @param transitions action to perform when entering state x.
 */
public NStateLatch(Runnable[] transitions) {
    _transitions = transitions;
    _lock = new ReentrantLock();
    _depth0 = _lock.newCondition();
}

From source file:de.sevenpass.sample.AuthStateManager.java

private AuthStateManager(Context context) {
    mPrefs = context.getSharedPreferences(STORE_NAME, Context.MODE_PRIVATE);
    mPrefsLock = new ReentrantLock();
    mCurrentAuthState = new AtomicReference<>();
}

From source file:org.kei.android.phone.cellhistory.CellHistoryApp.java

public CellHistoryApp() {
    filterCtx = new FilterCtx();
    lock = new ReentrantLock();
    providerCtx = new ProviderCtx();
    recorderCtx = new RecorderCtx();
}

From source file:core.client.impl.ConditionalWriterImpl.java

private static RowLock getRowLock(byte[] row) {
    synchronized (rowLocks) {
        ArrayByteSequence rowSeq = new ArrayByteSequence(row);
        RowLock lock = rowLocks.get(rowSeq);
        if (lock == null) {
            lock = new RowLock(new ReentrantLock(), rowSeq);
            // TODO will byte array change?
            rowLocks.put(rowSeq, lock);//from ww  w  .  j  av  a2  s .  c  o m
        }

        lock.count++;
        return lock;
    }
}

From source file:com.cloudera.lib.service.instrumentation.InstrumentationService.java

@Override
@SuppressWarnings("unchecked")
public void init() throws ServiceException {
    timersSize = getServiceConfig().getInt(CONF_TIMERS_SIZE, 10);
    counterLock = new ReentrantLock();
    timerLock = new ReentrantLock();
    variableLock = new ReentrantLock();
    samplerLock = new ReentrantLock();
    jvmVariables = new ConcurrentHashMap<String, VariableHolder>();
    counters = new ConcurrentHashMap<String, Map<String, AtomicLong>>();
    timers = new ConcurrentHashMap<String, Map<String, Timer>>();
    variables = new ConcurrentHashMap<String, Map<String, VariableHolder>>();
    samplers = new ConcurrentHashMap<String, Map<String, Sampler>>();
    samplersList = new ArrayList<Sampler>();
    all = new LinkedHashMap<String, Map<String, ?>>();
    all.put("os-env", System.getenv());
    all.put("sys-props", (Map<String, ?>) (Map) System.getProperties());
    all.put("jvm", jvmVariables);
    all.put("counters", (Map) counters);
    all.put("timers", (Map) timers);
    all.put("variables", (Map) variables);
    all.put("samplers", (Map) samplers);

    jvmVariables.put("free.memory", new VariableHolder<Long>(new Instrumentation.Variable<Long>() {
        public Long getValue() {
            return Runtime.getRuntime().freeMemory();
        }/*from  w  w w  .  ja va 2s. c o  m*/
    }));
    jvmVariables.put("max.memory", new VariableHolder<Long>(new Instrumentation.Variable<Long>() {
        public Long getValue() {
            return Runtime.getRuntime().maxMemory();
        }
    }));
    jvmVariables.put("total.memory", new VariableHolder<Long>(new Instrumentation.Variable<Long>() {
        public Long getValue() {
            return Runtime.getRuntime().totalMemory();
        }
    }));
}