Create a console-based application that spawns two threads: a producer and a con
ID: 3855695 • Letter: C
Question
Create a console-based application that spawns two threads: a producer and a consumer. Use any programming language.
The producer should open an input file and repeatedly copy values to a circular buffer.
The consumer should open an output file and repeatedly copy values from the same circular buffer to the file.
For both the producer and consumer, a random number of bytes between 1 and n, inclusive, should be copied each iteration, where n is specified by the user. The number of bytes should be randomized each iteration.
If the producer is unable to write to the buffer (because it does not contain enough empty elements), or if the consumer is unable to read from the buffer (because it does not contain enough unread items), then it should proceed to the next iteration, choosing a new random number of bytes to copy. (Exception: Once the producer has already read the entire file, then the consumer does not have to continue generating random numbers until it gets an exact hit; instead, it should write the rest of the buffer to complete the copy.)
When the program completes, the output file should be an exact copy of the input file.
The syntax of the program should be as follows:
copyfile input output n m
where
copyfile is the name of your executable
input is the name of the input file
output is the name of the output file
n is the maximum number of bytes to copy in any given iteration (described above)
m is the size of the circular buffer, in bytes
Explanation / Answer
I have made the program using JAVA and most of the requirements are been fulfilled and made right for the use.The required code for the Producer Consumer problem is as follows.Hope you'll like it.
PCTesting.java
public class PCTesting {
public static void main(String[] args) {
Holes ch = new Holes();
Prod prod = new Prod(ch, 1);
Cons cons = new Cons(ch, 1);
prod.start();
cons.start();
}
}
Holes.java
class Holes {
private int cont;
private boolean ready = false;
public synchronized int get() {
while (ready == false) {
try {
wait();
} catch (InterruptedException e) {}
}
ready = false;
notifyAll();
return cont;
}
public synchronized void insert(int val) {
while (ready == true) {
try {
wait();
} catch (InterruptedException e) { }
}
cont = val;
ready = true;
notifyAll();
}
}
Cons.java
class Cons extends Thread {
private Holes hole;
private int no;
public Cons(Holes ch, int no) {
hole = ch;
this.no = no;
}
public void run() {
int val = 0;
for (int uu = 0; uu < 10; uu++) {
val = hole.get();
System.out.println("Cons #" + this.no + " got: " + val);
}
}
}
Prod.java
class Prod extends Thread {
private Holes hole;
private int no;
public Prod(Holes ch, int no) {
hole = ch;
this.no = no;
}
public void run() {
for (int uu = 0; uu < 10; uu++) {
hole.insert(uu);
System.out.println("Prod #" + this.no + " insert: " + uu);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
Hence, this is the code for the given Producer COnsumer problem using multithread and the random numbers between 1-100.
Please rate the answer if it helped.....Thankyou
Hope it helps.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.