Package java.util.concurrent.locks

Examples of java.util.concurrent.locks.Condition


    }
  }

  ClientMmap getOrCreateClientMmap(ShortCircuitReplica replica,
      boolean anchored) {
    Condition newCond;
    lock.lock();
    try {
      while (replica.mmapData != null) {
        if (replica.mmapData instanceof MappedByteBuffer) {
          ref(replica);
          MappedByteBuffer mmap = (MappedByteBuffer)replica.mmapData;
          return new ClientMmap(replica, mmap, anchored);
        } else if (replica.mmapData instanceof Long) {
          long lastAttemptTimeMs = (Long)replica.mmapData;
          long delta = Time.monotonicNow() - lastAttemptTimeMs;
          if (delta < mmapRetryTimeoutMs) {
            if (LOG.isTraceEnabled()) {
              LOG.trace(this + ": can't create client mmap for " +
                  replica + " because we failed to " +
                  "create one just " + delta + "ms ago.");
            }
            return null;
          }
          if (LOG.isTraceEnabled()) {
            LOG.trace(this + ": retrying client mmap for " + replica +
                ", " + delta + " ms after the previous failure.");
          }
        } else if (replica.mmapData instanceof Condition) {
          Condition cond = (Condition)replica.mmapData;
          cond.awaitUninterruptibly();
        } else {
          Preconditions.checkState(false, "invalid mmapData type " +
              replica.mmapData.getClass().getName());
        }
      }
View Full Code Here


    }

    @Test
    public void testWaitQueueLength() throws Exception {
        final Condition condition = lock.newCondition();
        assertEquals(0, lock.getWaitQueueLength(condition));
        assertFalse(lock.hasWaiters(condition));

        final Exception[] ex=new Exception[1];
       
        Thread t = new Thread(){
            @Override
            public void run() {
                try {
                    lock.lock();
                    condition.await();
                    lock.unlock();
                } catch (Exception e) {
                    ex[0]=e;
                }
            }
        };

        t.start();

        UDIGTestUtil.inDisplayThreadWait(WAIT_LENGTH, new WaitCondition(){

            public boolean isTrue() {
                return lock.getWaitQueueLength(condition) == 1 || ex[0]!=null;
            }

        }, true);
        if( ex[0]!=null )
            throw ex[0];
        assertTrue(lock.hasWaiters(condition));

        lock.lock();
        condition.signal();
        lock.unlock();

        UDIGTestUtil.inDisplayThreadWait(WAIT_LENGTH, new WaitCondition(){

            public boolean isTrue() {
View Full Code Here

   
    @Test
    public void testConditionWithDisplay() throws Exception {
        lock.lock();
        try {
            final Condition condition = lock.newCondition();
            Display.getCurrent().asyncExec(new Runnable(){

                public void run() {
                    lock.lock();
                    try {
                        condition.signal();
                    } finally {
                        lock.unlock();
                    }
                }

            });

            condition.await();
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

    }
   
    @Ignore("fails in tycho")
    @Test
    public void testSignalAll() throws Exception {
            final Condition condition = lock.newCondition();
            final Exception[] exception=new Exception[1];
            final boolean[] awake=new boolean[2];
            Thread t=new Thread(){

                public void run() {
                    lock.lock();
                    try {
                        condition.await();
                        awake[0]=true;
                    } catch (InterruptedException e) {
                        exception[0]=e;
                    } finally {
                        lock.unlock();
                    }
                }

            };
            t.start();

            Thread t2=new Thread(){

                public void run() {
                    lock.lock();
                    try {
                        condition.await();
                        awake[1]=true;
                    } catch (InterruptedException e) {
                        exception[0]=e;
                    } finally {
                        lock.unlock();
                    }
                }

            };
            t2.start();

            UDIGTestUtil.inDisplayThreadWait(WAIT_LENGTH, new WaitCondition(){

                public boolean isTrue() {
                    return lock.getWaitQueueLength(condition)==2;
                }
               
            }, false);
            if( exception[0]!=null )
                throw exception[0];
            lock.lock();
            condition.signalAll();
            lock.unlock();
            UDIGTestUtil.inDisplayThreadWait(WAIT_LENGTH, new WaitCondition(){

                public boolean isTrue() {
                    return lock.getWaitQueueLength(condition)==0;
View Full Code Here

    public static void openDataChangedDialog( final ILayer layer, final Envelope dirtyArea ) {
        Display d = Display.getCurrent();
        if (d == null)
            d = Display.getDefault();

        Condition condition = blackboardLock.newCondition();
        try {
            blackboardLock.lock();
            if (dialog != null) {
                // we're in the display thread, which means that the viewport is repainting since the dialog has any other input blocked.
                // so just return
                if( Display.getCurrent()!=null )
                    return;
                // the issue is being resolved we should wait for it to be resolved.
                while( dialog != null ) {
                    try{
                            condition.await(500, TimeUnit.MILLISECONDS);
                    }catch(InterruptedException e){
                        return;
                    }
                }
                return;
View Full Code Here

      if (signalBeforeWaiting) {
        signalConditionsOfSatisfiedGuards(null);
      }
      incrementWaiters(guard);
      try {
        final Condition condition = guard.condition;
        do {
          try {
            condition.await();
          } catch (InterruptedException interrupt) {
            try {
              signalConditionsOfSatisfiedGuards(guard);
            } catch (Throwable throwable) {
              Thread.currentThread().interrupt();
View Full Code Here

      if (signalBeforeWaiting) {
        signalConditionsOfSatisfiedGuards(null);
      }
      incrementWaiters(guard);
      try {
        final Condition condition = guard.condition;
        do {
          condition.awaitUninterruptibly();
        } while (!guard.isSatisfied());
      } finally {
        decrementWaiters(guard);
      }
    }
View Full Code Here

      if (signalBeforeWaiting) {
        signalConditionsOfSatisfiedGuards(null);
      }
      incrementWaiters(guard);
      try {
        final Condition condition = guard.condition;
        do {
          if (remainingNanos <= 0) {
            return false;
          }
          try {
            remainingNanos = condition.awaitNanos(remainingNanos);
          } catch (InterruptedException interrupt) {
            try {
              signalConditionsOfSatisfiedGuards(guard);
            } catch (Throwable throwable) {
              Thread.currentThread().interrupt();
View Full Code Here

      }
      boolean interruptIgnored = false;
      try {
        incrementWaiters(guard);
        try {
          final Condition condition = guard.condition;
          long remainingNanos = timeoutNanos;
          do {
            if (remainingNanos <= 0) {
              return false;
            }
            try {
              remainingNanos = condition.awaitNanos(remainingNanos);
            } catch (InterruptedException ignored) {
              try {
                signalConditionsOfSatisfiedGuards(guard);
              } catch (Throwable throwable) {
                Thread.currentThread().interrupt();
View Full Code Here

  public List<BatchCommand<?,?>> poll() throws InterruptedException {
    lock.lockInterruptibly();
    try {
      outerLoop: while (true) {
        ++numThreads;
        Condition myCondition = null;
        if (!unusedConditions.isEmpty())
          myCondition = unusedConditions.pop();
        else {
          myCondition = lock.newCondition();
        }
        if (batches.size() >= numThreads) {
          batches.get(numThreads - 1).signaller = myCondition;
        } else {
          freeConditions.add(myCondition);
        }
        waitLoop: while (true) {
          int queuePosition = findQueuePosition(myCondition);
          if (queuePosition == -1) {
            if (state == State.STOPPED) {
              unusedConditions.add(myCondition);
              return null;
            }
            myCondition.await();
            continue waitLoop;
          }
          BatchInfo myBatch = batches.get(queuePosition);
          long waitDelay = myBatch.waitDelay();
          if (state != State.STOPPED && waitDelay > 0) {
            myCondition.await(waitDelay, TimeUnit.MILLISECONDS);
          } else {
            --numThreads;
            List<BatchCommand<?,?>> commands = myBatch
                .removeCommands(state == State.STOPPING);
            batches.remove(queuePosition);
View Full Code Here

TOP

Related Classes of java.util.concurrent.locks.Condition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.