Ejemplo n.º 1
0
  @Override
  public T poll(final ForeachContext<T> ctx) {
    int tid = ctx.getThreadId();
    int idx = getIndex(tid);

    if (current[idx] == null) current[idx] = pool.poll();

    T retval = null;
    while (current[idx] != null) {
      retval = current[idx].poll(ctx);

      if (retval == null) {
        current[idx] = pool.poll();
      } else {
        break;
      }
    }

    // Current and poll are empty, try our next queue
    if (current[idx] == null) {
      retval = next[idx].poll(ctx);
    }

    if (size != null && retval != null) size.decrementAndGet();

    return retval;
  }
Ejemplo n.º 2
0
  @Override
  public void add(T item, ForeachContext<T> ctx) {
    int tid = ctx.getThreadId();
    int idx = getIndex(tid);

    if (size != null) size.incrementAndGet();

    Worklist<T> n = next[idx];

    n.add(item, ctx);

    if (n.size() >= chunkSize) {
      pool.add(n);
      next[idx] = n.newInstance();
    }
  }