|
1 | 1 | # Introduction |
2 | 2 |
|
3 | 3 | There are multiple ways to solve the Parallel Letter Frequency problem. |
4 | | -One approach is to use parallelStream, and another involves using ForkJoinPool. |
| 4 | + |
| 5 | +One approach is to use [`Stream.parallelStream`][stream], and another involves using [`ForkJoinPool`][ForkJoinPool]. |
5 | 6 |
|
6 | 7 | ## General guidance |
7 | 8 |
|
8 | 9 | To count occurrences of items, a map data structure is often used, though arrays and lists can work as well. |
9 | 10 |
|
10 | | -A map, being a key-value pair structure, is suitable for recording frequency by incrementing the value for each key. |
| 11 | +A [`map`][map], being a key-value pair structure, is suitable for recording frequency by incrementing the value for each key. |
| 12 | + |
| 13 | +If the data being counted has a limited range (e.g., characters or integers), an `int[] array` or [`List<Integer>`][list] can be used to record frequencies. |
| 14 | + |
| 15 | +Parallel processing typically takes place in a multi-[`thread`][thread] environment. |
11 | 16 |
|
12 | | -If the data being counted has a limited range (e.g., characters or integers), an int[] array or List<Integer> can be used to record frequencies. |
| 17 | +The Java 8 [`stream`][stream] API provides methods that make parallel processing easier, including the parallelStream() method. |
13 | 18 |
|
14 | | -Parallel processing typically takes place in a multi-[`thread`][thread] environment. The Java 8 [`stream`][stream] API provides methods that make parallel processing easier, including the parallelStream() method. With parallelStream(), developers can use the ForkJoinPool model for workload division and parallel execution, without the need to manually manage threads or create custom thread pools. |
| 19 | +With parallelStream(), developers can use the ForkJoinPool model for workload division and parallel execution, without the need to manually manage threads or create custom thread pools. |
15 | 20 |
|
16 | 21 | The [`ForkJoinPool`][ForkJoinPool] class, optimized for dividing and managing tasks, makes parallel processing efficient. |
17 | 22 |
|
18 | | -However, parallelStream() uses the common ForkJoinPool by default, meaning multiple parallelStream instances share the same thread pool unless configured otherwise. As a result, parallel streams may interfere with each other when sharing this thread pool, potentially affecting performance. |
| 23 | +However, parallelStream() uses the common ForkJoinPool by default, meaning multiple parallelStream instances share the same thread pool unless configured otherwise. |
| 24 | + |
| 25 | +As a result, parallel streams may interfere with each other when sharing this thread pool, potentially affecting performance. |
| 26 | + |
| 27 | +Although this doesn’t directly impact solving the Parallel Letter Frequency problem, it may introduce issues when thread pool sharing causes conflicts in other applications. |
19 | 28 |
|
20 | | -Although this doesn’t directly impact solving the Parallel Letter Frequency problem, it may introduce issues when thread pool sharing causes conflicts in other applications. Therefore, a custom ForkJoinPool approach is also provided below. |
| 29 | +Therefore, a custom ForkJoinPool approach is also provided below. |
21 | 30 |
|
22 | 31 | ## Approach: `parallelStream` |
23 | 32 |
|
@@ -135,5 +144,7 @@ However, if the work is complex or there is a need to isolate thread pools from |
135 | 144 | [thread]: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html |
136 | 145 | [stream]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html |
137 | 146 | [ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html |
| 147 | +[map]: https://docs.oracle.com/javase/8/docs/api/?java/util/Map.html |
| 148 | +[list]: https://docs.oracle.com/javase/8/docs/api/?java/util/List.html |
138 | 149 | [approach-parallel-stream]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/parallel-stream |
139 | 150 | [approach-fork-join]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/fork-join |
0 commit comments