Last active
November 17, 2017 00:47
-
-
Save bromne/99e6f1f8997e29fea0da7cd2e5dcddd7 to your computer and use it in GitHub Desktop.
ネストされた並列ストリームでは、ScalaのストリームとJavaのストリームで挙動が異なる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.stream.IntStream | |
object NestedParallelStream { | |
def main(args: Array[String])]: Unit = { | |
iterateScalaStream() | |
iterateJavaStream() | |
} | |
def iterateScalaStream(): Unit = { | |
Stream.range(0, 100) | |
.par | |
.flatMap(i => { | |
Thread.sleep(latency()) | |
System.out.println("map: " + i) | |
Stream.range(0 , 100) | |
.par | |
.map(j => i * 100 + j) | |
}) | |
.foreach(i => System.out.println(i)) | |
} | |
def iterateJavaStream(): Unit = { | |
IntStream.range(0, 100) | |
.parallel() | |
.flatMap(i => { | |
Thread.sleep(latency()) | |
System.out.println("map: " + i) | |
IntStream.range(0, 100) | |
.parallel() | |
.map(j => i * 100 + j) | |
}).forEach(i => System.out.println(i)) | |
} | |
def latency(): Long = (500 + (Math.random() * 1500)).longValue() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment