|
| 1 | +# Introduction |
| 2 | + |
| 3 | +There are multiple ways to solve the Parallel Letter Frequency problem. |
| 4 | +One approach is to use [`Stream.parallelStream`][stream], and another involves using [`ForkJoinPool`][ForkJoinPool]. |
| 5 | + |
| 6 | +## General guidance |
| 7 | + |
| 8 | +To count occurrences of items, a map data structure is often used, though arrays and lists can work as well. |
| 9 | +A [`map`][map], being a key-value pair structure, is suitable for recording frequency by incrementing the value for each key. |
| 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. |
| 11 | + |
| 12 | +Parallel processing typically takes place in a multi-[`thread`][thread] environment. |
| 13 | +The Java 8 [`stream`][stream] API provides methods that make parallel processing easier, including the [`parallelStream()`][stream] method. |
| 14 | +With [`parallelStream()`][stream], 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. |
| 15 | + |
| 16 | +The [`ForkJoinPool`][ForkJoinPool] class, optimized for dividing and managing tasks, makes parallel processing efficient. |
| 17 | +However, [`parallelStream()`][stream] uses the common [`ForkJoinPool`][ForkJoinPool] by default, meaning multiple [`parallelStream`][stream] instances share the same thread pool unless configured otherwise. |
| 18 | + |
| 19 | +As a result, parallel streams may interfere with each other when sharing this thread pool, potentially affecting performance. |
| 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. |
| 21 | +Therefore, a custom [`ForkJoinPool`][ForkJoinPool] approach is also provided below. |
| 22 | + |
| 23 | +## Approach: `parallelStream` |
| 24 | + |
| 25 | +```java |
| 26 | +import java.util.Map; |
| 27 | +import java.util.List; |
| 28 | +import java.util.concurrent.ConcurrentMap; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | + |
| 31 | +class ParallelLetterFrequency { |
| 32 | + |
| 33 | + List<String> texts; |
| 34 | + ConcurrentMap<Character, Integer> letterCount; |
| 35 | + |
| 36 | + ParallelLetterFrequency(String[] texts) { |
| 37 | + this.texts = List.of(texts); |
| 38 | + letterCount = new ConcurrentHashMap<>(); |
| 39 | + } |
| 40 | + |
| 41 | + Map<Character, Integer> countLetters() { |
| 42 | + if (!letterCount.isEmpty() || texts.isEmpty()) { |
| 43 | + return letterCount; |
| 44 | + } |
| 45 | + texts.parallelStream().forEach(text -> { |
| 46 | + for (char c: text.toLowerCase().toCharArray()) { |
| 47 | + if (Character.isAlphabetic(c)) { |
| 48 | + letterCount.merge(c, 1, Integer::sum); |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + return letterCount; |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +For more information, check the [`parallelStream` approach][approach-parallel-stream]. |
| 59 | + |
| 60 | +## Approach: `Fork/Join` |
| 61 | + |
| 62 | +```java |
| 63 | +import java.util.Map; |
| 64 | +import java.util.List; |
| 65 | +import java.util.concurrent.ConcurrentMap; |
| 66 | +import java.util.concurrent.ConcurrentHashMap; |
| 67 | +import java.util.concurrent.ForkJoinPool; |
| 68 | +import java.util.concurrent.RecursiveTask; |
| 69 | + |
| 70 | +class ParallelLetterFrequency { |
| 71 | + |
| 72 | + List<String> texts; |
| 73 | + ConcurrentMap<Character, Integer> letterCount; |
| 74 | + |
| 75 | + ParallelLetterFrequency(String[] texts) { |
| 76 | + this.texts = List.of(texts); |
| 77 | + letterCount = new ConcurrentHashMap<>(); |
| 78 | + } |
| 79 | + |
| 80 | + Map<Character, Integer> countLetters() { |
| 81 | + if (!letterCount.isEmpty() || texts.isEmpty()) { |
| 82 | + return letterCount; |
| 83 | + } |
| 84 | + |
| 85 | + ForkJoinPool forkJoinPool = new ForkJoinPool(); |
| 86 | + forkJoinPool.invoke(new LetterCountTask(texts, 0, texts.size(), letterCount)); |
| 87 | + forkJoinPool.shutdown(); |
| 88 | + |
| 89 | + return letterCount; |
| 90 | + } |
| 91 | + |
| 92 | + private static class LetterCountTask extends RecursiveTask<Void> { |
| 93 | + private static final int THRESHOLD = 10; |
| 94 | + private final List<String> texts; |
| 95 | + private final int start; |
| 96 | + private final int end; |
| 97 | + private final ConcurrentMap<Character, Integer> letterCount; |
| 98 | + |
| 99 | + LetterCountTask(List<String> texts, int start, int end, ConcurrentMap<Character, Integer> letterCount) { |
| 100 | + this.texts = texts; |
| 101 | + this.start = start; |
| 102 | + this.end = end; |
| 103 | + this.letterCount = letterCount; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + protected Void compute() { |
| 108 | + if (end - start <= THRESHOLD) { |
| 109 | + for (int i = start; i < end; i++) { |
| 110 | + for (char c : texts.get(i).toLowerCase().toCharArray()) { |
| 111 | + if (Character.isAlphabetic(c)) { |
| 112 | + letterCount.merge(c, 1, Integer::sum); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } else { |
| 117 | + int mid = (start + end) / 2; |
| 118 | + LetterCountTask leftTask = new LetterCountTask(texts, start, mid, letterCount); |
| 119 | + LetterCountTask rightTask = new LetterCountTask(texts, mid, end, letterCount); |
| 120 | + invokeAll(leftTask, rightTask); |
| 121 | + } |
| 122 | + return null; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +``` |
| 128 | + |
| 129 | +For more information, check the [`fork/join` approach][approach-fork-join]. |
| 130 | + |
| 131 | +## Which approach to use? |
| 132 | + |
| 133 | +When tasks are simple or do not require a dedicated thread pool (such as in this case), the [`parallelStream`][stream] 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. |
| 135 | + |
| 136 | +[thread]: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html |
| 137 | +[stream]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html |
| 138 | +[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html |
| 139 | +[map]: https://docs.oracle.com/javase/8/docs/api/?java/util/Map.html |
| 140 | +[list]: https://docs.oracle.com/javase/8/docs/api/?java/util/List.html |
| 141 | +[approach-parallel-stream]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/parallel-stream |
| 142 | +[approach-fork-join]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/fork-join |
0 commit comments