com.google.common.util.concurrent.Monitor.enterWhen()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(4.6k)|赞(0)|评价(0)|浏览(158)

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

Monitor.enterWhen介绍

[英]Enters this monitor when the guard is satisfied. Blocks indefinitely, but may be interrupted.
[中]当警卫满意时,进入该监视器。无限期阻塞,但可能会被中断。

代码示例

代码示例来源:origin: apache/jackrabbit-oak

/**
 * Same as {@code monitor.enterWhen(guard)} but copes with that pesky {@code
 * InterruptedException} by catching it and setting this thread's
 * interrupted flag.
 */
private static boolean safeEnterWhen(Monitor monitor, Guard guard) {
  try {
    monitor.enterWhen(guard);
    return true;
  } catch (InterruptedException ignore) {
    currentThread().interrupt();
    return false;
  }
}

代码示例来源:origin: bbejeck/guava-blog

public void addToList(String item) throws InterruptedException {
    monitor.enterWhen(listBelowCapacity);
    try {
      list.add(item);
    } finally {
      monitor.leave();
    }
  }
}

代码示例来源:origin: bbejeck/guava-blog

public void demoEnterWhen() throws InterruptedException {
  monitor.enterWhen(conditionGuard);
  try {
    taskDoneCounter++;
    if (taskDoneCounter == stopTaskCount) {
      condition = false;
    }
  } finally {
    monitor.leave();
  }
}

代码示例来源:origin: org.daisy.pipeline/common-utils

/**
 * See {@link java.util.concurrent.BlockingQueue#take}, it also waits for any updating 
 * operations to finish.
 */
@Override
public Runnable take() throws InterruptedException {
    //int num=this.takes.incrementAndGet();
    //System.out.println("Take("+num+"): before mon");
    monitor.enterWhen(canTake);
    //System.out.println("Take("+num+"): entered monitor");
    Runnable res=this.delegate.poll();
    monitor.leave();
    //System.out.println("Take("+num+"): left monitor");
    return res;
}

代码示例来源:origin: com.google.guava/guava-tests

@Override
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
  final Monitor monitor = this.monitor;
  if (monitor.enterWhen(notEmpty, timeout, unit)) {
    try {
      return q.poll();
    } finally {
      monitor.leave();
    }
  } else {
    return null;
  }
}

代码示例来源:origin: org.daisy.pipeline/common-utils

/**
 * See {@link java.util.concurrent.BlockingQueue#add()}, This method may block if the queue 
 * is being updated.
 */
@SuppressWarnings("unchecked")
@Override
public synchronized boolean add(Runnable element) {
    boolean res;
    try {
        monitor.enterWhen(canAdd);
    } catch (InterruptedException e) {
        monitor.leave();
        throw new RuntimeException(e);
    }
    res=this.delegate.add((PrioritizableRunnable<T>)element);
    monitor.leave();
    return res;
}

代码示例来源:origin: com.google.guava/guava-tests

@Override
public E take() throws InterruptedException {
  final Monitor monitor = this.monitor;
  monitor.enterWhen(notEmpty);
  try {
    return q.poll();
  } finally {
    monitor.leave();
  }
}

代码示例来源:origin: org.daisy.pipeline/common-utils

/**
 * See {@link java.util.concurrent.BlockingQueue#offer()}, This method may block if the queue 
 * is being updated.
 */
@SuppressWarnings("unchecked")
@Override
public synchronized boolean offer(Runnable o) {
    boolean res;
    try {
        monitor.enterWhen(canAdd);
    } catch (InterruptedException e) {
        monitor.leave();
        throw new RuntimeException(e);
    }
    //System.out.println("offer: entered monitor");
    res = this.delegate.offer((PrioritizableRunnable<T>) o);
    //System.out.println("offer: left monitor");
    monitor.leave();
    return res;
}

代码示例来源:origin: com.google.guava/guava-tests

/**
 * Inserts the specified element at the tail of this queue, waiting
 * for space to become available if the queue is full.
 *
 * @throws InterruptedException {@inheritDoc}
 * @throws NullPointerException {@inheritDoc}
 */
@Override
public void put(E e) throws InterruptedException {
  if (e == null) throw new NullPointerException();
  final Monitor monitor = this.monitor;
  monitor.enterWhen(notFull);
  try {
    insert(e);
  } finally {
    monitor.leave();
  }
}

代码示例来源:origin: com.google.guava/guava-tests

@Override
public E take() throws InterruptedException {
  final Monitor monitor = this.monitor;
  monitor.enterWhen(notEmpty);
  try {
    return extract();
  } finally {
    monitor.leave();
  }
}

代码示例来源:origin: com.google.guava/guava-tests

@Override
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
  final Monitor monitor = this.monitor;
  if (monitor.enterWhen(notEmpty, timeout, unit)) {
    try {
      return extract();
    } finally {
      monitor.leave();
    }
  } else {
   return null;
  }
}

代码示例来源:origin: com.google.guava/guava-tests

/**
 * Inserts the specified element at the tail of this queue, waiting
 * up to the specified wait time for space to become available if
 * the queue is full.
 *
 * @throws InterruptedException {@inheritDoc}
 * @throws NullPointerException {@inheritDoc}
 */
@Override
public boolean offer(E e, long timeout, TimeUnit unit)
  throws InterruptedException {
  if (e == null) throw new NullPointerException();
  final Monitor monitor = this.monitor;
  if (monitor.enterWhen(notFull, timeout, unit)) {
    try {
      insert(e);
      return true;
    } finally {
      monitor.leave();
    }
  } else {
   return false;
  }
}

相关文章