Asked by JJ
Two processes communicating using (memory locations “variables” or files).
Answers
Answered by
JJ
here is my code but have an error !!
there are different classes.
// this class creates the variables we are going to use in the threads
public class Qs {
int n; //creating an integer value
boolean valueSet = false; //creating a boolean statement
//creating a method with a constraint that gets or return the integer value of the number of queues
synchronized int get() {
if(!valueSet)// if the value was not a false value , the process will wait in the queue
try {
wait();
} catch(InterruptedException e) {
System.out.println("exception handeled");
}
System.out.println("Got: " + n);
//if the value was false, it will specify the number of queues ahead and will notify the system
valueSet = false;
notify();
return n;
}
//this method doesn't return any value, it automatically either notifies or asks the process to wait in the queue
synchronized void put(int n) {
if(valueSet)
try {
wait();
} catch(InterruptedException e) {
System.out.println("exception handeled");
}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}
public class P
implements Runnable { // creating an interface for the previous class
Qs q;
P(Qs q)
{
this.q = q;
new Thread(this, "Producer").start();
}
// running the first thread, this thread allows the process to wait in the queue
public void run() {
int i = 0;
while(true) {
q.put(i++);
}
}
}
public class C implements Runnable {
Qs q;
C(Qs q) { // creating an interface for the previous class
this.q = q;
new Thread(this, "Consumer").start();
}
// running the second thread, this thread allows the process to get the integer value and be in the waiting queue
public void run() {
while(true) {
q.get();
}
}
}
// this is the class which we use to test the output
public class tester {
public static void main(String args[]) {
Qs q = new Qs(); //creating an object
new P(q);
new C(q);
System.out.println("Press Control-C to stop.");
}
}
there are different classes.
// this class creates the variables we are going to use in the threads
public class Qs {
int n; //creating an integer value
boolean valueSet = false; //creating a boolean statement
//creating a method with a constraint that gets or return the integer value of the number of queues
synchronized int get() {
if(!valueSet)// if the value was not a false value , the process will wait in the queue
try {
wait();
} catch(InterruptedException e) {
System.out.println("exception handeled");
}
System.out.println("Got: " + n);
//if the value was false, it will specify the number of queues ahead and will notify the system
valueSet = false;
notify();
return n;
}
//this method doesn't return any value, it automatically either notifies or asks the process to wait in the queue
synchronized void put(int n) {
if(valueSet)
try {
wait();
} catch(InterruptedException e) {
System.out.println("exception handeled");
}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}
public class P
implements Runnable { // creating an interface for the previous class
Qs q;
P(Qs q)
{
this.q = q;
new Thread(this, "Producer").start();
}
// running the first thread, this thread allows the process to wait in the queue
public void run() {
int i = 0;
while(true) {
q.put(i++);
}
}
}
public class C implements Runnable {
Qs q;
C(Qs q) { // creating an interface for the previous class
this.q = q;
new Thread(this, "Consumer").start();
}
// running the second thread, this thread allows the process to get the integer value and be in the waiting queue
public void run() {
while(true) {
q.get();
}
}
}
// this is the class which we use to test the output
public class tester {
public static void main(String args[]) {
Qs q = new Qs(); //creating an object
new P(q);
new C(q);
System.out.println("Press Control-C to stop.");
}
}
Answered by
MathMate
What do you expect for output?
It ran and gave only "put" and "got", and does not stop on control-C.
It ran and gave only "put" and "got", and does not stop on control-C.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.