Make a Java, program with two processes, a producer and a consumer.
The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random wait of from one to three seconds (compute a new random wait value on each pass through the loop). The loop is to be executed five times.
The consumer process consists of a loop that reads the variable it shares with the producer five times and computes the sum of the values it has read. On each pass through the loop before it reads the shared variable, it does a random wait of from one to three seconds (compute a new random value on each pass through the loop). When the loop finishes, the program is to write the sum into a file.
You must run the program twice. Note that the output from the runs will almost certainly be different if you have written your program correctly. (Mine is only output the same number all time)
This program must output to a data file like "output.txt"
The contents of the output file should look something like this (where ### is your computed sum).
Jack Smith
Assignment X
The sum is ###
Hey, please follow below, i have added comments where ever required.
App.java
import java.util.Random;
public class App {
private int count;
private int sum;
public static void main(String[] args) {
App app = new App();
Thread producer = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 4; i++) {
// lock will be attained by producer first
synchronized (this) {
app.count = 100;
int nextWait = new Random().nextInt(4) + 1;
try {
wait(nextWait * 1000); // releases the lock
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread consumer = new Thread(new Runnable() {
@Override
public void run() {
// adding sleep of 100 milli seconds as we want the lock to be
// acquired by the producer thread first
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for (int i = 0; i <= 4; i++) {
synchronized (this) {
app.sum += app.count; // calculates the sum
notify(); // notifies waiting producer thread
} // releases the lock at this point
}
}
});
producer.start();
consumer.start();
// stopping the main thread, as we want the producer and consumer thread
// to complete their work
try {
producer.join();
consumer.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Sum is : " + app.sum);
}
}

Thanks !!!
Make a Java, program with two processes, a producer and a consumer. The producer process consists...
Java Programming: Make a Java program with two processes, a producer and a consumer. If you want to use another language, clear it with me first. The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random...
Producer and Consumer Code in C++ The program will contain two methods- one each for the producer and consumer. Both methods share access to an integer array buffer of size 50 with all values initialized to 0 at the beginning and an integer variable counter initialized to 0. As given below, the producer accesses the buffer elements and updates the element to 1 ( indicating production). The consumer changes a buffer elements to 0 (indicating consumption). Enhancements and modifications: You...
*Help Please with the code**
CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and shared memory program for producer and consumer as shown in the following. You can start with the code provided in the virtual machine in the virtual box you installed. The code can be found in /home/oscreader/osc9e-src/ch3 a. For 3.21, you can start with the newprocposix.c and modify the code to meet your requirement. Then type: gcc neypCOCROSİS.c to...
C++
Write a program based on 8_lb that reads in the integer data from "input.txt" into a vector. Please prompt for the file name and append the "txt". Then create another vector where each element is the original vector times 10. Create a variable total and sum up the numbers from the second vector. The total should be 17510. Then write the data from the original vector and the 10 times vector to a file. See output.txt for the format....
Write a script that works in vim: We are given a Java program RandomTest.java that generates specified number of random integers in the range [0...99]. The program prints FAIL if either 0 or 99 is generated (along with a count of how many times). Otherwise it prints PASS. The program takes the number of random integers to generate as a command line argument as shown below. Note that the given results each time is is run. [an@localhost ~] java Random...
Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum. It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...
This part requires you to write two programs, compile them, and execute them - just like our lab questions. You may access these questions as many times as you need to and you have the entire duration of the test to complete these problems. You must submit the program code and the sample outputs for each problem - just like lab. These questions are worth 6 points each. 1. C++ Write a program that reads 20 data values from a data...
please put the comment each line, make sure i will have output too. write a program, Summarize (Summarize.java), containing the main() method, that first writes 10,000 random positive double type numbers, to the full accuracy of the number (15/16 decimal places), to a text file named DataValues.txt, one number per line. The program must then close that file, and reopen it for reading. Read back the values from the file and write the following information to a file named Summary.txt:...
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...
Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following requirement. You and your partner will be writing a simple Java program that implements some basic file I/O operations. You can use this code as the basis for some of your next project. FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each...