notify() question
Alright, ive been learning about multithreading and here is my question. In the below example in the queue class, does the notify(); statement notify the opposite method that is waiting to continue, or the one that the statment is being called from? Because the get method is running on a loop from one thread and same for put method.
class Queue{
int exchangeValue;
boolean busy =false;
synchronizedint get(){
if (!busy)
try{
wait();
}catch (InterruptedException e){
System.out.println("Get: Interrupted Exception");
}
busy =false;
System.out.println("Get: " + exchangeValue);
notify();
return exchangeValue;
}
synchronizedvoid put (int exchangeValue){
if (busy)
try{
wait();
}catch (InterruptedException e){
System.out.println("Put : Interrupted Exception");
}
busy =true;
this.exchangeValue = exchangeValue;
System.out.println("Put: " + exchangeValue);
notify();
}
}
class Publisherimplements Runnable{
Queue q;
Publisher(Queue q){
this.q = q;
new Thread(this,"Publisher").start();
}
publicvoid run(){
for (int i = 0; i < 5; i++){
q.put(i);
}
}
}
class Consumerimplements Runnable{
Queue q;
Consumer(Queue q){
this.q = q;
new Thread(this,"Consumer").start();
}
publicvoid run(){
for (int i = 0; i < 5; i++){
q.get();
}
}
}
class Demo{
publicstaticvoid main(String args[]){
Queue q =new Queue();
new Publisher(q);
new Consumer(q);
}
}
Message was edited by:
ByronTheOmnipotent
Message was edited by:
ByronTheOmnipotent
Message was edited by:
ByronTheOmnipotent

