ConcurrentHashMap 源码分析04之内部类ReduceTask

这里的内部类基本类似,这里只选取 MapReduceMappingsTask、MapReduceEntriesToDoubleTask详解。

1.3.35 ReduceKeysTask

@SuppressWarnings("serial")
static final class ReduceKeysTask<K,V>
    extends BulkTask<K,V,K> {
    final BiFunction<? super K, ? super K, ? extends K> reducer;
    K result;
    ReduceKeysTask<K,V> rights, nextRight;
    ReduceKeysTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         ReduceKeysTask<K,V> nextRight,
         BiFunction<? super K, ? super K, ? extends K> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.reducer = reducer;
    }
    public final K getRawResult() { return result; }
    public final void compute() {
        final BiFunction<? super K, ? super K, ? extends K> reducer;
        if ((reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new ReduceKeysTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, reducer)).fork();
            }
            K r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                K u = p.key;
                r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                ReduceKeysTask<K,V>
                    t = (ReduceKeysTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    K tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.36 ReduceValuesTask

@SuppressWarnings("serial")
static final class ReduceValuesTask<K,V>
    extends BulkTask<K,V,V> {
    final BiFunction<? super V, ? super V, ? extends V> reducer;
    V result;
    ReduceValuesTask<K,V> rights, nextRight;
    ReduceValuesTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         ReduceValuesTask<K,V> nextRight,
         BiFunction<? super V, ? super V, ? extends V> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.reducer = reducer;
    }
    public final V getRawResult() { return result; }
    public final void compute() {
        final BiFunction<? super V, ? super V, ? extends V> reducer;
        if ((reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new ReduceValuesTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, reducer)).fork();
            }
            V r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                V v = p.val;
                r = (r == null) ? v : reducer.apply(r, v);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                ReduceValuesTask<K,V>
                    t = (ReduceValuesTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    V tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.37 ReduceEntriesTask

@SuppressWarnings("serial")
static final class ReduceEntriesTask<K,V>
    extends BulkTask<K,V,Map.Entry<K,V>> {
    final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
    Map.Entry<K,V> result;
    ReduceEntriesTask<K,V> rights, nextRight;
    ReduceEntriesTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         ReduceEntriesTask<K,V> nextRight,
         BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.reducer = reducer;
    }
    public final Map.Entry<K,V> getRawResult() { return result; }
    public final void compute() {
        final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
        if ((reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new ReduceEntriesTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, reducer)).fork();
            }
            Map.Entry<K,V> r = null;
            for (Node<K,V> p; (p = advance()) != null; )
                r = (r == null) ? p : reducer.apply(r, p);
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                ReduceEntriesTask<K,V>
                    t = (ReduceEntriesTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    Map.Entry<K,V> tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.38 MapReduceKeysTask

@SuppressWarnings("serial")
static final class MapReduceKeysTask<K,V,U>
    extends BulkTask<K,V,U> {
    final Function<? super K, ? extends U> transformer;
    final BiFunction<? super U, ? super U, ? extends U> reducer;
    U result;
    MapReduceKeysTask<K,V,U> rights, nextRight;
    MapReduceKeysTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceKeysTask<K,V,U> nextRight,
         Function<? super K, ? extends U> transformer,
         BiFunction<? super U, ? super U, ? extends U> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.reducer = reducer;
    }
    public final U getRawResult() { return result; }
    public final void compute() {
        final Function<? super K, ? extends U> transformer;
        final BiFunction<? super U, ? super U, ? extends U> reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceKeysTask<K,V,U>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, reducer)).fork();
            }
            U r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                U u;
                if ((u = transformer.apply(p.key)) != null)
                    r = (r == null) ? u : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceKeysTask<K,V,U>
                    t = (MapReduceKeysTask<K,V,U>)c,
                    s = t.rights;
                while (s != null) {
                    U tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.39 MapReduceValuesTask

@SuppressWarnings("serial")
static final class MapReduceValuesTask<K,V,U>
    extends BulkTask<K,V,U> {
    final Function<? super V, ? extends U> transformer;
    final BiFunction<? super U, ? super U, ? extends U> reducer;
    U result;
    MapReduceValuesTask<K,V,U> rights, nextRight;
    MapReduceValuesTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceValuesTask<K,V,U> nextRight,
         Function<? super V, ? extends U> transformer,
         BiFunction<? super U, ? super U, ? extends U> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.reducer = reducer;
    }
    public final U getRawResult() { return result; }
    public final void compute() {
        final Function<? super V, ? extends U> transformer;
        final BiFunction<? super U, ? super U, ? extends U> reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceValuesTask<K,V,U>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, reducer)).fork();
            }
            U r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                U u;
                if ((u = transformer.apply(p.val)) != null)
                    r = (r == null) ? u : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceValuesTask<K,V,U>
                    t = (MapReduceValuesTask<K,V,U>)c,
                    s = t.rights;
                while (s != null) {
                    U tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.40 MapReduceEntriesTask

@SuppressWarnings("serial")
static final class MapReduceEntriesTask<K,V,U>
    extends BulkTask<K,V,U> {
    final Function<Map.Entry<K,V>, ? extends U> transformer;
    final BiFunction<? super U, ? super U, ? extends U> reducer;
    U result;
    MapReduceEntriesTask<K,V,U> rights, nextRight;
    MapReduceEntriesTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceEntriesTask<K,V,U> nextRight,
         Function<Map.Entry<K,V>, ? extends U> transformer,
         BiFunction<? super U, ? super U, ? extends U> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.reducer = reducer;
    }
    public final U getRawResult() { return result; }
    public final void compute() {
        final Function<Map.Entry<K,V>, ? extends U> transformer;
        final BiFunction<? super U, ? super U, ? extends U> reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceEntriesTask<K,V,U>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, reducer)).fork();
            }
            U r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                U u;
                if ((u = transformer.apply(p)) != null)
                    r = (r == null) ? u : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceEntriesTask<K,V,U>
                    t = (MapReduceEntriesTask<K,V,U>)c,
                    s = t.rights;
                while (s != null) {
                    U tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.41 MapReduceMappingsTask

@SuppressWarnings("serial")
static final class MapReduceMappingsTask<K,V,U>
    extends BulkTask<K,V,U> {
    final BiFunction<? super K, ? super V, ? extends U> transformer;
    final BiFunction<? super U, ? super U, ? extends U> reducer;
    U result; // 当前任务的返回结果
    /* 指向下一任务的指针,rights 用于分解任务时的首节点,nextRight 用于该首节点外的其他的节点 */
    MapReduceMappingsTask<K,V,U> rights, nextRight;
    MapReduceMappingsTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceMappingsTask<K,V,U> nextRight,
         BiFunction<? super K, ? super V, ? extends U> transformer,
         BiFunction<? super U, ? super U, ? extends U> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.reducer = reducer;
    }
    /* 获取结果 */
    public final U getRawResult() { return result; }
    public final void compute() {
        final BiFunction<? super K, ? super V, ? extends U> transformer;
        final BiFunction<? super U, ? super U, ? extends U> reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            /* 循环分解遍历任务,bacth > 0 则将当前map对半分解 */
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1); // 分任务 + 1
                /* fork() 函数只是将任务提交到队列,剩下的交给 pool 执行 */
                (rights = new MapReduceMappingsTask<K,V,U>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, reducer)).fork();
            }
            /* 循环遍历当前任务的所有元素,叠加不为 null 的返回结果 */
            U r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                U u;
                /* u: transformer 转换得到的值, r: 累加值,默认为 null  */
                if ((u = transformer.apply(p.key, p.val)) != null)
                    r = (r == null) ? u : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            /* 循环累加所有任务的 result,nextComplete()是获取当前对象的 complete
             * 拥有第一个元素的对象的 complete 为 null,执行 quietlyComplete() 完成任务 */
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceMappingsTask<K,V,U>
                    t = (MapReduceMappingsTask<K,V,U>)c,
                    s = t.rights;
                while (s != null) {
                    U tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    /* 这里使用 t.right 是为了略过已累加的节点 */                
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.42 MapReduceKeysToDoubleTask

@SuppressWarnings("serial")
staticfinal class MapReduceKeysToDoubleTask<K,V>
    extends BulkTask<K,V,Double> {
    final ToDoubleFunction<? super K> transformer;
    final DoubleBinaryOperator reducer;
    final double basis;
    double result;
    MapReduceKeysToDoubleTask<K,V> rights, nextRight;
    MapReduceKeysToDoubleTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceKeysToDoubleTask<K,V> nextRight,
         ToDoubleFunction<? super K> transformer,
         double basis,
         DoubleBinaryOperator reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.basis = basis; this.reducer = reducer;
    }
    public final Double getRawResult() { return result; }
    public final void compute() {
        final ToDoubleFunction<? super K> transformer;
        final DoubleBinaryOperator reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            double r = this.basis;
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceKeysToDoubleTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, r, reducer)).fork();
            }
            for (Node<K,V> p; (p = advance()) != null; )
                r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceKeysToDoubleTask<K,V>
                    t = (MapReduceKeysToDoubleTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    t.result = reducer.applyAsDouble(t.result, s.result);
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.43 MapReduceValuesToDoubleTask

@SuppressWarnings("serial")
static final class MapReduceValuesToDoubleTask<K,V>
    extends BulkTask<K,V,Double> {
    final ToDoubleFunction<? super V> transformer;
    final DoubleBinaryOperator reducer;
    final double basis;
    double result;
    MapReduceValuesToDoubleTask<K,V> rights, nextRight;
    MapReduceValuesToDoubleTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceValuesToDoubleTask<K,V> nextRight,
         ToDoubleFunction<? super V> transformer,
         double basis,
         DoubleBinaryOperator reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.basis = basis; this.reducer = reducer;
    }
    public final Double getRawResult() { return result; }
    public final void compute() {
        final ToDoubleFunction<? super V> transformer;
        final DoubleBinaryOperator reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            double r = this.basis;
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceValuesToDoubleTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, r, reducer)).fork();
            }
            for (Node<K,V> p; (p = advance()) != null; )
                r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceValuesToDoubleTask<K,V>
                    t = (MapReduceValuesToDoubleTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    t.result = reducer.applyAsDouble(t.result, s.result);
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.44 MapReduceEntriesToDoubleTask

@SuppressWarnings("serial")
static final class MapReduceEntriesToDoubleTask<K,V>
    extends BulkTask<K,V,Double> {
    final ToDoubleFunction<Map.Entry<K,V>> transformer;
    final DoubleBinaryOperator reducer;
    final double basis;
    double result;
    MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
    MapReduceEntriesToDoubleTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceEntriesToDoubleTask<K,V> nextRight,
         ToDoubleFunction<Map.Entry<K,V>> transformer,
         double basis,
         DoubleBinaryOperator reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.basis = basis; this.reducer = reducer;
    }
    /* 获取累加结果 */
    public final Double getRawResult() { return result; }
    public final void compute() {
        final ToDoubleFunction<Map.Entry<K,V>> transformer;
        final DoubleBinaryOperator reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            double r = this.basis; // 累加默认值为传入的值
            /* 循环分解任务 */
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceEntriesToDoubleTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, r, reducer)).fork();
            }
            /* 循环遍历元素,进行累加操作 */
            for (Node<K,V> p; (p = advance()) != null; )
                r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
            result = r;
            CountedCompleter<?> c;
            /* 循环多任务的累加 */
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceEntriesToDoubleTask<K,V>
                    t = (MapReduceEntriesToDoubleTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    t.result = reducer.applyAsDouble(t.result, s.result);
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.45 MapReduceMappingsToDoubleTask

@SuppressWarnings("serial")
static final class MapReduceMappingsToDoubleTask<K,V>
    extends BulkTask<K,V,Double> {
    final ToDoubleBiFunction<? super K, ? super V> transformer;
    final DoubleBinaryOperator reducer;
    final double basis;
    double result;
    MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
    /* 构造函数:初始化属性值 */
    MapReduceMappingsToDoubleTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceMappingsToDoubleTask<K,V> nextRight,
         ToDoubleBiFunction<? super K, ? super V> transformer,
         double basis,
         DoubleBinaryOperator reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.basis = basis; this.reducer = reducer;
    }
    
    public final Double getRawResult() { return result; }
    public final void compute() {
        final ToDoubleBiFunction<? super K, ? super V> transformer;
        final DoubleBinaryOperator reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            double r = this.basis;
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceMappingsToDoubleTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, r, reducer)).fork();
            }
            for (Node<K,V> p; (p = advance()) != null; )
                r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceMappingsToDoubleTask<K,V>
                    t = (MapReduceMappingsToDoubleTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    t.result = reducer.applyAsDouble(t.result, s.result);
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.46 ToLong、ToInt

  • ToLong、ToInt 的任务类与 ToDouble 基本一致,只是类型不同,这里不做赘述。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页