本文整理了Java中java.util.concurrent.locks.Lock.lock()
方法的一些代码示例,展示了Lock.lock()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lock.lock()
方法的具体详情如下:
包路径:java.util.concurrent.locks.Lock
类名称:Lock
方法名:lock
[英]Acquires the lock.
If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.
Implementation Considerations
A Lock implementation may be able to detect erroneous use of the lock, such as an invocation that would cause deadlock, and may throw an (unchecked) exception in such circumstances. The circumstances and the exception type must be documented by that Lock implementation.
[中]获得锁。
如果锁不可用,则出于线程调度目的,当前线程将被禁用,并处于休眠状态,直到获得锁为止。
实施考虑
锁实现可能能够检测锁的错误使用,例如可能导致死锁的调用,并且在这种情况下可能抛出(未检查的)异常。锁实现必须记录情况和异常类型。
代码示例来源:origin: ReactiveX/RxJava
void signalConsumer() {
lock.lock();
try {
condition.signalAll();
} finally {
lock.unlock();
}
}
代码示例来源:origin: jenkinsci/jenkins
/**
* The opposite of {@link #isIdle()} — the executor is doing some work.
*/
public boolean isBusy() {
lock.readLock().lock();
try {
return workUnit != null || executable != null;
} finally {
lock.readLock().unlock();
}
}
代码示例来源:origin: neo4j/neo4j
public Resource checkPoint()
{
Lock writeLock = lock.writeLock();
writeLock.lock();
return writeLock::unlock;
}
}
代码示例来源:origin: jenkinsci/jenkins
void addCriticalField(Class<?> clazz, String field) {
// Lock the write lock
criticalFieldsLock.writeLock().lock();
try {
// If the class already exists, then add a new field, otherwise
// create the hash map field
if (!criticalFields.containsKey(field)) {
criticalFields.put(field, new HashSet<String>());
}
criticalFields.get(field).add(clazz.getName());
}
finally {
// Unlock
criticalFieldsLock.writeLock().unlock();
}
}
代码示例来源:origin: jenkinsci/jenkins
private boolean hasCriticalField(Class<?> clazz, String field) {
// Lock the write lock
criticalFieldsLock.readLock().lock();
try {
Set<String> classesWithField = criticalFields.get(field);
if (classesWithField == null) {
return false;
}
if (!classesWithField.contains(clazz.getName())) {
return false;
}
return true;
}
finally {
criticalFieldsLock.readLock().unlock();
}
}
代码示例来源:origin: bumptech/glide
void acquire(String safeKey) {
WriteLock writeLock;
synchronized (this) {
writeLock = locks.get(safeKey);
if (writeLock == null) {
writeLock = writeLockPool.obtain();
locks.put(safeKey, writeLock);
}
writeLock.interestedThreads++;
}
writeLock.lock.lock();
}
代码示例来源:origin: alibaba/jstorm
@Override
public Object initWindowState(TimeWindow window) {
WindowedKvState<K, V> windowedKvState = new WindowedKvState<>(window, windowedStateManager);
try {
windowUpdateLock.lock();
windowedStates.put(window, windowedKvState);
} finally {
windowUpdateLock.unlock();
}
return windowedKvState;
}
代码示例来源:origin: sohutv/cachecloud
public JedisPool getNode(String nodeKey) {
r.lock();
try {
return nodes.get(nodeKey);
} finally {
r.unlock();
}
}
代码示例来源:origin: jenkinsci/jenkins
/*protected*/ void start(WorkUnit task) {
lock.writeLock().lock();
try {
this.workUnit = task;
super.start();
started = true;
} finally {
lock.writeLock().unlock();
}
}
代码示例来源:origin: alibaba/druid
public WebSessionStat getSessionStat(String sessionId, boolean create) {
sessionStatLock.readLock().lock();
try {
WebSessionStat uriStat = sessionStatMap.get(sessionId);
sessionStatLock.readLock().unlock();
sessionStatLock.writeLock().lock();
try {
WebSessionStat uriStat = sessionStatMap.get(sessionId);
sessionStatLock.writeLock().unlock();
代码示例来源:origin: apache/ignite
/**
* @throws Exception If failed.
*/
@SuppressWarnings({"LockAcquiredButNotSafelyReleased"})
@Test
public void testReadThenWriteLockAcquire() throws Exception {
ReadWriteLock lock = new ReentrantReadWriteLock();
lock.readLock().lock();
lock.writeLock().lock();
}
代码示例来源:origin: alibaba/druid
private ProfileEntryStat getProfileEntry(ProfileEntryKey entryKey) {
lock.readLock().lock();
try {
ProfileEntryStat entryStat = entries.get(entryKey);
if (entryStat != null) {
return entryStat;
}
} finally {
lock.readLock().unlock();
}
lock.writeLock().lock();
try {
ProfileEntryStat entryStat = entries.get(entryKey);
if (entryStat == null) {
entries.put(entryKey, new ProfileEntryStat());
entryStat = entries.get(entryKey);
}
return entryStat;
} finally {
lock.writeLock().unlock();
}
}
代码示例来源:origin: gocd/gocd
public int instanceCount(CaseInsensitiveString pipelineName) {
scheduleOrderLock.readLock().lock();
try {
ArrayList<PipelineTimelineEntry> instances = scheduleOrderPmm.get(pipelineName);
return instances == null ? 0 : instances.size();
} finally {
scheduleOrderLock.readLock().unlock();
}
}
代码示例来源:origin: sohutv/cachecloud
public void setNodeIfNotExist(HostAndPort node) {
w.lock();
try {
String nodeKey = getNodeKey(node);
if (nodes.containsKey(nodeKey)) return;
JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(),
connectionTimeout, soTimeout, null, 0, null);
nodes.put(nodeKey, nodePool);
} finally {
w.unlock();
}
}
代码示例来源:origin: sohutv/cachecloud
public JedisPool getSlotPool(int slot) {
r.lock();
try {
return slots.get(slot);
} finally {
r.unlock();
}
}
代码示例来源:origin: jenkinsci/jenkins
/**
* Returns true if this executor is waiting for a task to execute.
*/
public boolean isParking() {
lock.readLock().lock();
try {
return !started;
} finally {
lock.readLock().unlock();
}
}
代码示例来源:origin: apache/incubator-druid
/**
* Called when the histogram encounters a null value.
*/
public void incrementMissing()
{
readWriteLock.writeLock().lock();
try {
missingValueCount++;
}
finally {
readWriteLock.writeLock().unlock();
}
}
代码示例来源:origin: ReactiveX/RxJava
void signalConsumer() {
lock.lock();
try {
condition.signalAll();
} finally {
lock.unlock();
}
}
代码示例来源:origin: google/guava
public void testWeakReadWrite() {
Striped<ReadWriteLock> striped = Striped.lazyWeakReadWriteLock(1000);
Object key = new Object();
Lock readLock = striped.get(key).readLock();
WeakReference<Object> garbage = new WeakReference<>(new Object());
GcFinalization.awaitClear(garbage);
Lock writeLock = striped.get(key).writeLock();
readLock.lock();
assertFalse(writeLock.tryLock());
readLock.unlock();
}
代码示例来源:origin: Netflix/eureka
public String cachedValueOf(final String str) {
if (str != null && (lengthLimit < 0 || str.length() <= lengthLimit)) {
// Return value from cache if available
try {
lock.readLock().lock();
WeakReference<String> ref = cache.get(str);
if (ref != null) {
return ref.get();
}
} finally {
lock.readLock().unlock();
}
// Update cache with new content
try {
lock.writeLock().lock();
WeakReference<String> ref = cache.get(str);
if (ref != null) {
return ref.get();
}
cache.put(str, new WeakReference<>(str));
} finally {
lock.writeLock().unlock();
}
return str;
}
return str;
}
内容来源于网络,如有侵权,请联系作者删除!