In: Computer Science
Write a multithreaded program that calculates various
statistical values for a list of numbers. This program will be
passed a series of numbers on the command line and will then create
three separate worker threads. One thread will determine the
average of the numbers, the second
will determine the maximum value, and the third will determine the
minimum value. For example, suppose your program is passed the
integers
90 81 78 95 79 72 85 The program will report
The average value is 82 The minimum value is 72 The maximum value is 95
The variables representing the average, minimum, and maximum values will be stored globally. The worker threads will set these values, and the parent thread will output the values once the workers have exited. (We could obviously expand this program by creating additional threads
that determine other statistical values, such as median and standard deviation.) (You can use Java or C)
For this program, we will be coding in Java as java threading API is more easy to use than PThread API in C.
Program will ask user for number of elements, input each element and displays required statistics. For taking inputs, java.util.Scanner is used as it is very convinent to use
For creating threads, there are two methods in Java
Second method is used here, we create a anonymous class implementing Runnable. Doing so will help us to access global variables from threads functions.
In order to access a global variable from inner class, they have to be final. If they are final, they could not be modified. This is in case of primitive data types. In order to overcome this problem, we will be using final arrays, where we shall store the result.
After starting the threads, they have to be joined with the main thread in order for main thread to wait for them to complete execution. While doing so, the thread to be joined may throw an exception. They have to be catched and handled. Here we won't be handling the exception.
The logic for calculating statistics is easy to follow
-- code start --
import java.util.Scanner; public class Test { public static void main(String []args){ int n; int[] data; final int[] res = new int[2]; // res[0] holds min, res[1] holds max final double[] resf = new double[1]; // resf[0] holds avg Scanner s = new Scanner(System.in); System.out.print("Enter the number of elements :"); n = s.nextInt(); data = new int[n]; System.out.println("Enter the elements :"); for (int i = 0; i < n; i++) data[i] = s.nextInt(); Thread avgThread = new Thread(new Runnable() { @Override public void run() { double avg = 0; for (int i = 0; i < n ; i++) avg += data[i]; avg = avg / n; resf[0] = avg; } }); Thread minThread = new Thread(new Runnable() { @Override public void run() { int min = data[0]; for (int i = 1; i < n ; i++) if (min > data[i]) min = data[i]; res[0] = min; } }); Thread maxThread = new Thread(new Runnable() { @Override public void run() { int max = data[0]; for (int i = 1; i < n ; i++) if (max < data[i]) max = data[i]; res[1] = max; } }); avgThread.start(); minThread.start(); maxThread.start(); try { avgThread.join(); minThread.join(); maxThread.join(); } catch (Exception e) { } System.out.printf("Avarage of Data : %f\nMinimum of Data : %d\nMaximum of Data :%d\n", resf[0], res[0],res[1]); } }
-- code end --
Sample Output