java.util.concurrent.locks.ReentrantLock#newCondition()源码实例Demo

下面列出了java.util.concurrent.locks.ReentrantLock#newCondition() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jforgame   文件: CallbackTask.java
@Override
public Message call() throws Exception {
    try {
        CReqCallBack reqCallBack = (CReqCallBack) request;
        int index = CallBack.nextMsgId();
        reqCallBack.setIndex(index);
        reqCallBack.serialize();

        session.sendMessage(reqCallBack);

        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        long maxTimeOut = 5 * TimeUtil.ONE_SECOND;
        long startTime = System.currentTimeMillis();
        CallBackService callBackService = CallBackService.getInstance();
        while (System.currentTimeMillis() - startTime <= maxTimeOut) {
            CallBack c = callBackService.removeCallBack(index);
            if (c != null) {
                return c.getData();
            }

            try {
                lock.lockInterruptibly();
                condition.await(10, TimeUnit.MILLISECONDS);
            } catch (Exception e) {

            } finally {
                lock.unlock();
            }
        }
    } finally {
        C2SSessionPoolFactory.getInstance().returnSession(session);
    }
    // 超时返回
    return null;
}
 
源代码2 项目: diozero   文件: FirmataAdapter.java
public FirmataAdapter(FirmataEventListener eventListener) {
	this.eventListener = eventListener;

	running = new AtomicBoolean(false);
	responseQueue = new ConcurrentLinkedQueue<>();
	lock = new ReentrantLock();
	condition = lock.newCondition();
	pins = new ConcurrentHashMap<>();
	adcToGpioMapping = new ConcurrentHashMap<>();

	executor = Executors.newSingleThreadExecutor();
}
 
@Test
void testInterruptDuringRetrySleep() throws InterruptedException {
  ReentrantLock lock = new ReentrantLock();
  Condition noConnectionCondition = lock.newCondition();
  when(consumerContainerMock.isConnectionAvailable()).thenReturn(true);
  when(consumerContainerMock.ensureConsumersAreActive()).thenReturn(false);
  ConsumerContainerWatcherThread consumerContainerWatcherThread =
      new ConsumerContainerWatcherThread(consumerContainerMock, 1000000, lock,
          noConnectionCondition);
  consumerContainerWatcherThread.start();
  Thread.sleep(350);
  assertTrue(consumerContainerWatcherThread.isAlive());
  killThreadAndCheckState(consumerContainerWatcherThread);
}
 
源代码4 项目: gemfirexd-oss   文件: SingletonValue.java
public SingletonValue(SingletonBuilder<T> builder) {
  this.builder = builder;
  
  state = ValueState.NOT_SET;
  sync = new ReentrantLock();
  change = sync.newCondition();
}
 
源代码5 项目: j2objc   文件: ReentrantLockTest.java
public void testHasWaitersIMSE(boolean fair) {
    final ReentrantLock lock = new ReentrantLock(fair);
    final Condition c = lock.newCondition();
    try {
        lock.hasWaiters(c);
        shouldThrow();
    } catch (IllegalMonitorStateException success) {}
}
 
源代码6 项目: mt-flume   文件: ThriftRpcClient.java
public ConnectionPoolManager(int poolSize) {
  this.maxPoolSize = poolSize;
  availableClients = new LinkedList<ClientWrapper>();
  checkedOutClients = new HashSet<ClientWrapper>();
  poolLock = new ReentrantLock();
  availableClientsCondition = poolLock.newCondition();
  currentPoolSize = 0;
}
 
源代码7 项目: JALSE   文件: AbstractManualActionContext.java
/**
    * Creates a new instance of AbstractManualActionContext with the supplied engine, action and
    * source bindings.
    *
    * @param engine
    *            Parent engine.
    * @param action
    *            The action this context is for.
    * @param sourceBindings
    *            Bindings to shallow copy.
    */
   protected AbstractManualActionContext(final ActionEngine engine, final Action<T> action,
    final ActionBindings sourceBindings) {
super(engine, action, sourceBindings);
lock = new ReentrantLock();
ran = lock.newCondition();
done = new AtomicBoolean();
performing = new AtomicBoolean();
cancelled = new AtomicBoolean();
estimated = new AtomicLong();
unschedulable = unschedulableActionContext(this);
   }
 
源代码8 项目: jdk-source-analysis   文件: ReentrantLockTest.java
@Test
public void testCondition() throws InterruptedException {
    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    Thread thread = new Thread(new ConditionTask(lock, condition));
    thread.start();
    Thread.sleep(2000);
    lock.lock();
    condition.signal();
    lock.unlock();
}
 
源代码9 项目: MiBandDecompiled   文件: GifDecoder.java
public GifDecoder(GifAction gifaction)
{
    isDestroy = false;
    f = 1;
    A = new byte[256];
    B = 0;
    C = 0;
    D = 0;
    E = false;
    F = 0;
    O = new ArrayBlockingQueue(15);
    P = new ReentrantLock();
    Q = P.newCondition();
    R = P.newCondition();
    S = 0;
    T = false;
    U = new ArrayList(M);
    V = 0;
    W = false;
    X = null;
    Y = null;
    Z = false;
    aa = 0;
    ab = null;
    ac = 0;
    ad = null;
    ae = null;
    af = new int[256];
    X = gifaction;
}
 
源代码10 项目: diozero   文件: LcdGame.java
public LcdGame(LcdConnection lcdConnection, int leftGpio, int rightGpio, int okGpio) {
	random = new Random();
	lcd = new HD44780Lcd(lcdConnection, 20, 4);
	leftButton = new Button(leftGpio);
	rightButton = new Button(rightGpio);
	okButton = new Button(okGpio);
	
	lock = new ReentrantLock();
	cond = lock.newCondition();
	
	leftButton.whenReleased(this::leftReleased);
	rightButton.whenReleased(this::rightReleased);
	okButton.whenReleased(this::okReleased);
}
 
源代码11 项目: openjdk-jdk9   文件: ReentrantLockTest.java
public void testGetWaitQueueLengthIAE(boolean fair) {
    final ReentrantLock lock = new ReentrantLock(fair);
    final Condition c = lock.newCondition();
    final ReentrantLock lock2 = new ReentrantLock(fair);
    try {
        lock2.getWaitQueueLength(c);
        shouldThrow();
    } catch (IllegalArgumentException success) {}
}
 
源代码12 项目: lorne_core   文件: Task.java
protected Task() {
    lock = new ReentrantLock();
    condition = lock.newCondition();
}
 
源代码13 项目: Raincat   文件: BlockTask.java
public BlockTask() {
    lock = new ReentrantLock();
    condition = lock.newCondition();
    notify = false;
    remove = false;
}
 
PausableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
	super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
	threadLock = new ReentrantLock();
	condition = threadLock.newCondition();
}
 
源代码15 项目: consulo   文件: BlockingSet.java
public BlockingSet() {
  set = new HashSet<T>();
  lock = new ReentrantLock();
  unlock = lock.newCondition();
}
 
源代码16 项目: Camera2   文件: StateMachineImpl.java
public StateMachineImpl()
{
    mStateLock = new ReentrantLock();
    mStateChangedCondition = mStateLock.newCondition();
    mState = new StateUninitialized(this);
}
 
源代码17 项目: j2objc   文件: ArrayBlockingQueue.java
/**
 * 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();
}
 
源代码18 项目: database   文件: LinkedBlockingDeque.java
/**
 * Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity and
 * the caller's {@link ReentrantLock} object.
 * <p>
 * <strong>Caution:</strong> By using the caller's lock, this constructor
 * allows the caller to break the encapsulation of the synchronization and
 * lock-based notification (signals). This can be used advantageously to
 * create designs where an outer lock is shared by the collection which
 * avoid deadlock arising from blocking operations on an inner lock while
 * holding a distinct outer lock. However, the caller's decisions about its
 * lock are no longer independent of the design decisions within this class
 * since they share the same lock.
 * 
 * @param capacity
 *            the capacity of this deque
 * @param lock
 *            the lock object.
 * @throws IllegalArgumentException
 *             if {@code capacity} is less than 1
 */
public LinkedBlockingDeque(final int capacity, final ReentrantLock lock) {
    if (capacity <= 0) throw new IllegalArgumentException();
    if (lock == null) throw new NullPointerException();
    this.capacity = capacity;
    this.lock = lock;
    this.notEmpty = lock.newCondition();
    this.notFull = lock.newCondition();
}
 
源代码19 项目: jdk8u60   文件: ArrayBlockingQueue.java
/**
 * 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();
}
 
源代码20 项目: Android   文件: WifiSearcher.java
public WifiSearcher( Context context, SearchWifiListener listener ) {
    
    mContext = context;
    mSearchWifiListener = listener;
    
    mLock = new ReentrantLock();
    mCondition = mLock.newCondition();

    mWifiManager=(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);        
    
    mWifiReceiver = new WiFiScanReceiver();        
}