本文整理了Java中java.util.concurrent.locks.ReentrantLock.lockInterruptibly
方法的一些代码示例,展示了ReentrantLock.lockInterruptibly
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ReentrantLock.lockInterruptibly
方法的具体详情如下:
包路径:java.util.concurrent.locks.ReentrantLock
类名称:ReentrantLock
方法名:lockInterruptibly
[英]Acquires the lock unless the current thread is Thread#interrupt.
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
If the lock is acquired by the current thread then the lock hold count is set to one.
If the current thread:
In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock.
[中]获取锁,除非当前线程是线程#中断。
如果锁未被另一个线程持有,则获取锁并立即返回,将锁持有计数设置为1。
如果当前线程已经持有该锁,那么持有计数将增加1,并且该方法立即返回。
如果锁由另一个线程持有,那么出于线程调度目的,当前线程将被禁用,并处于休眠状态,直到发生以下两种情况之一:
*锁被当前线程获取;或
*其他线程#中断当前线程。
如果锁是由当前线程获取的,则锁保持计数设置为1。
如果当前线程:
*在进入该方法时设置其中断状态;或
*是线程在获取锁时中断,
然后抛出InterruptedException,并清除当前线程的中断状态。
在该实现中,由于该方法是一个显式中断点,因此优先考虑对中断进行响应,而不是对锁的正常或可重入获取。
代码示例来源:origin: alibaba/canal
public void destory() {
try {
lock.lockInterruptibly();
timer.cancel();
binlogs.clear();
nextCondition.signalAll();// 唤醒线程,通知退出
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}
}
代码示例来源:origin: apache/incubator-druid
private T takeObject() throws InterruptedException
{
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (objects.isEmpty()) {
notEnough.await();
}
return objects.pop();
}
finally {
lock.unlock();
}
}
代码示例来源:origin: apache/incubator-druid
@Nullable
private T pollObject(long timeoutMs) throws InterruptedException
{
long nanos = TIME_UNIT.toNanos(timeoutMs);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (objects.isEmpty()) {
if (nanos <= 0) {
return null;
}
nanos = notEnough.awaitNanos(nanos);
}
return objects.pop();
}
finally {
lock.unlock();
}
}
代码示例来源:origin: alibaba/canal
public Events<Event> get(Position start, int batchSize) throws InterruptedException, CanalStoreException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
try {
while (!checkUnGetSlotAt((LogPosition) start, batchSize))
notEmpty.await();
} catch (InterruptedException ie) {
notEmpty.signal(); // propagate to non-interrupted thread
throw ie;
}
return doGet(start, batchSize);
} finally {
lock.unlock();
}
}
代码示例来源:origin: apache/flink
@Override
public AsyncResult poll() throws InterruptedException {
lock.lockInterruptibly();
try {
while (completedQueue.isEmpty()) {
hasCompletedEntries.await();
}
numberEntries--;
notFull.signalAll();
LOG.debug("Polled element from unordered stream element queue. New filling degree " +
"({}/{}).", numberEntries, capacity);
return completedQueue.poll();
} finally {
lock.unlock();
}
}
代码示例来源:origin: NLPchina/elasticsearch-sql
final long nanos = TimeUnit.MILLISECONDS.toNanos(maxWait);
final int maxWaitThreadCount = getMaxWaitThreadCount();
lock.lockInterruptibly();
} catch (InterruptedException e) {
connectErrorCount.incrementAndGet();
throw e;
} finally {
lock.unlock();
代码示例来源:origin: apache/geode
/**
* Inserts the specified element at the tail of this queue even if the queue is currently at its
* capacity. // GEMFIRE addition
*/
public void forcePut(E e) throws InterruptedException {
if (e == null)
throw new NullPointerException();
// Note: convention in all put/take/etc is to preset local var
// holding count negative to indicate failure unless set.
int c = -1;
final ReentrantLock putLock = this.putLock;
final AtomicInteger count = this.count;
putLock.lockInterruptibly();
try {
enqueue(e);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
}
代码示例来源:origin: google/guava
/**
* Enters this monitor if the guard is satisfied. Blocks indefinitely acquiring the lock, but does
* not wait for the guard to be satisfied, and may be interrupted.
*
* @return whether the monitor was entered, which guarantees that the guard is now satisfied
* @throws InterruptedException if interrupted while waiting
*/
public boolean enterIfInterruptibly(Guard guard) throws InterruptedException {
if (guard.monitor != this) {
throw new IllegalMonitorStateException();
}
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
boolean satisfied = false;
try {
return satisfied = guard.isSatisfied();
} finally {
if (!satisfied) {
lock.unlock();
}
}
}
代码示例来源:origin: nostra13/Android-Universal-Image-Loader
public E pollFirst(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
E x;
while ((x = unlinkFirst()) == null) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
return x;
} finally {
lock.unlock();
}
}
代码示例来源:origin: alibaba/canal
public void put(List<Event> data) throws InterruptedException, CanalStoreException {
if (data == null || data.isEmpty()) {
return;
}
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
try {
while (!checkFreeSlotAt(putSequence.get() + data.size())) { // 检查是否有空位
notFull.await(); // wait until not full
}
} catch (InterruptedException ie) {
notFull.signal(); // propagate to non-interrupted thread
throw ie;
}
doPut(data);
if (Thread.interrupted()) {
throw new InterruptedException();
}
} finally {
lock.unlock();
}
}
代码示例来源:origin: apache/flink
@Override
public <T> void put(StreamElementQueueEntry<T> streamElementQueueEntry) throws InterruptedException {
lock.lockInterruptibly();
try {
while (queue.size() >= capacity) {
notFull.await();
}
addEntry(streamElementQueueEntry);
} finally {
lock.unlock();
}
}
代码示例来源:origin: apache/flink
@Override
public AsyncResult poll() throws InterruptedException {
lock.lockInterruptibly();
try {
while (queue.isEmpty() || !queue.peek().isDone()) {
headIsCompleted.await();
}
notFull.signalAll();
LOG.debug("Polled head element from ordered stream element queue. New filling degree " +
"({}/{}).", queue.size() - 1, capacity);
return queue.poll();
} finally {
lock.unlock();
}
}
代码示例来源:origin: alibaba/druid
final long nanos = TimeUnit.MILLISECONDS.toNanos(maxWait);
final int maxWaitThreadCount = this.maxWaitThreadCount;
lock.unlock();
lock.lockInterruptibly();
} catch (InterruptedException e) {
connectErrorCountUpdater.incrementAndGet(this);
throw e;
} finally {
lock.unlock();
代码示例来源:origin: stackoverflow.com
protected Void doInBackground(Input... params) {
lock.lockInterruptibly();
tryAgain.signal();
lock.unlock();
代码示例来源:origin: alibaba/canal
private boolean offer(File file) {
try {
lock.lockInterruptibly();
if (file != null) {
if (!binlogs.contains(file)) {
binlogs.add(file);
nextCondition.signalAll();// 唤醒
return true;
}
}
nextCondition.signalAll();// 唤醒
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
} finally {
lock.unlock();
}
}
代码示例来源:origin: apache/flink
@Override
public Collection<StreamElementQueueEntry<?>> values() throws InterruptedException {
lock.lockInterruptibly();
try {
StreamElementQueueEntry<?>[] array = new StreamElementQueueEntry[queue.size()];
array = queue.toArray(array);
return Arrays.asList(array);
} finally {
lock.unlock();
}
}
代码示例来源:origin: nostra13/Android-Universal-Image-Loader
public E pollLast(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
E x;
while ((x = unlinkLast()) == null) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
return x;
} finally {
lock.unlock();
}
}
代码示例来源:origin: alibaba/cobar
public void put(ByteBuffer buffer) throws InterruptedException {
final ByteBuffer[] items = this.items;
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
try {
while (count == items.length) {
notFull.await();
}
} catch (InterruptedException ie) {
notFull.signal();
throw ie;
}
insert(buffer);
} finally {
lock.unlock();
}
}
代码示例来源:origin: apache/flink
@Override
public <T> void put(StreamElementQueueEntry<T> streamElementQueueEntry) throws InterruptedException {
lock.lockInterruptibly();
try {
while (numberEntries >= capacity) {
notFull.await();
}
addEntry(streamElementQueueEntry);
} finally {
lock.unlock();
}
}
代码示例来源:origin: alibaba/druid
lock.lockInterruptibly();
} catch (InterruptedException e2) {
break;
empty.await();
empty.await();
continue;
lock.unlock();
notEmpty.signalAll();
} finally {
lock.unlock();
内容来源于网络,如有侵权,请联系作者删除!