Reputation: 61
public synchronized static int get() {
while(cheia()==false){
try{
wait();
}
catch(InterruptedException e){
}
}
if (fila[inicio] != 0) {
int retornaValor = fila[inicio];
fila[inicio] = 0;
inicio++;
if (inicio == size) {
inicio = 0;
}
notifyAll();
return retornaValor;
}
notifyAll();
return 0;
}
Why the wait() and notifyAll() do no run in this code?
IDE says: the method wait() (or notifyAll) is not static?
Can you help me?
Upvotes: 3
Views: 11172
Reputation: 69
synchronized static method locks on the Class object, so naturally, you can do:
[youclass].class.wait();
[youclass].class.notify();
Upvotes: 3
Reputation: 3191
You're calling a non-static method such as wait()
or notifyAll()
from a static method. You cannot do this. Change your get method to this
public synchronized int get()
Upvotes: 1
Reputation: 40256
It is because you are within a static method, which means the method is executing on the class instance and not the object instance. wait
and notify
are instance methods.
Create an object lock instead and use that to do the synchronization and signaling.
private static final Object lock = new Object();
public static int get(){
synchronized(lock){
lock.wait();
lock.notify();
...etc
}
}
Upvotes: 5
Reputation: 182829
What do you expect it to wait
for? You have to wait for some specific object to be notified and there's no object here.
Upvotes: -1