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

[4222 byte] By [ByronTheOmnipotenta] at [2007-11-27 11:08:29]
# 1

Every object has a wait-set o threads. calling notify takes one thread out of the

wait-set and puts it into the ready-set for that object. There's no guarantee that

this most-recently liberated thread will be the one that will next grab the

object lock. That's all in the vagaries of thread-scheduling. It could even be the

thread that called notify that gets in next!

Hippolytea at 2007-7-29 13:28:58 > top of Java-index,Java Essentials,New To Java...
# 2

is there any way to specify which one gets notified? like does notify() method have any args?

Message was edited by:

ByronTheOmnipotent

ByronTheOmnipotenta at 2007-7-29 13:28:58 > top of Java-index,Java Essentials,New To Java...