Skip to content

Commit a0ca392

Browse files
committed
Add approaches for Parallel-Letter-Frequency
1 parent 1338484 commit a0ca392

File tree

6 files changed

+318
-0
lines changed

6 files changed

+318
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"introduction": {
3+
"authors": [
4+
"masiljangajji"
5+
]
6+
},
7+
"approaches": [
8+
{
9+
"uuid": "dee2a79d-3e64-4220-b99f-55667549c12c",
10+
"slug": "fork-join",
11+
"title": "Fork/Join",
12+
"blurb": "Parallel Computation Using Fork/Join",
13+
"authors": [
14+
"masiljangajji"
15+
]
16+
},
17+
{
18+
"uuid": "75e9e93b-4da4-4474-8b6e-3c0cb9b3a9bb",
19+
"slug": "parallel-stream",
20+
"title": "Parallel Stream",
21+
"blurb": "Parallel Computation Using Parallel Stream",
22+
"authors": [
23+
"masiljangajji"
24+
]
25+
}
26+
]
27+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# `Fork/Join`
2+
3+
```java
4+
import java.util.Map;
5+
import java.util.List;
6+
import java.util.concurrent.ConcurrentMap;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.ForkJoinPool;
9+
import java.util.concurrent.RecursiveTask;
10+
11+
class ParallelLetterFrequency {
12+
13+
List<String> texts;
14+
ConcurrentMap<Character, Integer> letterCount;
15+
16+
ParallelLetterFrequency(String[] texts) {
17+
this.texts = List.of(texts);
18+
letterCount = new ConcurrentHashMap<>();
19+
}
20+
21+
Map<Character, Integer> countLetters() {
22+
if (texts.isEmpty()) {
23+
return letterCount;
24+
}
25+
26+
ForkJoinPool forkJoinPool = new ForkJoinPool();
27+
forkJoinPool.invoke(new LetterCountTask(texts, 0, texts.size(), letterCount));
28+
forkJoinPool.shutdown();
29+
30+
return letterCount;
31+
}
32+
33+
private static class LetterCountTask extends RecursiveTask<Void> {
34+
private static final int THRESHOLD = 10;
35+
private final List<String> texts;
36+
private final int start;
37+
private final int end;
38+
private final ConcurrentMap<Character, Integer> letterCount;
39+
40+
LetterCountTask(List<String> texts, int start, int end, ConcurrentMap<Character, Integer> letterCount) {
41+
this.texts = texts;
42+
this.start = start;
43+
this.end = end;
44+
this.letterCount = letterCount;
45+
}
46+
47+
@Override
48+
protected Void compute() {
49+
if (end - start <= THRESHOLD) {
50+
for (int i = start; i < end; i++) {
51+
for (char c : texts.get(i).toLowerCase().toCharArray()) {
52+
if (Character.isAlphabetic(c)) {
53+
letterCount.merge(c, 1, Integer::sum);
54+
}
55+
}
56+
}
57+
} else {
58+
int mid = (start + end) / 2;
59+
LetterCountTask leftTask = new LetterCountTask(texts, start, mid, letterCount);
60+
LetterCountTask rightTask = new LetterCountTask(texts, mid, end, letterCount);
61+
invokeAll(leftTask, rightTask);
62+
}
63+
return null;
64+
}
65+
}
66+
}
67+
```
68+
69+
Using [`ConcurrentHashMap`][ConcurrentHashMap] ensures that frequency counting and updates are safely handled in a parallel environment.
70+
71+
If there are no strings, a validation step prevents unnecessary processing.
72+
73+
A [`ForkJoinPool`][ForkJoinPool] is then created. The core of [`ForkJoinPool`][ForkJoinPool] is the Fork/Join mechanism, which divides tasks into smaller units and processes them in parallel.
74+
75+
THRESHOLD is the criterion for task division. If the range of texts exceeds the THRESHOLD, the task is divided into two subtasks, and [`invokeAll`][invokeAll](leftTask, rightTask) is called to execute both tasks in parallel.
76+
77+
Each subtask in LetterCountTask will continue calling compute() to divide itself further until the range is smaller than or equal to the threshold.
78+
79+
For tasks that are within the threshold, letter frequency is calculated. The [`isAlphabetic`][isAlphabetic] method is used to identify all characters classified as alphabetic in Unicode, covering various languages like English, Korean, Japanese, Chinese, etc., returning true for alphabetic characters and false for numbers, special characters, spaces, and others.
80+
81+
Additionally, since uppercase and lowercase letters are treated as the same character (e.g., A and a), each character is converted to lowercase.
82+
83+
After updating letter frequencies, the final map is returned.
84+
85+
86+
87+
[ConcurrentHashMap]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html
88+
[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html
89+
[isAlphabetic]: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int-
90+
[invokeAll]: https://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
for (int i = start; i < end; i++) {
2+
for (char c : texts.get(i).toLowerCase().toCharArray()) {
3+
if (Character.isAlphabetic(c)) {
4+
letterCount.merge(c, 1, Integer::sum);
5+
}
6+
}
7+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Introduction
2+
3+
There are multiple ways to solve the Parallel Letter Frequency problem.
4+
One approach is to use parallelStream, and another involves using 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. A map, being a key-value pair structure, is suitable for recording frequency by incrementing the value for each key.
9+
10+
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.
11+
12+
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.
13+
14+
The [`ForkJoinPool`][ForkJoinPool] class, optimized for dividing and managing tasks, makes parallel processing efficient.
15+
16+
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.
17+
18+
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.
19+
20+
21+
## Approach: `parallelStream`
22+
23+
```java
24+
import java.util.Map;
25+
import java.util.List;
26+
import java.util.concurrent.ConcurrentMap;
27+
import java.util.concurrent.ConcurrentHashMap;
28+
29+
class ParallelLetterFrequency {
30+
31+
List<String> texts;
32+
ConcurrentMap<Character, Integer> letterCount;
33+
34+
ParallelLetterFrequency(String[] texts) {
35+
this.texts = List.of(texts);
36+
letterCount = new ConcurrentHashMap<>();
37+
}
38+
39+
Map<Character, Integer> countLetters() {
40+
if (!letterCount.isEmpty() || texts.isEmpty()) {
41+
return letterCount;
42+
}
43+
texts.parallelStream().forEach(text -> {
44+
for (char c: text.toLowerCase().toCharArray()) {
45+
if (Character.isAlphabetic(c)) {
46+
letterCount.merge(c, 1, Integer::sum);
47+
}
48+
}
49+
});
50+
return letterCount;
51+
}
52+
53+
}
54+
```
55+
56+
For more information, check the [`parallelStream` approach][approach-parallel-stream].
57+
58+
## Approach: `Fork/Join`
59+
60+
```java
61+
import java.util.Map;
62+
import java.util.List;
63+
import java.util.concurrent.ConcurrentMap;
64+
import java.util.concurrent.ConcurrentHashMap;
65+
import java.util.concurrent.ForkJoinPool;
66+
import java.util.concurrent.RecursiveTask;
67+
68+
class ParallelLetterFrequency {
69+
70+
List<String> texts;
71+
ConcurrentMap<Character, Integer> letterCount;
72+
73+
ParallelLetterFrequency(String[] texts) {
74+
this.texts = List.of(texts);
75+
letterCount = new ConcurrentHashMap<>();
76+
}
77+
78+
Map<Character, Integer> countLetters() {
79+
if (!letterCount.isEmpty() || texts.isEmpty()) {
80+
return letterCount;
81+
}
82+
83+
ForkJoinPool forkJoinPool = new ForkJoinPool();
84+
forkJoinPool.invoke(new LetterCountTask(texts, 0, texts.size(), letterCount));
85+
forkJoinPool.shutdown();
86+
87+
return letterCount;
88+
}
89+
90+
private static class LetterCountTask extends RecursiveTask<Void> {
91+
private static final int THRESHOLD = 10;
92+
private final List<String> texts;
93+
private final int start;
94+
private final int end;
95+
private final ConcurrentMap<Character, Integer> letterCount;
96+
97+
LetterCountTask(List<String> texts, int start, int end, ConcurrentMap<Character, Integer> letterCount) {
98+
this.texts = texts;
99+
this.start = start;
100+
this.end = end;
101+
this.letterCount = letterCount;
102+
}
103+
104+
@Override
105+
protected Void compute() {
106+
if (end - start <= THRESHOLD) {
107+
for (int i = start; i < end; i++) {
108+
for (char c : texts.get(i).toLowerCase().toCharArray()) {
109+
if (Character.isAlphabetic(c)) {
110+
letterCount.merge(c, 1, Integer::sum);
111+
}
112+
}
113+
}
114+
} else {
115+
int mid = (start + end) / 2;
116+
LetterCountTask leftTask = new LetterCountTask(texts, start, mid, letterCount);
117+
LetterCountTask rightTask = new LetterCountTask(texts, mid, end, letterCount);
118+
invokeAll(leftTask, rightTask);
119+
}
120+
return null;
121+
}
122+
}
123+
}
124+
125+
```
126+
127+
For more information, check the [`fork/join` approach][approach-fork-join].
128+
129+
## Which approach to use?
130+
131+
When tasks are simple or do not require a dedicated thread pool (such as in this case), the parallelStream approach is recommended.
132+
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+
134+
[thread]: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
135+
[stream]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
136+
[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html
137+
[approach-parallel-stream]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/parallel-stream
138+
[approach-fork-join]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/fork-join
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# `parallelStream`
2+
3+
```java
4+
import java.util.Map;
5+
import java.util.List;
6+
import java.util.concurrent.ConcurrentMap;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
9+
class ParallelLetterFrequency {
10+
11+
List<String> texts;
12+
ConcurrentMap<Character, Integer> letterCount;
13+
14+
ParallelLetterFrequency(String[] texts) {
15+
this.texts = List.of(texts);
16+
letterCount = new ConcurrentHashMap<>();
17+
}
18+
19+
Map<Character, Integer> countLetters() {
20+
if (texts.isEmpty()) {
21+
return letterCount;
22+
}
23+
texts.parallelStream().forEach(text -> {
24+
for (char c: text.toLowerCase().toCharArray()) {
25+
if (Character.isAlphabetic(c)) {
26+
letterCount.merge(c, 1, Integer::sum);
27+
}
28+
}
29+
});
30+
return letterCount;
31+
}
32+
33+
}
34+
```
35+
36+
Using [`ConcurrentHashMap`][ConcurrentHashMap] ensures that frequency counting and updates are safely handled in a parallel environment.
37+
38+
If there are no strings to process, a validation step avoids unnecessary computation.
39+
40+
To calculate letter frequency, a parallel stream is used. The [`isAlphabetic`][isAlphabetic] method identifies all characters classified as alphabetic in Unicode, covering characters from various languages like English, Korean, Japanese, Chinese, etc., returning true. Non-alphabetic characters, including numbers, special characters, and spaces, return false.
41+
42+
Since we treat uppercase and lowercase letters as the same character (e.g., A and a), characters are converted to lowercase.
43+
44+
After updating letter frequencies, the final map is returned.
45+
46+
47+
48+
[ConcurrentHashMap]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html
49+
[isAlphabetic]: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int-
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
texts.parallelStream().forEach(text -> {
2+
for (char c: text.toLowerCase().toCharArray()) {
3+
if (Character.isAlphabetic(c)) {
4+
letterCount.merge(c, 1, Integer::sum);
5+
}
6+
}
7+
});

0 commit comments

Comments
 (0)