提交 379e137f 编写于 作者: oldratlee's avatar oldratlee 🔥

= improve ForkJoinPoolDemo and ParallelStreamDemo

上级 5f74ca5d
package com.alibaba.demo.forkjoinpool
import org.junit.Assert.fail
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.RecursiveTask
import java.util.concurrent.TimeUnit
/**
* ForkJoinPool use demo.
*
* @author Jerry Lee (oldratlee at gmail dot com)
*/
fun main() {
val pool = ForkJoinPool()
val result = pool.invoke(SumTask(1L..100000L))
println(result) // result is 5000050000
pool.shutdown()
if (!pool.awaitTermination(100, TimeUnit.MILLISECONDS)) fail("Fail to shutdown thread pool")
}
internal class SumTask(private val numbers: LongRange) : RecursiveTask<Long>() {
override fun compute(): Long =
if (numbers.count() <= 16) {
// compute directly
numbers.sum()
} else {
// split task
val middle = numbers.start + numbers.count() / 2
val taskLeft = SumTask(numbers.start until middle)
val taskRight = SumTask(middle..numbers.endInclusive)
taskLeft.fork()
taskRight.fork()
taskLeft.join() + taskRight.join()
}
}
package com.alibaba.demo.forkjoinpool
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.RecursiveTask
/**
* ForkJoinPool use demo.
*
* @author Jerry Lee (oldratlee at gmail dot com)
*/
fun main() {
val pool = ForkJoinPool.commonPool()
val result = pool.invoke(SumTask(1..1000))
println("computed result: $result") // result is 500500
}
internal class SumTask(private val numbers: IntRange, private val forkLevel: Int = 0) : RecursiveTask<Int>() {
override fun compute(): Int =
if (numbers.count() <= 16) {
println(String.format("direct compute %9s[%4s] at fork level %2s @ thread ${Thread.currentThread().name}",
numbers, numbers.count(), forkLevel))
// compute directly
numbers.sum()
} else {
println(String.format("fork compute %9s[%4s] at fork level %2s @ thread ${Thread.currentThread().name}",
numbers, numbers.count(), forkLevel))
// split task
val middle = numbers.first + numbers.count() / 2
val nextForkLevel = forkLevel + 1
val taskLeft = SumTask(numbers.first until middle, nextForkLevel)
val taskRight = SumTask(middle..numbers.last, nextForkLevel)
// fork-join compute
taskLeft.fork()
taskRight.fork()
taskLeft.join() + taskRight.join()
}
}
/*
Output:
fork compute 1..1000[1000] at fork level 0 @ thread main
fork compute 1..500[ 500] at fork level 1 @ thread ForkJoinPool.commonPool-worker-19
fork compute 501..1000[ 500] at fork level 1 @ thread ForkJoinPool.commonPool-worker-5
fork compute 501..750[ 250] at fork level 2 @ thread ForkJoinPool.commonPool-worker-23
fork compute 751..1000[ 250] at fork level 2 @ thread ForkJoinPool.commonPool-worker-13
fork compute 251..500[ 250] at fork level 2 @ thread ForkJoinPool.commonPool-worker-27
fork compute 501..625[ 125] at fork level 3 @ thread ForkJoinPool.commonPool-worker-17
fork compute 1..250[ 250] at fork level 2 @ thread ForkJoinPool.commonPool-worker-9
fork compute 751..875[ 125] at fork level 3 @ thread ForkJoinPool.commonPool-worker-13
fork compute 876..1000[ 125] at fork level 3 @ thread ForkJoinPool.commonPool-worker-3
fork compute 251..375[ 125] at fork level 3 @ thread ForkJoinPool.commonPool-worker-27
fork compute 376..500[ 125] at fork level 3 @ thread ForkJoinPool.commonPool-worker-7
fork compute 751..812[ 62] at fork level 4 @ thread ForkJoinPool.commonPool-worker-13
fork compute 1..125[ 125] at fork level 3 @ thread ForkJoinPool.commonPool-worker-9
fork compute 376..437[ 62] at fork level 4 @ thread ForkJoinPool.commonPool-worker-7
fork compute 876..937[ 62] at fork level 4 @ thread ForkJoinPool.commonPool-worker-3
fork compute 626..750[ 125] at fork level 3 @ thread ForkJoinPool.commonPool-worker-31
fork compute 376..406[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-7
fork compute 1..62[ 62] at fork level 4 @ thread ForkJoinPool.commonPool-worker-9
fork compute 751..781[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-13
fork compute 501..562[ 62] at fork level 4 @ thread ForkJoinPool.commonPool-worker-17
fork compute 251..312[ 62] at fork level 4 @ thread ForkJoinPool.commonPool-worker-27
fork compute 626..687[ 62] at fork level 4 @ thread ForkJoinPool.commonPool-worker-31
direct compute 751..765[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-13
fork compute 876..906[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-3
fork compute 563..625[ 63] at fork level 4 @ thread ForkJoinPool.commonPool-worker-21
fork compute 501..531[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-17
fork compute 1..31[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-9
direct compute 876..890[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
direct compute 376..390[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-7
fork compute 563..593[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-21
direct compute 1..15[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
direct compute 766..781[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-13
fork compute 626..656[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-31
direct compute 391..406[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-7
fork compute 251..281[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-27
direct compute 16..31[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
direct compute 563..577[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-21
direct compute 891..906[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
fork compute 407..437[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-7
fork compute 32..62[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-9
direct compute 578..593[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-21
direct compute 501..515[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-17
direct compute 407..421[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-7
fork compute 907..937[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-3
direct compute 251..265[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-27
direct compute 626..640[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-31
fork compute 782..812[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-13
direct compute 907..921[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
direct compute 266..281[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-27
direct compute 516..531[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-17
direct compute 422..437[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-7
fork compute 594..625[ 32] at fork level 5 @ thread ForkJoinPool.commonPool-worker-21
direct compute 922..937[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
direct compute 32..46[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
fork compute 532..562[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-17
fork compute 282..312[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-27
direct compute 782..796[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-13
direct compute 47..62[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
direct compute 641..656[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-31
direct compute 797..812[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-13
fork compute 63..125[ 63] at fork level 4 @ thread ForkJoinPool.commonPool-worker-9
direct compute 282..296[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-27
direct compute 532..546[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-17
fork compute 938..1000[ 63] at fork level 4 @ thread ForkJoinPool.commonPool-worker-3
fork compute 63..93[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-9
direct compute 594..609[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-21
fork compute 438..500[ 63] at fork level 4 @ thread ForkJoinPool.commonPool-worker-7
direct compute 547..562[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-17
direct compute 297..312[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-27
fork compute 813..875[ 63] at fork level 4 @ thread ForkJoinPool.commonPool-worker-13
fork compute 657..687[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-31
fork compute 438..468[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-7
fork compute 313..375[ 63] at fork level 4 @ thread ForkJoinPool.commonPool-worker-27
direct compute 610..625[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-21
direct compute 63..77[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
direct compute 438..452[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-7
fork compute 938..968[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-3
fork compute 344..375[ 32] at fork level 5 @ thread ForkJoinPool.commonPool-worker-21
direct compute 78..93[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
fork compute 688..750[ 63] at fork level 4 @ thread ForkJoinPool.commonPool-worker-17
fork compute 94..125[ 32] at fork level 5 @ thread ForkJoinPool.commonPool-worker-9
direct compute 938..952[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
fork compute 688..718[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-17
direct compute 94..109[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
fork compute 313..343[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-27
direct compute 657..671[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-31
fork compute 813..843[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-13
direct compute 110..125[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
direct compute 672..687[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-23
fork compute 719..750[ 32] at fork level 5 @ thread ForkJoinPool.commonPool-worker-31
direct compute 813..827[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-13
direct compute 688..702[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-17
direct compute 953..968[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
direct compute 453..468[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-7
direct compute 344..359[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-21
direct compute 703..718[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-17
direct compute 828..843[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-13
direct compute 719..734[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-31
fork compute 126..250[ 125] at fork level 3 @ thread ForkJoinPool.commonPool-worker-9
direct compute 313..327[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-27
fork compute 844..875[ 32] at fork level 5 @ thread ForkJoinPool.commonPool-worker-13
direct compute 735..750[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-17
direct compute 360..375[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-21
fork compute 469..500[ 32] at fork level 5 @ thread ForkJoinPool.commonPool-worker-7
fork compute 969..1000[ 32] at fork level 5 @ thread ForkJoinPool.commonPool-worker-3
direct compute 860..875[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-31
fork compute 188..250[ 63] at fork level 4 @ thread ForkJoinPool.commonPool-worker-17
direct compute 485..500[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-23
direct compute 969..984[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
direct compute 844..859[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-13
fork compute 126..187[ 62] at fork level 4 @ thread ForkJoinPool.commonPool-worker-9
direct compute 328..343[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-27
fork compute 219..250[ 32] at fork level 5 @ thread ForkJoinPool.commonPool-worker-23
fork compute 188..218[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-17
fork compute 126..156[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-9
direct compute 985..1000[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-31
direct compute 469..484[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-21
direct compute 188..202[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-17
direct compute 219..234[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-23
direct compute 203..218[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
fork compute 157..187[ 31] at fork level 5 @ thread ForkJoinPool.commonPool-worker-31
direct compute 126..140[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-9
direct compute 141..156[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-13
direct compute 235..250[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-5
direct compute 172..187[ 16] at fork level 6 @ thread ForkJoinPool.commonPool-worker-3
direct compute 157..171[ 15] at fork level 6 @ thread ForkJoinPool.commonPool-worker-31
computed result: 500500
*/
......@@ -2,6 +2,11 @@ package com.alibaba.demo.forkjoinpool
import java.util.concurrent.ConcurrentSkipListSet
/**
* Parallel Stream use demo.
*
* @author Jerry Lee (oldratlee at gmail dot com)
*/
fun main() {
println("availableProcessors: ${Runtime.getRuntime().availableProcessors()}")
......@@ -10,11 +15,137 @@ fun main() {
(0..100).toList().stream().parallel().mapToInt {
threadNames.add(Thread.currentThread().name)
Thread.sleep(10)
println("${Thread.currentThread().name}: $it")
println("map $it @ thread ${Thread.currentThread().name}")
it
}.sum().let {
println(it)
println("sum result: $it")
}
println("${threadNames.size}:\n$threadNames")
println(threadNames.joinToString(
separator = "\n\t",
prefix = "run threads(${threadNames.size}):\n\t"
))
}
/*
Output:
availableProcessors: 12
map 78 @ thread ForkJoinPool.commonPool-worker-7
map 76 @ thread ForkJoinPool.commonPool-worker-21
map 91 @ thread ForkJoinPool.commonPool-worker-19
map 71 @ thread ForkJoinPool.commonPool-worker-13
map 65 @ thread main
map 97 @ thread ForkJoinPool.commonPool-worker-3
map 32 @ thread ForkJoinPool.commonPool-worker-5
map 15 @ thread ForkJoinPool.commonPool-worker-9
map 79 @ thread ForkJoinPool.commonPool-worker-17
map 82 @ thread ForkJoinPool.commonPool-worker-27
map 53 @ thread ForkJoinPool.commonPool-worker-31
map 57 @ thread ForkJoinPool.commonPool-worker-23
map 77 @ thread ForkJoinPool.commonPool-worker-21
map 86 @ thread ForkJoinPool.commonPool-worker-7
map 72 @ thread ForkJoinPool.commonPool-worker-13
map 92 @ thread ForkJoinPool.commonPool-worker-19
map 66 @ thread main
map 83 @ thread ForkJoinPool.commonPool-worker-27
map 58 @ thread ForkJoinPool.commonPool-worker-23
map 54 @ thread ForkJoinPool.commonPool-worker-31
map 98 @ thread ForkJoinPool.commonPool-worker-3
map 33 @ thread ForkJoinPool.commonPool-worker-5
map 16 @ thread ForkJoinPool.commonPool-worker-9
map 80 @ thread ForkJoinPool.commonPool-worker-17
map 75 @ thread ForkJoinPool.commonPool-worker-21
map 87 @ thread ForkJoinPool.commonPool-worker-7
map 93 @ thread ForkJoinPool.commonPool-worker-19
map 73 @ thread ForkJoinPool.commonPool-worker-13
map 67 @ thread main
map 81 @ thread ForkJoinPool.commonPool-worker-27
map 56 @ thread ForkJoinPool.commonPool-worker-23
map 55 @ thread ForkJoinPool.commonPool-worker-31
map 17 @ thread ForkJoinPool.commonPool-worker-9
map 99 @ thread ForkJoinPool.commonPool-worker-3
map 31 @ thread ForkJoinPool.commonPool-worker-5
map 84 @ thread ForkJoinPool.commonPool-worker-17
map 63 @ thread ForkJoinPool.commonPool-worker-19
map 89 @ thread ForkJoinPool.commonPool-worker-7
map 74 @ thread ForkJoinPool.commonPool-worker-13
map 95 @ thread ForkJoinPool.commonPool-worker-21
map 62 @ thread ForkJoinPool.commonPool-worker-27
map 60 @ thread ForkJoinPool.commonPool-worker-23
map 100 @ thread ForkJoinPool.commonPool-worker-3
map 51 @ thread ForkJoinPool.commonPool-worker-31
map 13 @ thread ForkJoinPool.commonPool-worker-9
map 35 @ thread ForkJoinPool.commonPool-worker-5
map 85 @ thread ForkJoinPool.commonPool-worker-17
map 64 @ thread ForkJoinPool.commonPool-worker-19
map 90 @ thread ForkJoinPool.commonPool-worker-7
map 69 @ thread ForkJoinPool.commonPool-worker-13
map 96 @ thread ForkJoinPool.commonPool-worker-21
map 94 @ thread ForkJoinPool.commonPool-worker-27
map 61 @ thread ForkJoinPool.commonPool-worker-23
map 14 @ thread ForkJoinPool.commonPool-worker-9
map 36 @ thread ForkJoinPool.commonPool-worker-5
map 44 @ thread ForkJoinPool.commonPool-worker-3
map 52 @ thread ForkJoinPool.commonPool-worker-31
map 88 @ thread ForkJoinPool.commonPool-worker-17
map 68 @ thread ForkJoinPool.commonPool-worker-19
map 40 @ thread ForkJoinPool.commonPool-worker-7
map 70 @ thread ForkJoinPool.commonPool-worker-13
map 48 @ thread ForkJoinPool.commonPool-worker-21
map 46 @ thread ForkJoinPool.commonPool-worker-27
map 59 @ thread ForkJoinPool.commonPool-worker-23
map 12 @ thread ForkJoinPool.commonPool-worker-9
map 34 @ thread ForkJoinPool.commonPool-worker-5
map 50 @ thread ForkJoinPool.commonPool-worker-31
map 38 @ thread ForkJoinPool.commonPool-worker-17
map 45 @ thread ForkJoinPool.commonPool-worker-3
map 41 @ thread ForkJoinPool.commonPool-worker-7
map 43 @ thread ForkJoinPool.commonPool-worker-19
map 28 @ thread ForkJoinPool.commonPool-worker-13
map 49 @ thread ForkJoinPool.commonPool-worker-21
map 47 @ thread ForkJoinPool.commonPool-worker-27
map 7 @ thread ForkJoinPool.commonPool-worker-23
map 21 @ thread ForkJoinPool.commonPool-worker-9
map 25 @ thread ForkJoinPool.commonPool-worker-3
map 3 @ thread ForkJoinPool.commonPool-worker-31
map 26 @ thread ForkJoinPool.commonPool-worker-5
map 39 @ thread ForkJoinPool.commonPool-worker-17
map 42 @ thread ForkJoinPool.commonPool-worker-7
map 19 @ thread ForkJoinPool.commonPool-worker-19
map 29 @ thread ForkJoinPool.commonPool-worker-13
map 1 @ thread ForkJoinPool.commonPool-worker-21
map 0 @ thread ForkJoinPool.commonPool-worker-27
map 8 @ thread ForkJoinPool.commonPool-worker-23
map 27 @ thread ForkJoinPool.commonPool-worker-5
map 22 @ thread ForkJoinPool.commonPool-worker-9
map 37 @ thread ForkJoinPool.commonPool-worker-17
map 4 @ thread ForkJoinPool.commonPool-worker-3
map 10 @ thread ForkJoinPool.commonPool-worker-31
map 20 @ thread ForkJoinPool.commonPool-worker-19
map 18 @ thread ForkJoinPool.commonPool-worker-7
map 30 @ thread ForkJoinPool.commonPool-worker-13
map 2 @ thread ForkJoinPool.commonPool-worker-21
map 23 @ thread ForkJoinPool.commonPool-worker-23
map 6 @ thread ForkJoinPool.commonPool-worker-27
map 9 @ thread ForkJoinPool.commonPool-worker-5
map 5 @ thread ForkJoinPool.commonPool-worker-3
map 11 @ thread ForkJoinPool.commonPool-worker-31
map 24 @ thread ForkJoinPool.commonPool-worker-23
sum result: 5050
run threads(12):
ForkJoinPool.commonPool-worker-13
ForkJoinPool.commonPool-worker-17
ForkJoinPool.commonPool-worker-19
ForkJoinPool.commonPool-worker-21
ForkJoinPool.commonPool-worker-23
ForkJoinPool.commonPool-worker-27
ForkJoinPool.commonPool-worker-3
ForkJoinPool.commonPool-worker-31
ForkJoinPool.commonPool-worker-5
ForkJoinPool.commonPool-worker-7
ForkJoinPool.commonPool-worker-9
main
*/
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册