|
1 | 1 | # Introduction |
2 | 2 |
|
3 | 3 | There are multiple ways to solve the Parallel Letter Frequency problem. |
4 | | - |
5 | 4 | One approach is to use [`Stream.parallelStream`][stream], and another involves using [`ForkJoinPool`][ForkJoinPool]. |
6 | 5 |
|
7 | 6 | ## General guidance |
8 | 7 |
|
9 | 8 | To count occurrences of items, a map data structure is often used, though arrays and lists can work as well. |
10 | | - |
11 | 9 | A [`map`][map], being a key-value pair structure, is suitable for recording frequency by incrementing the value for each key. |
12 | | - |
13 | 10 | 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 | 11 |
|
15 | 12 | Parallel processing typically takes place in a multi-[`thread`][thread] environment. |
16 | | - |
17 | | -The Java 8 [`stream`][stream] API provides methods that make parallel processing easier, including the parallelStream() method. |
18 | | - |
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. |
| 13 | +The Java 8 [`stream`][stream] API provides methods that make parallel processing easier, including the `parallelStream()` method. |
| 14 | +With `parallelStream()`, developers can use the [`ForkJoinPool`][ForkJoinPool] model for workload division and parallel execution, without the need to manually manage threads or create custom thread pools. |
20 | 15 |
|
21 | 16 | The [`ForkJoinPool`][ForkJoinPool] class, optimized for dividing and managing tasks, makes parallel processing efficient. |
22 | | - |
23 | | -However, parallelStream() uses the common ForkJoinPool by default, meaning multiple parallelStream instances share the same thread pool unless configured otherwise. |
| 17 | +However, `parallelStream()` uses the common [`ForkJoinPool`][ForkJoinPool] by default, meaning multiple `parallelStream` instances share the same thread pool unless configured otherwise. |
24 | 18 |
|
25 | 19 | As a result, parallel streams may interfere with each other when sharing this thread pool, potentially affecting performance. |
26 | | - |
27 | 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. |
28 | | - |
29 | | -Therefore, a custom ForkJoinPool approach is also provided below. |
| 21 | +Therefore, a custom [`ForkJoinPool`][ForkJoinPool] approach is also provided below. |
30 | 22 |
|
31 | 23 | ## Approach: `parallelStream` |
32 | 24 |
|
@@ -138,8 +130,8 @@ For more information, check the [`fork/join` approach][approach-fork-join]. |
138 | 130 |
|
139 | 131 | ## Which approach to use? |
140 | 132 |
|
141 | | -When tasks are simple or do not require a dedicated thread pool (such as in this case), the parallelStream approach is recommended. |
142 | | -However, if the work is complex or there is a need to isolate thread pools from other concurrent tasks, the ForkJoinPool approach is preferable. |
| 133 | +When tasks are simple or do not require a dedicated thread pool (such as in this case), the `parallelStream` approach is recommended. |
| 134 | +However, if the work is complex or there is a need to isolate thread pools from other concurrent tasks, the [`ForkJoinPool`][ForkJoinPool] approach is preferable. |
143 | 135 |
|
144 | 136 | [thread]: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html |
145 | 137 | [stream]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html |
|
0 commit comments