Question: The second program you will write will be called Worker.java, again with no package. The Worker program will perform the following tasks in the main

The second program you will write will be called Worker.java, again with no package. The Worker program will perform the following tasks in the main method:

  1. Get the command line arguments for Worker: the number of threads to create and the filename to read, from the main method parameter and place them in variables.

  2. Open the file for reading and then, read and store the number in the file. You will probably find it easiest to use an ArrayList to store the numbers.

  3. Create the specified number of threads, using the Runnable class you write and create an double array with an element for each thread to store its results.

  4. Start the thread you created.

  5. Using the Thread join() method, wait for each of the thread to complete.

  6. Sum the values in the results array and display the sum of the numbers that were read.

You also have to write a class that implements the Runnable interface that the main method uses to create the threads. You should make this class an inner class of the Worker class. This will allow the Runnable class to have access to the static variables of the Worker class, thereby allows the running threads access to threse variables as shared data. The three pieces of shared data needed are the total number of threads, the list of numbers and the result array. The Runnable class should have a private instance variable which will hold the index of the thread object, which will be a number between 0 and the number of threads minus one. This index is assigned by the main method when it create the Runnable class for each thread. The index will be used by the thread object to write its final result to the proper element of the shared result array array and to partition the numbers in the numbers list.

The Runnable class should be written so that it does the following:

1/2

Prof. Pitts CS554AH1: Operating Systems Spring 2021

  1. Based on its index, the thread decides which of the numbers it will process. If the number of threads is T and the size of the numbers list is N , then thread i will process elements i through (i+1)NT 1 . For instance, if

    T is 4 and N is 39, then thread 0 will process elements 0 through 8, thread 1 will process elements 9 through 17, thread 2 will process elements 18 through 26, and thread 3 will process elements 28 through 39. Note that the last thread may process more elements than the others.

  2. Compute the sum of the numbers it is to process.

  3. Store the sum into the result array element using the thread index.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!