Created
June 12, 2014 05:37
-
-
Save komiya-atsushi/dc6fe65adb9fdfde9604 to your computer and use it in GitHub Desktop.
Stream#parallel() によるスレッド処理がどのようなものかを眺めてみる。
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; | |
/** | |
* parallel() によるスレッド処理がどのようなものかを眺めてみる。 | |
*/ | |
public class ParallelDemo { | |
public static void main(String[] args) { | |
IntStream.range(0, 10) | |
.parallel() | |
.map(v -> { | |
showThreadNameAndSleep("add +1"); | |
return v + 1; | |
}) | |
.mapToObj(v -> { | |
showThreadNameAndSleep("int to String"); | |
return String.valueOf(v); | |
}) | |
.map(s -> { | |
showThreadNameAndSleep("String to Double"); | |
return Double.valueOf(s); | |
}) | |
.forEach(d -> { | |
showThreadNameAndSleep("print Double value"); | |
System.out.println(d); | |
}); | |
} | |
static void showThreadNameAndSleep(String jobName) { | |
try { | |
String threadName = Thread.currentThread().getName(); | |
String message = String.format("threadName: '%s', jobName: '%s'", threadName, jobName); | |
System.out.println(message); | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
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
threadName: 'main', jobName: 'add +1' | |
threadName: 'ForkJoinPool.commonPool-worker-2', jobName: 'add +1' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'add +1' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'add +1' | |
threadName: 'main', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-2', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-2', jobName: 'String to Double' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'String to Double' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'String to Double' | |
threadName: 'main', jobName: 'String to Double' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'print Double value' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'print Double value' | |
threadName: 'ForkJoinPool.commonPool-worker-2', jobName: 'print Double value' | |
threadName: 'main', jobName: 'print Double value' | |
7.0 | |
3.0 | |
9.0 | |
threadName: 'main', jobName: 'add +1' | |
8.0 | |
threadName: 'ForkJoinPool.commonPool-worker-2', jobName: 'add +1' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'add +1' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'add +1' | |
threadName: 'ForkJoinPool.commonPool-worker-2', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'int to String' | |
threadName: 'main', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'String to Double' | |
threadName: 'main', jobName: 'String to Double' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'String to Double' | |
threadName: 'ForkJoinPool.commonPool-worker-2', jobName: 'String to Double' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'print Double value' | |
threadName: 'main', jobName: 'print Double value' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'print Double value' | |
threadName: 'ForkJoinPool.commonPool-worker-2', jobName: 'print Double value' | |
5.0 | |
2.0 | |
10.0 | |
6.0 | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'add +1' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'add +1' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'int to String' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'String to Double' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'String to Double' | |
threadName: 'ForkJoinPool.commonPool-worker-3', jobName: 'print Double value' | |
threadName: 'ForkJoinPool.commonPool-worker-1', jobName: 'print Double value' | |
1.0 | |
4.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment