java.util.concurrent.locks.ReentrantLock.newCondition()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(212)

本文整理了Java中java.util.concurrent.locks.ReentrantLock.newCondition方法的一些代码示例,展示了ReentrantLock.newCondition的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ReentrantLock.newCondition方法的具体详情如下:
包路径:java.util.concurrent.locks.ReentrantLock
类名称:ReentrantLock
方法名:newCondition

ReentrantLock.newCondition介绍

[英]Returns a Condition instance for use with this Lock instance.

The returned Condition instance supports the same usages as do the Object monitor methods ( Object#wait(), Object#notify, and Object#notifyAll) when used with the built-in monitor lock.

  • If this lock is not held when any of the ConditionCondition#await() or Condition#signal methods are called, then an IllegalMonitorStateException is thrown.
  • When the condition Condition#await()methods are called the lock is released and, before they return, the lock is reacquired and the lock hold count restored to what it was when the method was called.
  • If a thread is Thread#interruptwhile waiting then the wait will terminate, an InterruptedException will be thrown, and the thread's interrupted status will be cleared.
  • Waiting threads are signalled in FIFO order.
  • The ordering of lock reacquisition for threads returning from waiting methods is the same as for threads initially acquiring the lock, which is in the default case not specified, but for fair locks favors those threads that have been waiting the longest.
    [中]返回用于此锁实例的条件实例。
    当与内置监视器锁一起使用时,返回的条件实例支持与对象监视器方法(Object#wait()、Object#notify和Object#notifyAll)相同的用法。
    *如果在调用任何ConditionCondition#await()或Condition#signal方法时未保持此锁,则会引发非法MonitorStateException。
    *当调用条件#await()方法时,锁被释放,在它们返回之前,锁被重新获取,锁保持计数恢复到调用该方法时的状态。
    *如果线程是线程#interruptwhile waiting,那么等待将终止,将抛出InterruptedException,线程的中断状态将被清除。
    等待线程按FIFO顺序发出信号。
    对于从等待方法返回的线程,锁重新获取的顺序与最初获取锁的线程相同,这在默认情况下没有指定,但对于fair
    锁,优先于等待时间最长的线程。

代码示例

代码示例来源:origin: alibaba/druid

public DruidAbstractDataSource(boolean lockFair){
  lock = new ReentrantLock(lockFair);
  notEmpty = lock.newCondition();
  empty = lock.newCondition();
}

代码示例来源:origin: apache/flink

/**
 * Creates a new empty queue, reserving space for at least the specified number
 * of elements. The queue can still grow, of more elements are added than the
 * reserved space.
 *
 * @param initialSize The number of elements to reserve space for.
 */
public ClosableBlockingQueue(int initialSize) {
  this.lock = new ReentrantLock(true);
  this.nonEmpty = this.lock.newCondition();
  this.elements = new ArrayDeque<>(initialSize);
  this.open = true;
}

代码示例来源:origin: alibaba/cobar

public BufferQueue(int capacity) {
  items = new ByteBuffer[capacity];
  lock = new ReentrantLock();
  notFull = lock.newCondition();
}

代码示例来源:origin: MovingBlocks/Terasology

public DynamicPriorityBlockingQueue(Comparator<T> comparator) {
  this.comparator = comparator;
  lock = new ReentrantLock();
  notEmpty = lock.newCondition();
}

代码示例来源:origin: igniterealtime/Smack

@SuppressWarnings("unchecked")
public ArrayBlockingQueueWithShutdown(int capacity, boolean fair) {
  if (capacity <= 0)
    throw new IllegalArgumentException();
  items = (E[]) new Object[capacity];
  lock = new ReentrantLock(fair);
  notEmpty = lock.newCondition();
  notFull = lock.newCondition();
}

代码示例来源:origin: apache/incubator-druid

public DefaultBlockingPool(
  Supplier<T> generator,
  int limit
)
{
 this.objects = new ArrayDeque<>(limit);
 this.maxSize = limit;
 for (int i = 0; i < limit; i++) {
  objects.add(generator.get());
 }
 this.lock = new ReentrantLock();
 this.notEnough = lock.newCondition();
}

代码示例来源:origin: Alluxio/alluxio

/**
 * Internal constructor that can provide an object to be used for the internal queue.
 *
 * @param maxCapacity bhe maximum of resources in this pool
 * @param resources blocking queue to use
 */
protected ResourcePool(int maxCapacity, ConcurrentLinkedQueue<T> resources) {
 mTakeLock = new ReentrantLock();
 mNotEmpty = mTakeLock.newCondition();
 mMaxCapacity = maxCapacity;
 mCurrentCapacity = new AtomicInteger();
 mResources = resources;
}

代码示例来源:origin: alibaba/druid

public void setUseUnfairLock(boolean useUnfairLock) {
  if (lock.isFair() == !useUnfairLock) {
    return;
  }
  if (!this.inited) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
      if (!this.inited) {
        this.lock = new ReentrantLock(!useUnfairLock);
        this.notEmpty = this.lock.newCondition();
        this.empty = this.lock.newCondition();
        this.useUnfairLock = useUnfairLock;
      }
    } finally {
      lock.unlock();
    }
  }
}

代码示例来源:origin: robovm/robovm

/**
 * Creates an {@code ArrayBlockingQueue} with the given (fixed)
 * capacity and the specified access policy.
 *
 * @param capacity the capacity of this queue
 * @param fair if {@code true} then queue accesses for threads blocked
 *        on insertion or removal, are processed in FIFO order;
 *        if {@code false} the access order is unspecified.
 * @throws IllegalArgumentException if {@code capacity < 1}
 */
public ArrayBlockingQueue(int capacity, boolean fair) {
  if (capacity <= 0)
    throw new IllegalArgumentException();
  this.items = new Object[capacity];
  lock = new ReentrantLock(fair);
  notEmpty = lock.newCondition();
  notFull =  lock.newCondition();
}

代码示例来源:origin: apache/ignite

/**
 * @param concLvl Concurrency level, must be a power of two.
 */
public OffheapReadWriteLock(int concLvl) {
  if ((concLvl & concLvl - 1) != 0)
    throw new IllegalArgumentException("Concurrency level must be a power of 2: " + concLvl);
  monitorsMask = concLvl - 1;
  locks = new ReentrantLock[concLvl];
  readConditions = new Condition[concLvl];
  writeConditions = new Condition[concLvl];
  balancers = new AtomicInteger[concLvl];
  for (int i = 0; i < locks.length; i++) {
    ReentrantLock lock = new ReentrantLock();
    locks[i] = lock;
    readConditions[i] = lock.newCondition();
    writeConditions[i] = lock.newCondition();
    balancers[i] = new AtomicInteger(0);
  }
}

代码示例来源:origin: alibaba/druid

public void setMaxWait(long maxWaitMillis) {
  if (maxWaitMillis == this.maxWait) {
    return;
  }
  if (maxWaitMillis > 0 && useUnfairLock == null && !this.inited) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
      if ((!this.inited) && (!lock.isFair())) {
        this.lock = new ReentrantLock(true);
        this.notEmpty = this.lock.newCondition();
        this.empty = this.lock.newCondition();
      }
    } finally {
      lock.unlock();
    }
  }
  if (inited) {
    LOG.error("maxWait changed : " + this.maxWait + " -> " + maxWaitMillis);
  }
  this.maxWait = maxWaitMillis;
}

代码示例来源:origin: apache/flink

public OrderedStreamElementQueue(
    int capacity,
    Executor executor,
    OperatorActions operatorActions) {
  Preconditions.checkArgument(capacity > 0, "The capacity must be larger than 0.");
  this.capacity = capacity;
  this.executor = Preconditions.checkNotNull(executor, "executor");
  this.operatorActions = Preconditions.checkNotNull(operatorActions, "operatorActions");
  this.lock = new ReentrantLock(false);
  this.headIsCompleted = lock.newCondition();
  this.notFull = lock.newCondition();
  this.queue = new ArrayDeque<>(capacity);
}

代码示例来源:origin: robovm/robovm

/**
 * Creates a {@code PriorityBlockingQueue} with the specified initial
 * capacity that orders its elements according to the specified
 * comparator.
 *
 * @param initialCapacity the initial capacity for this priority queue
 * @param  comparator the comparator that will be used to order this
 *         priority queue.  If {@code null}, the {@linkplain Comparable
 *         natural ordering} of the elements will be used.
 * @throws IllegalArgumentException if {@code initialCapacity} is less
 *         than 1
 */
public PriorityBlockingQueue(int initialCapacity,
               Comparator<? super E> comparator) {
  if (initialCapacity < 1)
    throw new IllegalArgumentException();
  this.lock = new ReentrantLock();
  this.notEmpty = lock.newCondition();
  this.comparator = comparator;
  this.queue = new Object[initialCapacity];
}

代码示例来源:origin: apache/flink

public UnorderedStreamElementQueue(
    int capacity,
    Executor executor,
    OperatorActions operatorActions) {
  Preconditions.checkArgument(capacity > 0, "The capacity must be larger than 0.");
  this.capacity = capacity;
  this.executor = Preconditions.checkNotNull(executor, "executor");
  this.operatorActions = Preconditions.checkNotNull(operatorActions, "operatorActions");
  this.uncompletedQueue = new ArrayDeque<>(capacity);
  this.completedQueue = new ArrayDeque<>(capacity);
  this.firstSet = new HashSet<>(capacity);
  this.lastSet = firstSet;
  this.numberEntries = 0;
  this.lock = new ReentrantLock();
  this.notFull = lock.newCondition();
  this.hasCompletedEntries = lock.newCondition();
}

代码示例来源:origin: spotbugs/spotbugs

public static void main(String args[]) throws Exception {
    ReentrantLock lock1 = new ReentrantLock();
    ReadWriteLock rwLock = new ReentrantReadWriteLock();
    Lock lock2 = rwLock.readLock();
    Lock lock3 = rwLock.writeLock();
    rwLock.readLock();

    lock1.newCondition();
    lock2.newCondition();
    lock1.tryLock();
    lock2.tryLock();
    lock3.tryLock();

    synchronized (lock1) {
      System.out.println("Howdy");
    }
  }
}

代码示例来源:origin: hierynomus/sshj

/**
 * Creates this promise with given {@code name}, exception {@code chainer}, and associated {@code lock}.
 *
 * @param name    name of this promise
 * @param chainer {@link ExceptionChainer} that will be used for chaining exceptions
 * @param lock    lock to use
 */
public Promise(String name, ExceptionChainer<T> chainer, ReentrantLock lock, LoggerFactory loggerFactory) {
  this.name = name;
  this.chainer = chainer;
  this.lock = lock == null ? new ReentrantLock() : lock;
  this.log = loggerFactory.getLogger(getClass());
  this.cond = this.lock.newCondition();
}

代码示例来源:origin: ninjaframework/ninja

public DelayedRestartTrigger(
    RunClassInSeparateJvmMachine runClassInSeparateJvmMachine) {
  
  this.shutdown = false;
  this.setDaemon(true);
  this.setName("DelayedRestartTrigger");
  this.restartCount = new AtomicInteger(0);
  this.accumulatedTriggerCount = new AtomicInteger(0);
  this.restartLock = new ReentrantLock();
  this.restartRequested = this.restartLock.newCondition();
  this.runClassInSeparateJvmMachine = runClassInSeparateJvmMachine;
}

代码示例来源:origin: robovm/robovm

this.lock = new ReentrantLock();
this.notEmpty = lock.newCondition();
boolean heapify = true; // true if not known to be in heap order
boolean screen = true;  // true if must screen for nulls

代码示例来源:origin: stackoverflow.com

private final ReentrantLock lock = new ReentrantLock();
private final Condition tryAgain = lock.newCondition();
private volatile boolean finished = false;

代码示例来源:origin: fengjiachun/Jupiter

private final ReentrantLock lock = new ReentrantLock();
private final Condition notifyCondition = lock.newCondition();

相关文章