当前位置: 首页>>代码示例>>Java>>正文


Java ReentrantLock.isHeldByCurrentThread方法代码示例

本文整理汇总了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();
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:26,代码来源:Monitor.java

示例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();
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:24,代码来源:Monitor.java

示例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;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:45,代码来源:MultiLockFairBlockingQueue.java

示例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;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:35,代码来源:MultiLockFairBlockingQueue.java

示例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;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:34,代码来源:FairBlockingQueue.java

示例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;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:66,代码来源:FairBlockingQueue.java


注:本文中的java.util.concurrent.locks.ReentrantLock.isHeldByCurrentThread方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。