提交 95f6f70d 编写于 作者: oldratlee's avatar oldratlee 🔥

add ParallelStreamDemo

上级 bc46717b
......@@ -25,7 +25,7 @@ internal class SumTask(private val numbers: LongRange) : RecursiveTask<Long>() {
override fun compute(): Long =
if (numbers.count() <= 16) {
// compute directly
numbers.asSequence().sum()
numbers.sum()
} else {
// split task
val middle = numbers.start + numbers.count() / 2
......
package com.alibaba.demo.forkjoinpool
import java.util.concurrent.ConcurrentSkipListSet
fun main(args: Array<String>) {
println("availableProcessors: ${Runtime.getRuntime().availableProcessors()}")
val threadNames: MutableSet<String> = ConcurrentSkipListSet()
(0..100).toList().stream().parallel().mapToInt {
threadNames.add(Thread.currentThread().name)
Thread.sleep(10)
println("${Thread.currentThread().name}: $it")
it
}.sum().let {
println(it)
}
println("${threadNames.size}:\n$threadNames")
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册