private void awaitAvailableConnection(long timeout, TimeUnit unit) throws InterruptedException {
   waitLock.lock();
   waiter++;
   try {
     hasAvailableConnection.await(timeout, unit);
   } finally {
     waiter--;
     waitLock.unlock();
   }
 }
Ejemplo n.º 2
0
 public void resetTypist(CharacterSource newTypist) {
   try {
     scoreLock.lock();
     if (typist != null) typist.removeCharacterListener(this);
     typist = newTypist;
     if (typist != null) typist.addCharacterListener(this);
   } finally {
     scoreLock.unlock();
   }
 }
Ejemplo n.º 3
0
 public boolean trySendOnSharedLine(String message, long timeout, TimeUnit unit)
     throws InterruptedException {
   long nanosToLock = unit.toNanos(timeout) - estimatedNanosToSend(message);
   if (!lock.tryLock(nanosToLock, NANOSECONDS)) return false;
   try {
     return sendOnSharedLine(message);
   } finally {
     lock.unlock();
   }
 }
Ejemplo n.º 4
0
 public void resetScore() {
   try {
     scoreLock.lock();
     score = 0;
     char2type = -1;
     setScore();
   } finally {
     scoreLock.unlock();
   }
 }
Ejemplo n.º 5
0
 public void resetGenerator(CharacterSource newGenerator) {
   try {
     scoreLock.lock();
     if (generator != null) generator.removeCharacterListener(this);
     generator = newGenerator;
     if (generator != null) generator.addCharacterListener(this);
   } finally {
     scoreLock.unlock();
   }
 }
  public void setDone(boolean b) {
    try {
      lock.lock();
      done = b;

      if (!done) cv.signal();
    } finally {
      lock.unlock();
    }
  }
Ejemplo n.º 7
0
    public void deposit(int amount) {
      lock.lock(); // Acquire the lock
      try {
        balance += amount;
        System.out.println("Deposit " + amount + "\t\t\t\t\t" + getBalance());

        newDeposit.signalAll();
      } finally {
        lock.unlock(); // Release the lock
      }
    }
Ejemplo n.º 8
0
  /** Notify the worker that new entry to delete appeared. */
  void signal() {
    lock.lock();

    try {
      force = true;

      cond.signalAll();
    } finally {
      lock.unlock();
    }
  }
  private void signalAllAvailableConnection() {
    // Quick check if it's worth signaling to avoid locking
    if (waiter == 0) return;

    waitLock.lock();
    try {
      hasAvailableConnection.signalAll();
    } finally {
      waitLock.unlock();
    }
  }
  /**
   * Set local batch size for this sequences.
   *
   * @param size Sequence batch size. Must be more then 0.
   */
  @Override
  public void batchSize(int size) {
    A.ensure(size > 0, " Batch size can't be less then 0: " + size);

    lock.lock();

    try {
      batchSize = size;
    } finally {
      lock.unlock();
    }
  }
  /** {@inheritDoc} */
  @Override
  public long get() throws GridException {
    checkRemoved();

    lock.lock();

    try {
      return locVal;
    } finally {
      lock.unlock();
    }
  }
Ejemplo n.º 12
0
 public void waitForOthers() {
   lock.lock();
   NumCount++;
   if (NumCount == NumWait) {
     workers.signalAll();
     NumCount = 0;
   } else {
     try {
       workers.await();
     } catch (InterruptedException e) {
     }
   }
   lock.unlock();
 }
Ejemplo n.º 13
0
    public void withdraw(int amount) {
      lock.lock(); // Acquire the lock
      try {
        while (balance < amount) {
          System.out.println("\t\t\tWait for a deposit");
          newDeposit.await();
        }

        balance -= amount;
        System.out.println("\t\t\tWithdraw " + amount + "\t\t" + getBalance());
      } catch (InterruptedException ex) {
        ex.printStackTrace();
      } finally {
        lock.unlock(); // Release the lock
      }
    }
Ejemplo n.º 14
0
  /** {@inheritDoc} */
  @Override
  protected void body() throws InterruptedException {
    if (log.isDebugEnabled()) log.debug("Delete worker started.");

    while (!cancelled) {
      lock.lock();

      try {
        if (!cancelled && !force) cond.await(FREQUENCY, TimeUnit.MILLISECONDS);

        force = false; // Reset force flag.
      } finally {
        lock.unlock();
      }

      if (!cancelled) delete();
    }
  }
 public void run() {
   try {
     lock.lock();
     while (true) {
       try {
         if (done) {
           cv.await();
         } else {
           nextCharacter();
           cv.await(getPauseTime(), TimeUnit.MILLISECONDS);
         }
       } catch (InterruptedException ie) {
         return;
       }
     }
   } finally {
     lock.unlock();
   }
 }
Ejemplo n.º 16
0
 public void newCharacter(CharacterEvent ce) {
   scoreLock.lock();
   try {
     if (ce.source == generator) {
       if (char2type != -1) {
         score--;
         setScore();
       }
       char2type = ce.character;
     } else {
       if (char2type != ce.character) {
         score--;
       } else {
         score++;
         char2type = -1;
       }
     }
     setScore();
   } finally {
     scoreLock.unlock();
   }
 }
Ejemplo n.º 17
0
 private final void unlockWrite() {
   $lock.unlock();
 }
  /**
   * Synchronous sequence update operation. Will add given amount to the sequence value.
   *
   * @param l Increment amount.
   * @param updateCall Cache call that will update sequence reservation count in accordance with l.
   * @param updated If {@code true}, will return sequence value after update, otherwise will return
   *     sequence value prior to update.
   * @return Sequence value.
   * @throws GridException If update failed.
   */
  private long internalUpdate(long l, @Nullable Callable<Long> updateCall, boolean updated)
      throws GridException {
    checkRemoved();

    assert l > 0;

    lock.lock();

    try {
      // If reserved range isn't exhausted.
      if (locVal + l <= upBound) {
        long curVal = locVal;

        locVal += l;

        return updated ? locVal : curVal;
      }
    } finally {
      lock.unlock();
    }

    if (updateCall == null) updateCall = internalUpdate(l, updated);

    while (true) {
      if (updateGuard.compareAndSet(false, true)) {
        try {
          // This call must be outside lock.
          return CU.outTx(updateCall, ctx);
        } finally {
          lock.lock();

          try {
            updateGuard.set(false);

            cond.signalAll();
          } finally {
            lock.unlock();
          }
        }
      } else {
        lock.lock();

        try {
          while (locVal >= upBound && updateGuard.get()) {
            try {
              cond.await(500, MILLISECONDS);
            } catch (InterruptedException e) {
              throw new GridInterruptedException(e);
            }
          }

          checkRemoved();

          // If reserved range isn't exhausted.
          if (locVal + l <= upBound) {
            long curVal = locVal;

            locVal += l;

            return updated ? locVal : curVal;
          }
        } finally {
          lock.unlock();
        }
      }
    }
  }
  /**
   * Asynchronous sequence update operation. Will add given amount to the sequence value.
   *
   * @param l Increment amount.
   * @param updateCall Cache call that will update sequence reservation count in accordance with l.
   * @param updated If {@code true}, will return sequence value after update, otherwise will return
   *     sequence value prior to update.
   * @return Future indicating sequence value.
   * @throws GridException If update failed.
   */
  private GridFuture<Long> internalUpdateAsync(
      long l, @Nullable Callable<Long> updateCall, boolean updated) throws GridException {
    checkRemoved();

    A.ensure(l > 0, " Parameter mustn't be less then 1: " + l);

    lock.lock();

    try {
      // If reserved range isn't exhausted.
      if (locVal + l <= upBound) {
        long curVal = locVal;

        locVal += l;

        return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
      }
    } finally {
      lock.unlock();
    }

    if (updateCall == null) updateCall = internalUpdate(l, updated);

    while (true) {
      if (updateGuard.compareAndSet(false, true)) {
        try {
          // This call must be outside lock.
          return ctx.closures().callLocalSafe(updateCall, true);
        } finally {
          lock.lock();

          try {
            updateGuard.set(false);

            cond.signalAll();
          } finally {
            lock.unlock();
          }
        }
      } else {
        lock.lock();

        try {
          while (locVal >= upBound && updateGuard.get()) {
            try {
              cond.await(500, MILLISECONDS);
            } catch (InterruptedException e) {
              throw new GridInterruptedException(e);
            }
          }

          checkRemoved();

          // If reserved range isn't exhausted.
          if (locVal + l <= upBound) {
            long curVal = locVal;

            locVal += l;

            return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
          }
        } finally {
          lock.unlock();
        }
      }
    }
  }