0

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).

randers
  • 5,031
  • 5
  • 37
  • 64
yerassyl
  • 2,958
  • 6
  • 42
  • 68
  • 1
    If you follow my answer http://stackoverflow.com/a/33202013/913286 your design will be much simpler, you just need the right Queue. – Gil Vegliach Oct 18 '15 at 19:52
  • @GilVegliach, yeh, I just have no idea how to use Blocking queue in the context of my problem – yerassyl Oct 18 '15 at 19:57
  • `BlockingQueue q = new LinkedBlockingQueue()`; you insert strings with `q.put("asdf")` and remove them (blocking) with `q.take()`. – Gil Vegliach Oct 18 '15 at 20:01
  • @GilVegliach, how to acces it from other classes, can i declare it public static? EDIT: sorry of course i can) – yerassyl Oct 18 '15 at 20:06
  • 1
    You can do that: having a static queue accessed from multiple classes (you don't even need the Resource class). Another approach would be to pass the queue as a parameter in the constructors of those multiple classes (i.e. the producer and the consumer class) – Gil Vegliach Oct 18 '15 at 20:10

1 Answers1

0

Sorry, this is meant to be a comment. I'd advise against using static in the most part of any program. Most of the time static can be avoided.

If there is only going to be one Resource class then yes, you could make it static and it could be accessed like this:

Resource.getString();

Or

Resource.addString("Hello!");

However, if you want to create multiple Resource instances then you'll need to instantiate a Resource object and access it from the different threads like this:

Resource r1 = new Resource();
r1.addString("Hello!");
String s = r1.getString();

Where the add and get statements can be executed in seperate Threads.

Steppers

Stepperz96
  • 23
  • 1
  • 6
  • what about notify() and wait() methods, if i declare my getString etc methods static then i get error that notify() can not be accessed from static context – yerassyl Oct 18 '15 at 20:23
  • Yes that's because if a method is static then everything used within that method must also be static. The notify and wait methods are not static because they are associated to a specific Resource object, which when static does not exist. Sorry, maybe a bit confusing! – Stepperz96 Oct 18 '15 at 20:29
  • You can only call other static methods within a static method. `notify()` and `wait()` are not static. [Java Object Docs!](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#notify()) – Stepperz96 Oct 18 '15 at 20:31