-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseWordsMultithreaded.java
More file actions
41 lines (36 loc) · 1.33 KB
/
ReverseWordsMultithreaded.java
File metadata and controls
41 lines (36 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
class ReverseWordsMultithreaded {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a paragraph: ");
String paragraph = scanner.nextLine();
String reversedParagraph = reverseWords(paragraph);
System.out.println("Reversed paragraph: " + reversedParagraph);
}
public static String reverseWords(String paragraph) {
String[] words = paragraph.split("\\s+");
StringBuilder reversedParagraph = new StringBuilder();
Thread[] threads = new Thread[words.length];
for (int i = 0; i < words.length; i++) {
final int index = i;
threads[i] = new Thread(() -> {
reversedParagraph.append(reverseWord(words[index])).append(" ");
});
threads[i].start();
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return reversedParagraph.toString().trim();
}
public static String reverseWord(String word) {
if (word.length() == 0) {
return "";
}
return word.charAt(word.length() - 1) + reverseWord(word.substring(0, word.length() - 1));
}
}