Question

In: Computer Science

Write ALL POSSIBLE outputs from the following multithreaded program (10pts). Explain your reasons (10pts).     class...

Write ALL POSSIBLE outputs from the following multithreaded program (10pts). Explain your reasons (10pts).    

class Sum{

private int sum;
public int getSum(){

return sum;
}
public void setSum(int sum){

this.sum = sum;
}
}

class Summation implements Runnable{

private int start;
private int end;
private Sum sumValue;

public Summation(int start, int end, Sum sumValue) {

this.start = start;
this.end = end;
this.sumValue = sumValue;
}

public void run(){

int sum = 0;
for(int i=start; i<=end;i++)
sum += i;
sumValue.setSum(sum);
System.out.println(" sum from " + start + " to "+ end + " is " + sum);
}
}

public class Test{

public static void main(String[] args){
int sum_total=0;
int upper=10;
Sum sumObject1=new Sum();
Sum sumObject2=new Sum();

Thread thrd1=new Thread(new Summation(1,upper/2-1,sumObject1));
Thread thrd2=new Thread(new Summation(upper/2,upper,sumObject2));

thrd1.start();
thrd2.start();
try{

thrd1.join();
thrd2.join();
sum_total=sumObject1.getSum() + sumObject2.getSum();
System.out.println("the result is " + sum_total);
}catch (InterruptedException ie) {}
}
}

Solutions

Expert Solution

There are only two possible outputs as shown in the screenshot

either " sum from 1 to 4 " is printed first or "sum from 5 to 10".

Reason --------------------

This happen because of  Thread scheduler of JVM(java virtual machine) . If the first Thread get chance to print then "sum from 1 to 4" is printed first , if not then "sum form 5 to 10 " is printed first.


Related Solutions

(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell script to take a directory as an argument and display the contents of that directory (10pts) Write a shell script that takes any command as a parameter and displays help on that command (e.g. the result of the execution of man <command>). (10pts) Write a shell script that requires two file names as parameters and copies content of one file into another without asking...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
Write a multithreaded program that calculates various statistical values for a list of numbers. This program...
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...
Write a multithreaded program that tests your solution to HW#1. You will create several threads –...
Write a multithreaded program that tests your solution to HW#1. You will create several threads – for example, 100 – and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process’ termination). On UNIX and Linux systems, sleeping is...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Using the data in the following array write a program that finds and outputs the highest...
Using the data in the following array write a program that finds and outputs the highest value, the lowest value, the count of elements in the array, the average value in the array, and the mode - the value that appears most often. dataArray = {173.4, 873.7, 783.9. 000.0, -383.5, 229.5, -345.5, 645.5, 873.7, 591.2};
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number. Enter 3 and 9 solution: 3456789 987654 45678 8765 567 76 6
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number.
4. Write a multithreaded program that calculates various statistical values for a list of numbers. This...
4. 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 five separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, the third will determine the minimum value, fourth will determine the median value and the fifth will compute the std. deviation. For example, suppose your program is passed...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT