本文整理汇总了Java中java.util.concurrent.locks.ReentrantLock.isHeldByCurrentThread方法的典型用法代码示例。如果您正苦于以下问题:Java ReentrantLock.isHeldByCurrentThread方法的具体用法?Java ReentrantLock.isHeldByCurrentThread怎么用?Java ReentrantLock.isHeldByCurrentThread使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.locks.ReentrantLock
的用法示例。
在下文中一共展示了ReentrantLock.isHeldByCurrentThread方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: enterWhen
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* Enters this monitor when the guard is satisfied. Blocks indefinitely, but may be interrupted.
*
* @throws InterruptedException if interrupted while waiting
*/
public void enterWhen(Guard guard) throws InterruptedException {
if (guard.monitor != this) {
throw new IllegalMonitorStateException();
}
final ReentrantLock lock = this.lock;
boolean signalBeforeWaiting = lock.isHeldByCurrentThread();
lock.lockInterruptibly();
boolean satisfied = false;
try {
if (!guard.isSatisfied()) {
await(guard, signalBeforeWaiting);
}
satisfied = true;
} finally {
if (!satisfied) {
leave();
}
}
}
示例2: enterWhenUninterruptibly
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* Enters this monitor when the guard is satisfied. Blocks indefinitely.
*/
public void enterWhenUninterruptibly(Guard guard) {
if (guard.monitor != this) {
throw new IllegalMonitorStateException();
}
final ReentrantLock lock = this.lock;
boolean signalBeforeWaiting = lock.isHeldByCurrentThread();
lock.lock();
boolean satisfied = false;
try {
if (!guard.isSatisfied()) {
awaitUninterruptibly(guard, signalBeforeWaiting);
}
satisfied = true;
} finally {
if (!satisfied) {
leave();
}
}
}
示例3: poll
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* Fair retrieval of an object in the queue.
* Objects are returned in the order the threads requested them.
* {@inheritDoc}
*/
@Override
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
int idx = getNextPoll();
E result = null;
final ReentrantLock lock = this.locks[idx];
boolean error = true;
//acquire the global lock until we know what to do
lock.lock();
try {
//check to see if we have objects
result = items[idx].poll();
if (result==null && timeout>0) {
//the queue is empty we will wait for an object
ExchangeCountDownLatch<E> c = new ExchangeCountDownLatch<E>(1);
//add to the bottom of the wait list
waiters[idx].addLast(c);
//unlock the global lock
lock.unlock();
//wait for the specified timeout
if (!c.await(timeout, unit)) {
//if we timed out, remove ourselves from the waitlist
lock.lock();
waiters[idx].remove(c);
lock.unlock();
}
//return the item we received, can be null if we timed out
result = c.getItem();
} else {
//we have an object, release
lock.unlock();
}
error = false;
} finally {
if (error && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
return result;
}
示例4: pollAsync
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* Request an item from the queue asynchronously
* @return - a future pending the result from the queue poll request
*/
public Future<E> pollAsync() {
int idx = getNextPoll();
Future<E> result = null;
final ReentrantLock lock = this.locks[idx];
boolean error = true;
//grab the global lock
lock.lock();
try {
//check to see if we have objects in the queue
E item = items[idx].poll();
if (item==null) {
//queue is empty, add ourselves as waiters
ExchangeCountDownLatch<E> c = new ExchangeCountDownLatch<E>(1);
waiters[idx].addLast(c);
lock.unlock();
//return a future that will wait for the object
result = new ItemFuture<E>(c);
} else {
lock.unlock();
//return a future with the item
result = new ItemFuture<E>(item);
}
error = false;
} finally {
if (error && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
return result;
}
示例5: pollAsync
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* Request an item from the queue asynchronously
* @return - a future pending the result from the queue poll request
*/
public Future<E> pollAsync() {
Future<E> result = null;
final ReentrantLock lock = this.lock;
boolean error = true;
//grab the global lock
lock.lock();
try {
//check to see if we have objects in the queue
E item = items.poll();
if (item==null) {
//queue is empty, add ourselves as waiters
ExchangeCountDownLatch<E> c = new ExchangeCountDownLatch<E>(1);
waiters.addLast(c);
lock.unlock();
//return a future that will wait for the object
result = new ItemFuture<E>(c);
} else {
lock.unlock();
//return a future with the item
result = new ItemFuture<E>(item);
}
error = false;
} finally {
if (error && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
return result;
}
示例6: poll
import java.util.concurrent.locks.ReentrantLock; //导入方法依赖的package包/类
/**
* Fair retrieval of an object in the queue.
* Objects are returned in the order the threads requested them.
* {@inheritDoc}
*/
@Override
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
E result = null;
final ReentrantLock lock = this.lock;
boolean error = true;
//acquire the global lock until we know what to do
lock.lock();
try {
//check to see if we have objects
result = items.poll();
if (result==null && timeout>0) {
//the queue is empty we will wait for an object
ExchangeCountDownLatch<E> c = new ExchangeCountDownLatch<E>(1);
//add to the bottom of the wait list
waiters.addLast(c);
//unlock the global lock
lock.unlock();
//wait for the specified timeout
boolean didtimeout = true;
InterruptedException interruptedException = null;
try {
//wait for the specified timeout
didtimeout = !c.await(timeout, unit);
} catch (InterruptedException ix) {
interruptedException = ix;
}
if (didtimeout) {
//if we timed out, or got interrupted
// remove ourselves from the waitlist
lock.lock();
try {
waiters.remove(c);
} finally {
lock.unlock();
}
}
//return the item we received, can be null if we timed out
result = c.getItem();
if (null!=interruptedException) {
//we got interrupted
if (null!=result) {
//we got a result - clear the interrupt status
//don't propagate cause we have removed a connection from pool
Thread.interrupted();
} else {
throw interruptedException;
}
}
} else {
//we have an object, release
lock.unlock();
}
error = false;
} finally {
if (error && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
return result;
}