Question: (30%) Write a program that utilises 2 threads. a. One thread accepts user input, a series of positive integers. A negative integer is to be
(30%) Write a program that utilises 2 threads. a. One thread accepts user input, a series of positive integers. A negative integer is to be used as a sentinel value to exit. b. The second thread will sum the numbers as they are input, and on receipt of a sentinel value print the sum, and exit. For example: Input a number: 20 Current Total: 20 Input a number: 10 Current Total: 30 Input a number: -1 The sum of the input numbers is 30 The 2 threads must be running concurrently (at the same time). The sum must be calculated as each number is input. i.e. The numbers must not be stored and summed at the end. The number of inputs must not be artificially limited. i.e. If 1 000 000 numbers are entered, your program must be capable of performing the action. Note: You are not required to check for integer overflow. Any negative integer can be used as a sentinel value. i.e. Not a specific value, -7, -13, -9000000 are all valid sentinel values. The program must not use busy/wait. The program must not have a race condition. The program must use use one of the syncronisation tools discussed in the course.
1. (30%) Write a program that utilises 2 threads. a. One thread accepts user input, a series of positive integers. A negative integer is to be used as a sentinel value to exit. b. The second thread will sum the numbers as they are input, and on receipt of a sentinel value print the sum, and exit. For example: Input a number: 20 Current Total: 20 Input a number: 10 Current Total: 30 Input a number: -1 The sum of the input numbers is 30 The 2 threads must be running concurrently (at the same time). The sum must be calculated as each number is input. i.e. The numbers must not be stored and summed at the end. The number of inputs must not be artificially limited. i.e. If 1 000 000 numbers are entered, your program must be capable of performing the action. Note: You are not required to check for integer overflow. Any negative integer can be used as a sentinel value. i.e. Not a specific value, -7, -13,-9000000 are all valid sentinel values. The program must not use busy/wait. The program must not have a race condition. The program must use use one of the syncronisation tools discussed in the course
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
