本文整理了Java中java.util.concurrent.locks.Lock.lockInterruptibly()
方法的一些代码示例,展示了Lock.lockInterruptibly()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lock.lockInterruptibly()
方法的具体详情如下:
包路径:java.util.concurrent.locks.Lock
类名称:Lock
方法名:lockInterruptibly
[英]Acquires the lock unless the current thread is Thread#interrupt.
Acquires the lock if it is available and returns immediately.
If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
If the current thread:
Implementation Considerations
The ability to interrupt a lock acquisition in some implementations may not be possible, and if possible may be an expensive operation. The programmer should be aware that this may be the case. An implementation should document when this is the case.
An implementation can favor responding to an interrupt over normal method return.
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.
[中]获取锁,除非当前线程是线程中断。
获取锁(如果可用)并立即返回。
如果锁不可用,则出于线程调度目的,当前线程将被禁用,并处于休眠状态,直到发生以下两种情况之一:
*锁被当前线程获取;或
*其他一些线程线程#中断当前线程,支持中断锁获取。
如果当前线程:
*在进入该方法时设置其中断状态;或
*是线程#在获取锁时中断,并且支持中断锁获取,
然后抛出InterruptedException,并清除当前线程的中断状态。
实施考虑
在某些实现中,中断锁获取的能力可能是不可能的,如果可能的话,可能是一个昂贵的操作。程序员应该知道可能是这样的。在这种情况下,实现应该记录下来。
与正常的方法返回相比,实现更倾向于响应中断。
锁实现可能能够检测锁的错误使用,例如可能导致死锁的调用,并且在这种情况下可能抛出(未检查的)异常。锁实现必须记录情况和异常类型。
代码示例来源:origin: apache/rocketmq
public boolean hasTempMessage() {
try {
this.lockTreeMap.readLock().lockInterruptibly();
try {
return !this.msgTreeMap.isEmpty();
} finally {
this.lockTreeMap.readLock().unlock();
}
} catch (InterruptedException e) {
}
return true;
}
代码示例来源:origin: wildfly/wildfly
public void _waitForMessages() throws InterruptedException {
lock.lockInterruptibly(); // fail fast
try {
while(count == 0)
not_empty.await();
}
finally {
lock.unlock();
}
}
代码示例来源:origin: apache/rocketmq
public void deleteTopic(final String topic) {
try {
try {
this.lock.writeLock().lockInterruptibly();
this.topicQueueTable.remove(topic);
} finally {
this.lock.writeLock().unlock();
}
} catch (Exception e) {
log.error("deleteTopic Exception", e);
}
}
代码示例来源:origin: apache/rocketmq
public void rollback() {
try {
this.lockTreeMap.writeLock().lockInterruptibly();
try {
this.msgTreeMap.putAll(this.consumingMsgOrderlyTreeMap);
this.consumingMsgOrderlyTreeMap.clear();
} finally {
this.lockTreeMap.writeLock().unlock();
}
} catch (InterruptedException e) {
log.error("rollback exception", e);
}
}
代码示例来源:origin: apache/rocketmq
public Properties getAllConfigs() {
try {
readWriteLock.readLock().lockInterruptibly();
try {
return this.allConfigs;
} finally {
readWriteLock.readLock().unlock();
}
} catch (InterruptedException e) {
log.error("getAllConfigs lock error");
}
return null;
}
代码示例来源:origin: apache/rocketmq
public long getMaxSpan() {
try {
this.lockTreeMap.readLock().lockInterruptibly();
try {
if (!this.msgTreeMap.isEmpty()) {
return this.msgTreeMap.lastKey() - this.msgTreeMap.firstKey();
}
} finally {
this.lockTreeMap.readLock().unlock();
}
} catch (InterruptedException e) {
log.error("getMaxSpan exception", e);
}
return 0;
}
代码示例来源:origin: apache/rocketmq
public String getKVConfig(final String namespace, final String key) {
try {
this.lock.readLock().lockInterruptibly();
try {
HashMap
if (null != kvTable) {
return kvTable.get(key);
}
} finally {
this.lock.readLock().unlock();
}
} catch (InterruptedException e) {
log.error("getKVConfig InterruptedException", e);
}
return null;
}
代码示例来源:origin: apache/rocketmq
public void clear() {
try {
this.lockTreeMap.writeLock().lockInterruptibly();
try {
this.msgTreeMap.clear();
this.consumingMsgOrderlyTreeMap.clear();
this.msgCount.set(0);
this.msgSize.set(0);
this.queueOffsetMax = 0L;
} finally {
this.lockTreeMap.writeLock().unlock();
}
} catch (InterruptedException e) {
log.error("rollback exception", e);
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public void resume() throws InterruptedException
{
pauseLock.lockInterruptibly();
try {
pauseRequested = false;
shouldResume.signalAll();
long nanos = TimeUnit.SECONDS.toNanos(5);
while (isPaused()) {
if (nanos <= 0L) {
throw new RuntimeException("Resume command was not accepted within 5 seconds");
}
nanos = shouldResume.awaitNanos(nanos);
}
}
finally {
pauseLock.unlock();
}
}
代码示例来源:origin: apache/incubator-druid
@VisibleForTesting
public void resume() throws InterruptedException
{
pauseLock.lockInterruptibly();
try {
pauseRequested = false;
shouldResume.signalAll();
long nanos = TimeUnit.SECONDS.toNanos(5);
while (isPaused()) {
if (nanos <= 0L) {
throw new RuntimeException("Resume command was not accepted within 5 seconds");
}
nanos = shouldResume.awaitNanos(nanos);
}
}
finally {
pauseLock.unlock();
}
}
代码示例来源:origin: apache/rocketmq
public int wipeWritePermOfBrokerByLock(final String brokerName) {
try {
try {
this.lock.writeLock().lockInterruptibly();
return wipeWritePermOfBroker(brokerName);
} finally {
this.lock.writeLock().unlock();
}
} catch (Exception e) {
log.error("wipeWritePermOfBrokerByLock Exception", e);
}
return 0;
}
代码示例来源:origin: apache/rocketmq
public String getAllConfigsFormatString() {
try {
readWriteLock.readLock().lockInterruptibly();
try {
return getAllConfigsInternal();
} finally {
readWriteLock.readLock().unlock();
}
} catch (InterruptedException e) {
log.error("getAllConfigsFormatString lock error");
}
return null;
}
代码示例来源:origin: apache/rocketmq
public long commit() {
try {
this.lockTreeMap.writeLock().lockInterruptibly();
try {
Long offset = this.consumingMsgOrderlyTreeMap.lastKey();
msgCount.addAndGet(0 - this.consumingMsgOrderlyTreeMap.size());
for (MessageExt msg : this.consumingMsgOrderlyTreeMap.values()) {
msgSize.addAndGet(0 - msg.getBody().length);
}
this.consumingMsgOrderlyTreeMap.clear();
if (offset != null) {
return offset + 1;
}
} finally {
this.lockTreeMap.writeLock().unlock();
}
} catch (InterruptedException e) {
log.error("commit exception", e);
}
return -1;
}
代码示例来源:origin: apache/rocketmq
/**
* The store path will be gotten from the field of object.
*
* @throws java.lang.RuntimeException if the field of object is not exist.
*/
public void setStorePathFromConfig(Object object, String fieldName) {
assert object != null;
try {
readWriteLock.writeLock().lockInterruptibly();
try {
this.storePathFromCOnfig= true;
this.storePathObject = object;
// check
this.storePathField = object.getClass().getDeclaredField(fieldName);
assert this.storePathField != null
&& !Modifier.isStatic(this.storePathField.getModifiers());
this.storePathField.setAccessible(true);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} finally {
readWriteLock.writeLock().unlock();
}
} catch (InterruptedException e) {
log.error("setStorePathFromConfig lock error");
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public E take() throws InterruptedException
{
E e;
takeLock.lockInterruptibly();
try {
while (elementCount.get() == 0) {
notEmpty.await();
}
e = delegate.remove();
elementRemoved(e);
}
finally {
takeLock.unlock();
}
if (e != null) {
signalNotFull();
}
return e;
}
代码示例来源:origin: apache/rocketmq
public void makeMessageToCosumeAgain(List
try {
this.lockTreeMap.writeLock().lockInterruptibly();
try {
for (MessageExt msg : msgs) {
this.consumingMsgOrderlyTreeMap.remove(msg.getQueueOffset());
this.msgTreeMap.put(msg.getQueueOffset(), msg);
}
} finally {
this.lockTreeMap.writeLock().unlock();
}
} catch (InterruptedException e) {
log.error("makeMessageToCosumeAgain exception", e);
}
}
代码示例来源:origin: apache/rocketmq
private String getStorePath() {
String realStorePath = null;
try {
readWriteLock.readLock().lockInterruptibly();
try {
realStorePath = this.storePath;
if (this.storePathFromConfig) {
try {
realStorePath = (String) storePathField.get(this.storePathObject);
} catch (IllegalAccessException e) {
log.error("getStorePath error, ", e);
}
}
} finally {
readWriteLock.readLock().unlock();
}
} catch (InterruptedException e) {
log.error("getStorePath lock error");
}
return realStorePath;
}
代码示例来源:origin: apache/rocketmq
public List
List
final long now = System.currentTimeMillis();
try {
this.lockTreeMap.writeLock().lockInterruptibly();
this.lastCOnsumeTimestamp= now;
try {
if (!this.msgTreeMap.isEmpty()) {
for (int i = 0; i
if (entry != null) {
result.add(entry.getValue());
consumingMsgOrderlyTreeMap.put(entry.getKey(), entry.getValue());
} else {
break;
}
}
}
if (result.isEmpty()) {
cOnsuming= false;
}
} finally {
this.lockTreeMap.writeLock().unlock();
}
} catch (InterruptedException e) {
log.error("take Messages exception", e);
}
return result;
}
代码示例来源:origin: apache/rocketmq
/**
* register config properties
*
* @return the current Configuration object
*/
public Configuration registerConfig(Properties extProperties) {
if (extProperties == null) {
return this;
}
try {
readWriteLock.writeLock().lockInterruptibly();
try {
merge(extProperties, this.allConfigs);
} finally {
readWriteLock.writeLock().unlock();
}
} catch (InterruptedException e) {
log.error("register lock error. {}" + extProperties);
}
return this;
}
代码示例来源:origin: apache/incubator-druid
@Override
public boolean close()
{
final Lock writeLock = startStopSync.writeLock();
try {
writeLock.lockInterruptibly();
}
catch (InterruptedException e) {
throw Throwables.propagate(e);
}
try {
if (entry == null) {
LOG.warn("Not started! [%s]", extractorID);
return true;
}
entry.close();
entry = null;
return true;
}
finally {
writeLock.unlock();
}
}