/** Handles size after put. */
  private void onPut() {
    cnt.incrementAndGet();

    int c;

    while ((c = cnt.get()) > max) {
      // Decrement count.
      if (cnt.compareAndSet(c, c - 1)) {
        try {
          K key = firstEntry().getKey();

          V val;

          // Make sure that an element is removed.
          while ((val = super.remove(firstEntry().getKey())) == null) {
            // No-op.
          }

          assert val != null;

          GridInClosure2<K, V> lsnr = this.lsnr;

          // Listener notification.
          if (lsnr != null) lsnr.apply(key, val);
        } catch (NoSuchElementException e1) {
          e1.printStackTrace(); // Should never happen.

          assert false : "Internal error in grid bounded ordered set.";
        }
      }
    }
  }
Esempio n. 2
0
  public void newCharacter(CharacterEvent ce) {
    int oldChar2type;

    // Previous character not typed correctly - 1 point penalty
    if (ce.source == generator.get()) {
      oldChar2type = char2type.getAndSet(ce.character); // 接收随机字母

      if (oldChar2type != -1) {
        score.decrementAndGet();
        setScore();
      }
    }
    // If character is extraneous - 1 point penalty
    // If character does not match - 1 point penalty
    else if (ce.source == typist.get()) {
      // 在此有个假设:即正在使用的该变量值不会被变更且程序代码完成时也是如此
      // 所有已经被我们设定的具有特定值的变量就应当是那个值。
      while (true) {
        oldChar2type = char2type.get(); // 获取上次已接收到的随机字母

        if (oldChar2type != ce.character) { // ce.character是用户输入的字母,现在作比较
          score.decrementAndGet();
          break;
        } else if (char2type.compareAndSet(oldChar2type, -1)) {
          score.incrementAndGet();
          break;
        }
      }

      setScore();
    }
  }
  /** {@inheritDoc} */
  @Override
  protected Collection<E> dequeue0(int cnt) {
    WindowHolder tup = ref.get();

    AtomicInteger size = tup.size();
    Collection<T> evts = tup.collection();

    Collection<E> resCol = new ArrayList<>(cnt);

    while (true) {
      int curSize = size.get();

      if (curSize > 0) {
        if (size.compareAndSet(curSize, curSize - 1)) {
          E res = pollInternal(evts, tup.set());

          if (res != null) {
            resCol.add(res);

            if (resCol.size() >= cnt) return resCol;
          } else {
            size.incrementAndGet();

            return resCol;
          }
        }
      } else return resCol;
    }
  }
  /**
   * Poll evicted internal implementation.
   *
   * @return Evicted element.
   */
  @Nullable
  private E pollEvictedInternal() {
    WindowHolder tup = ref.get();

    AtomicInteger size = tup.size();

    while (true) {
      int curSize = size.get();

      if (curSize > maxSize) {
        if (size.compareAndSet(curSize, curSize - 1)) {
          E evt = pollInternal(tup.collection(), tup.set());

          if (evt != null) return evt;
          else {
            // No actual events in queue, it means that other thread is just adding event.
            // return null as it is a concurrent add call.
            size.incrementAndGet();

            return null;
          }
        }
      } else return null;
    }
  }
 public void run() {
   done.countDown();
   remaining.incrementAndGet();
   int n;
   while (!Thread.interrupted() && (n = remaining.get()) > 0 && done.getCount() > 0) {
     if (remaining.compareAndSet(n, n - 1)) {
       try {
         pool.execute(this);
       } catch (RuntimeException ex) {
         System.out.print("*");
         while (done.getCount() > 0) done.countDown();
         return;
       }
     }
   }
 }
  public void testAdd() throws Exception {
    getIdentitiesOfFiles();

    fillFilesWithContent();

    validateFilesContent(version.byteValue());

    version.compareAndSet(1, 2);
    continuousWrite.compareAndSet(true, false);

    generateRemainingPagesQueueForAllFiles();

    executeConcurrentRandomReadAndWriteOperations();

    buffer.flushBuffer();

    validateFilesContent(version.byteValue());
  }