Counting Vowels in a String in Java Using Multithreading
Multithreading is a powerful tool in programming that allows for the execution of multiple threads simultaneously. In Java, multithreading can be used to perform tasks in parallel, which can greatly improve the performance and efficiency of a program. One common use case for multithreading is counting the number of vowels in a string. In this article, we will explore how to use multithreading in Java to count the number of vowels in a string.
First, let's take a look at a simple example of counting the number of vowels in a string using a single thread. The following code snippet demonstrates this:
Nrml.java
class Nrml {
public static void main(String args[]) {
String input = "Hello, World!";
int count = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
System.out.println("Number of vowels: " + count);
}
}
Output:
Number of vowels: 5
This code iterates through the input string and checks each character to see if it is a vowel. If it is, the count variable is incremented. This approach works well for small strings, but as the size of the input string increases, the performance of this code can become an issue.
To improve the performance of this code, we can use multithreading to count the number of vowels in the string. The idea is to divide the input string into two parts and have each thread count the number of vowels in each part. The following code demonstrates this:
VowelCounter.java
public class VowelCounter {
public static void main(String[] args) {
String input = "Hello, World!";
int length = input.length();
int mid = length / 2;
Thread thread1 = new Thread(() -> {
int count = 0;
for (int i = 0; i < mid; i++) {
char c = input.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
System.out.println("Vowels in first half: " + count);
});
Thread thread2 = new Thread(() -> {
int count = 0;
for (int i = mid; i < length; i++) {
char c = input.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
System.out.println("Vowels in second half: " + count);
});
thread1.start();
thread2.start();
}
}
Output:
Vowels in first half: 3
Vowels in second half: 2
The output of the code snippet is not guaranteed, as it depends on the timing of when each thread completes its task. Each thread will print the number of vowels it has counted in its assigned section of the string, and the order in which they print may vary.
For example, the output might look like this:
Vowels in first half: 3
Vowels in second half: 2
or it could be vice versa
Vowels in second half: 2
Vowels in first half: 3
In this code, we create two threads - one for counting the vowels in the first half of the input string and one for counting the vowels in the second half of the input string. The start() method is called on each thread to begin execution. Once both threads have completed, we can add the two counts together to get the total number of vowels in the input string.
This approach has the potential to greatly improve the performance of counting vowels in a large string. However, it's important. t's also important to note that the above code does not provide a way to get the total number of vowels in the string, as the two threads are running concurrently and their execution is not synchronized. So, if you want to get the total number of vowels in the string, you need to wait for both threads to complete and then add the counts together.
You can use join() method to wait for a thread to finish its execution, or you can use ExecutorService or ForkJoinPool to manage the threads, or use Atomic Integer or locks to synchronize the threads.