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

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

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

ReentrantLock.<init>介绍

[英]Creates an instance of ReentrantLock. This is equivalent to using ReentrantLock(false).
[中]创建ReentrantLock的实例。这相当于使用ReentrantLock(false)。

代码示例

代码示例来源:origin: ReactiveX/RxJava

BlockingObservableIterator(int batchSize) {
  this.queue = new SpscLinkedArrayQueue<T>(batchSize);
  this.lock = new ReentrantLock();
  this.condition = lock.newCondition();
}

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

public void clear() {
    Lock l = new ReentrantLock();
    l.lock();
    try {
      // do nothing
    } finally {
      l.unlock();
    }
  }
}

代码示例来源: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: stackoverflow.com

private Lock lock = new ReentrantLock();
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
  lock.lock();
  try {
    while(queue.size() == capacity) {
    notEmpty.signal();
  } finally {
    lock.unlock();
  lock.lock();
  try {
    while(queue.isEmpty()) {
    return item;
  } finally {
    lock.unlock();

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

Lock lock = new ReentrantLock()
if (lock.tryLock()) {
  try {
   // do stuff using the lock...
  }
  finally {
   lock.unlock();
  }
}
  ...

代码示例来源:origin: kiegroup/jbpm

protected void createLockOnNewProcessInstance(Long id, RuntimeEngine runtime) {
  if (!isUseLocking()) {
    logger.debug("Locking on runtime manager disabled");
    return;
  }
  ReentrantLock newLock = new ReentrantLock();
  ReentrantLock lock = engineLocks.putIfAbsent(id, newLock);
  if (lock == null) {
    lock = newLock;
  }
  lock.lock();
  logger.debug("[on new process instance] Lock {} created and stored in list by {}", lock, runtime);
}

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

String threadName = Thread.currentThread().getName();
  try {
    first.lock();
    latch.countDown();
    System.out.println(threadName + ": locked first lock");
    latch.await();
    System.out.println(threadName + ": attempting to lock second lock");
    second.lock();
    System.out.println(threadName + ": never reached");
  } catch (InterruptedException e) {
Lock lock1 = new ReentrantLock(), lock2 = new ReentrantLock();
new Thread(new Locker(latch, lock1, lock2), "Thread 1").start();
new Thread(new Locker(latch, lock2, lock1), "Thread 2").start();

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

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

代码示例来源:origin: HubSpot/Singularity

private void unlock(String requestId, String name, long start) {
 LOG.trace("{} - Unlocking {} ({})", name, requestId, JavaUtils.duration(start));
 ReentrantLock lock = requestLocks.computeIfAbsent(requestId, (r) -> new ReentrantLock());
 lock.unlock();
}

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

/**
   * Retrieves partition {@code data} correspondent to specified partition index if it exists in local storage or
   * loads it using the specified {@code supplier}. Unlike {@link ConcurrentMap#computeIfAbsent(Object, Function)},
   * this method guarantees that function will be called only once.
   *
   * @param <D> Type of data.
   * @param part Partition index.
   * @param supplier Partition {@code data} supplier.
   * @return Partition {@code data}.
   */
  @SuppressWarnings("unchecked")
  <D> D computeDataIfAbsent(int part, Supplier<D> supplier) {
    Object data = storage.get(part);

    if (data == null) {
      Lock lock = locks.computeIfAbsent(part, p -> new ReentrantLock());

      lock.lock();
      try {
        data = storage.computeIfAbsent(part, p -> supplier.get());
      }
      finally {
        lock.unlock();
      }
    }

    return (D)data;
  }
}

代码示例来源: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: stanfordnlp/CoreNLP

/**
 * {@inheritDoc}
 *
 * This method creates an async call to the server, and blocks until the server
 * has finished annotating the object.
 */
@Override
public void annotate(Annotation annotation) {
 final Lock lock = new ReentrantLock();
 final Condition annotationDone = lock.newCondition();
 annotate(Collections.singleton(annotation), 1, (Annotation annInput) -> {
  try {
   lock.lock();
   annotationDone.signal();
  } finally {
   lock.unlock();
  }
 });
 try {
  lock.lock();
  annotationDone.await();  // Only wait for one callback to complete; only annotating one document
 } catch (InterruptedException e) {
  log.info("Interrupt while waiting for annotation to return");
 } finally {
  lock.unlock();
 }
}

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

Lock lock = new ReentrantLock();
......
if (lock.tryLock())
{
  // Got the lock
  try
  {
    // Process record
  }
  finally
  {
    // Make sure to unlock so that we don't cause a deadlock
    lock.unlock();
  }
}
else
{
  // Someone else had the lock, abort
}

代码示例来源:origin: kiegroup/jbpm

protected void createLockOnGetEngine(Long id, RuntimeEngine runtime) {
  if (!isUseLocking()) {
    logger.debug("Locking on runtime manager disabled");
    return;
  }
  
  if (id != null) {
    ReentrantLock newLock = new ReentrantLock();
    ReentrantLock lock = engineLocks.putIfAbsent(id, newLock);
    if (lock == null) {
      lock = newLock;
      logger.debug("New lock created as it did not exist before");
    } else {
      logger.debug("Lock exists with {} waiting threads", lock.getQueueLength());
    }
    logger.debug("Trying to get a lock {} for {} by {}", lock, id, runtime);
    lock.lock();
    logger.debug("Lock {} taken for {} by {} for waiting threads by {}", lock, id, runtime, lock.hasQueuedThreads());
    
  }
  
}

代码示例来源:origin: ReactiveX/RxJava

BlockingFlowableIterator(int batchSize) {
  this.queue = new SpscArrayQueue<T>(batchSize);
  this.batchSize = batchSize;
  this.limit = batchSize - (batchSize >> 2);
  this.lock = new ReentrantLock();
  this.condition = lock.newCondition();
}

代码示例来源:origin: brianfrankcooper/YCSB

private void createColumnFamily(final String name) throws RocksDBException {
 COLUMN_FAMILY_LOCKS.putIfAbsent(name, new ReentrantLock());
 final Lock l = COLUMN_FAMILY_LOCKS.get(name);
 l.lock();
 try {
  if(!COLUMN_FAMILIES.containsKey(name)) {
   final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions().optimizeLevelStyleCompaction();
   final ColumnFamilyHandle cfHandle = rocksDb.createColumnFamily(
     new ColumnFamilyDescriptor(name.getBytes(UTF_8), cfOptions)
   );
   COLUMN_FAMILIES.put(name, new ColumnFamily(cfHandle, cfOptions));
  }
 } finally {
  l.unlock();
 }
}

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

ReentrantLock objectLock = null;
lock.lock();
try {
 value = (T) registry.getIfPresent(key);
  objectLock = locks.get(key);
 } else {
  objectLock = new ReentrantLock();
  locks.put(key, objectLock);
 lock.unlock();
objectLock.lock();
try{
 lock.lock();
 try {
  value = (T) registry.getIfPresent(key);
  lock.unlock();
  locks.remove(key);
 } finally {
  lock.unlock();

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

private final Lock lock = new ReentrantLock();
private final Condition isEmpty = lock.newCondition();
private final Condition isFull = lock.newCondition();
  lock.lock();
  try {
    while(!usedData) {//wait for data to be used
    lock.unlock();//interrupt or not, release lock
  lock.lock();
  try {
    while(usedData) {//usedData is lingo for empty
    lock.unlock();

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

Lock lock = this.locks.get(stringKey);
if (lock == null) {
 this.locks.putIfAbsent(stringKey, new ReentrantLock());
 lock = this.locks.get(stringKey);
  lock.unlock();

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

public void writeLock(final Object key) {
  ReentrantLock lock;
  synchronized (locks) {
    lock = (ReentrantLock) locks.get(key);
    if (lock == null) {
      lock = new ReentrantLock();
      locks.put(key, lock);
    }
  }
  // Must be outside the above synchronized section, since this call may block.
  lock.lock();
}

相关文章