Write a program that uses concurrent threads to count the occurrences of each un
ID: 657160 • Letter: W
Question
Write a program that uses concurrent threads to count the occurrences of each unique word in a text file. There will be two stages to this program. The first stage, which we will call the mapper stage, contains the most difficult elements of concurrency. This stage will technically consist of two parts, the reader and adder parts. There will be two arguments to this stage: n for the number of portions to split the text file into, and b for the size of the buffer that is handed from the reader to the adder. Therefore, there will be a total of n reader threads and n adder threads. reader[i] will create a (word, count) pair for each word (NOT each unique word) in the form of (word, 1). and add it to the buffer of size b. Then, adder[i] will sum the total occurrences of each word in the form of (word, total_count) and place this in a new, infinitely sized buffer that is shared with the second stage of the program. Note that the reader and adder function much like a producer-consumer relationship. Be careful to avoid overflowing the buffer of size b, or allowing adder[i] to consume from an empty buffer! The second stage of the program is the reducer stage. The reducer is a single thread which takes all occurrences of each unique word from each adder[i]'s buffer and reduces them to a single output file of (word, count) pairs.
For example, if the input file would be:
foo bar foo bar bar
Then the final output file should be of the form:
(foo, 2)
(bar, 3)
Explanation / Answer
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class WordCount { public static void main(String args[]) throws FileNotFoundException {
int i,j,check; String[] words= new String[1000];
int[] numbers=new int[1000];
for ( i=0;i<1000;i++){
words[i]=
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.