Reference no: EM132411998
CS520 Assignment -
You are strongly encouraged to add comments into your program!
Given a large text file, the goal of this project is to count how many times each alphabet character appears in the text file. The text file will be converted to lowercase during the reading process. The program will create 26 threads. The contents of text file read is shared between the 26 threads. The first thread will count the number of a's, the second thread will count the number of b's, and so on.
All the threads also share a Results object. Each thread will contribute its own result to the shared Results objects.
Create a new Java Project in Eclipse named HW6_lastName and complete the following requirements based on the Threads.
Part 1 -
Create a package named cs520.hw6.part1. Using this package, create the following classes.
1. Create a class named ResultsEntry as follows:
a. The instance private variables - count (int) and target (char)
b. A single constructor with the two values
c. Public get methods for the two variables
d. Public toString method that returns a string in the format <target,count>
2. Create a class named SharedResults as follows:
a. A private instance variable - results (ArrayList of ResultsEntry type)
b. A default constructor that initializes the above data structure. c. A void addToResults method which takes the given ResultsEntry argument and adds it to the end of the shared results. This method then prints to the console the name of the current thread, the entry it added, and the shared results data structure. Handle the synchronization issue with this method.
d. The getResult method with no arguments which returns the sum of the count entry values in the shared results data structure. Handle the synchronization issue with this method.
3. Create a class named LongTask that extends the Thread class.
a. The instance (or member) private variables - sharedData (of type SharedResults), inputData (of type StringBuffer), and target (of type char)
b. A single constructor which takes the above three arguments and stores them in the instance values. Also, create a name for this thread as Thread_<target>
c. In the run method, use a loop to go over char chacacter in the inputData and count the number of occurrences of target. After the loop is done, create a ResultsEntry object with this count and the target character, and invoke the addToResults method of the shared results object.
4. Create a Test class to test the following functionality in its main method.
a. Using the P01_URLDemo as an example, read the contents (attached) into a StringBuffer object. After reading each line from the network, convert it to lower case and append to the StringBuffer. This will be the input data that is shared for each thread.
b. Create the SharedResults object and assign it to a variable.
c. Create 26 LongTask objects using an array of size 26. Each LongTask object is responsible to counting the occurrences of the characters 'a', 'b', 'c', ..., 'z', respectively. Start the respective thread after creating each one.
d. Wait for all the threads to complete using the join method.
e. Print the result from the shared object.
Part 2 -
Create a package named cs520.hw6.part2. Modify the above program using the wait/notifyAll features. When a thread tries to contribute its results to the shared data, and if it is not this thread's turn, it has to wait. When it is the thread's turn, its contributing result is added to the shared results and all other threads are notified. Thread_a will be first, Thread_b will be second, and so on. Suggested modifications to Part1 code are shown below.
1. SharedResults class
a. addToResults method takes two arguments, the calling thread's turn and the contributing result that needs to be added to the shared results. Implement the wait and notifyAll functionality in this method. Print to the console the thread's turn, the name of the current thread, the value it added, and the shared results data structure. Handle the synchronization issue with this method. Use the size of the data structure to determine if it is the calling thread's turn.
2. LongTask class
a. Add the turn (integer) instance variable
b. Modify the constructor to take care of this additional argument.
c. In the run method, after the loop of computation is done, invoke the addToResults method of the shared object and provide this thread's turn and its this accumulated ResultsEntry.
3. Test class
Provide the turn for each LongTask thread when invoking the constructor. Thread_a's turn is 0, Thread_b's turn is 1, and so on.
Attachment:- Assignment File.rar