Esempio n. 1
0
 Consumer<S> check(long maxTime, PooledFatPipe parent) {
   if (parent != null && parent.group == null) return null;
   AtomicLongArray outUse = this.outUse;
   Consumer<S>[] consumers = this.consumers;
   long now = System.nanoTime();
   if (consumers.length > outUse.length()) {
     logger.warn("skipping check " + outUse.length() + "/" + consumers.length);
     return null;
   }
   try {
     for (int i = 0; i < consumers.length; i++) {
       long time = outUse.get(i);
       if (time == 0) continue;
       if (time == -1) continue;
       if (now < time + maxTime * 1_000_000) continue;
       if (parent != null) parent.group.list();
       return consumers[i];
     }
   } catch (Exception e) {
     logger.error("check", e);
   }
   return null;
 }
Esempio n. 2
-1
  /**
   * @param product
   * @param sequence
   * @return false if there is an error
   */
  boolean consume(S product, long sequence, long sleep) {
    if (product == null) return true;

    // make copies
    Consumer<S>[] consumers = this.consumers;
    AtomicLongArray outUse = this.outUse;
    long[] consumerSeqs = this.consumerSeqs;

    if (outUse.length() != consumers.length) return false;
    for (int j = 0; j < consumers.length; j++) {
      if (!consumers[j].isConsuming()) continue;
      long time = System.nanoTime();
      if (!outUse.compareAndSet(j, 0, time)) continue;
      try {
        if (sequence <= consumerSeqs[j]) {
          outUse.lazySet(j, 0);
          if (outUse != this.outUse) resetConsumer(consumers[j]);
          break;
        }
        consumerSeqs[j] = sequence;
        consumers[j].consume(product, time);
        if (sleep > 0) Thread.sleep(sleep);
        outUse.lazySet(j, 0);
        if (outUse != this.outUse) {
          resetConsumer(consumers[j]);
          break;
        }
      } catch (Exception e) {
        if (listener == null) logger.error("consume", e);
        else listener.exceptionThrown(e);
      }
    }
    finishConsuming(product, sequence);
    return true;
  }