Java8-Stream-No.12
2024-10-06 17:28:31
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
public class Streams12 {
public static void main(String[] args) {
List<String> strings = Arrays.asList("a1", "a2", "b1", "c2", "c1");
// test1();
// test2(strings);
test3(strings);
// test4();
}
private static void test4() {
List<String> values = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
UUID uuid = UUID.randomUUID();
values.add(uuid.toString());
}
// sequential
long t0 = System.nanoTime();
long count = values
.parallelStream()
.sorted((s1, s2) -> {
System.out.format("sort: %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName());
return s1.compareTo(s2);
})
.count();
System.out.println(count);
long t1 = System.nanoTime();
long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0);
System.out.println(String.format("parallel sort took: %d ms", millis));
}
private static void test3(List<String> strings) {
strings
.parallelStream()
.filter(s -> {
System.out.format("filter: %s [%s]\n", s, Thread.currentThread().getName());
return true;
})
.map(s -> {
System.out.format("map: %s [%s]\n", s, Thread.currentThread().getName());
return s.toUpperCase();
})
.sorted((s1, s2) -> {
System.out.format("sort: %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName());
return s1.compareTo(s2);
})
.forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));
}
private static void test2(List<String> strings) {
strings
.parallelStream()
.filter(s -> {
System.out.format("filter: %s [%s]\n", s, Thread.currentThread().getName());
return true;
})
.map(s -> {
System.out.format("map: %s [%s]\n", s, Thread.currentThread().getName());
return s.toUpperCase();
})
.forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));
}
private static void test1() {
// -Djava.util.concurrent.ForkJoinPool.common.parallelism=5
ForkJoinPool commonPool = ForkJoinPool.commonPool();
System.out.println(commonPool.getParallelism());
}
}
最新文章
- CMS系统存储路径
- 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)
- Android中使用Gson解析JSON数据的两种方法
- 腾讯数据总监:运营人员必须掌握的APP基础数据分析体系(没有比这篇更系统全面的)
- [LeetCode]题解(python):082 - Remove Duplicates from Sorted List II
- noip知识点总结之--贪心
- nethogs 实时查看进程使用流量情况。
- String 两种实例化方式的区别
- UVa 10132 - File Fragmentation
- win10如何快速扫描-上海IT外包
- OJ:自己实现一个简单的 priority_queue
- java生成UUID
- Unity3D动态生成多边形
- 计算apk包的安装之后占用空间以及运行时占用内存
- JVM调优——之CMS 常见参数解析
- jeecg 主-附表生成代码例子
- Call to undefined function think\finfo_open()
- Linux下通过管道杀死所有与tomcat相关的进程
- 初识DOM
- linux添加磁盘空间
热门文章
- [Agc028B]Removing Blocks_排列组合
- Python if __name__ == &#39;__main__&#39;: 理解
- Nginx03---重装
- 数据库(mysql和oracle)
- github骚操作
- Cache的一些总结
- php--正则(手机号码)
- VisualVM的使用
- 忘记root密码,修改方法
- CentOS开机启动进度条卡死问题