i am doing android app in which i am doing producer consumer problem. I have following code:
package nu.hci.codemenao;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Resource {
public Queue<String> semaphore = new LinkedList<String>();
public synchronized void addString(String commands) {
semaphore.add(commands);
notify();
}
public synchronized String getString() {
while(semaphore.isEmpty())
try{ wait();}
catch(InterruptedException e){}
return semaphore.remove();
}
}
How do I call addString()
and getString()
from other classes? I have another class that puts into the queue and another that reads out.
I tried to make semaphore queue and methods static but then I can not use notify()
and wait()
(got error: non static method notify() can not be referenced from static context
).