关于Characteristics.UNORDERED在Java 8中的行动书中的混淆
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了关于Characteristics.UNORDERED在Java 8中的行动书中的混淆,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1495字,纯文字阅读大概需要3分钟。
内容图文
java 8的作者在写这个类:
class ToListCollector<T> implements Collector<T, List<T>, List<T>> {
@Override
public Supplier<List<T>> supplier() {
return ArrayList::new;
}
@Override
public BiConsumer<List<T>, T> accumulator() {
return List::add;
}
@Override
public BinaryOperator<List<T>> combiner() {
return (l1, l2) -> {
l1.addAll(l2);
return l1;
};
}
@Override
public Function<List<T>, List<T>> finisher() {
return Function.identity();
}
@Override
public Set<Characteristics> characteristics() {
return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
}
}
然后他谈到了特征枚举中的不同值.然后他解释了为什么他写的这个收藏家是IDENTITY_FINISH和CONCURRENT而不是UNORDERED,他说:
The ToListCollector developed so far is IDENTITY_FINISH, because the List used to
accumulate the elements in the stream is already the expected final result and doesn’t need any
further transformation, but it isn’t UNORDERED because if you apply it to an ordered stream
you want this ordering to be preserved in the resulting List. Finally, it’s CONCURRENT, but
following what we just said, the stream will be processed in parallel only if its underlying data
source is unordered.
为什么只有当底层源无序时才会并行处理流?我认为它仍然会并行处理,但是combiner()必须保留顺序.这本书中的错误是什么?
我认为Brian Goetz在最后一个parahraph中非常清楚地讨论了this post中有序流的并行处理.
书中的页面是192 – 193.
解决方法:
这完全是错的.即使在此处添加CONCURRENT特性也是错误的,因为您需要供应商中的线程安全数据结构.
内容总结
以上是互联网集市为您收集整理的关于Characteristics.UNORDERED在Java 8中的行动书中的混淆全部内容,希望文章能够帮你解决关于Characteristics.UNORDERED在Java 8中的行动书中的混淆所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。