Synchronize two threads that do not know each other [Java]

First of all, excuse my poor English = S Second, sorry, if my title is a little strange, I did not know how to formulate it better.

I have a little problem. I am developing a game in Java, and I am using the MVC pattern the way I was taught. I don’t know if this is the most efficient way, but, in any case, here’s the general thing: 4 packages: model, view, controller and "observer".

There is one “Observer” interface in the observer, in which all the methods that the element “monitor the model” should implement are defined. There is also an Observable class with a list of observers and in all ways that notify them of some changes (methods such as "fireEvent1 (int i) {for (Observer obs: observers) obs.Event1 (i);}") The model extends the class Observable, and some GUI elements implement the Observer interface.

Now my problem: in the method in the model, I want the model to “wait” for 2 user interactions. This means something like this:

  • method invoked in the model
  • do something.
  • wait for the user to interact for the first time
  • get information about what the user has just done (which is assembled in the controller)
  • do something.
  • ,
  • do stuff

: , ActionListener, ActionListener.

, , , , , , . , , , , . , , , , , .

, , , , , .

Scentle5S

EDIT: , . , , . (, , , , . wait(), , ?).

, , , .

, .

public class Model extends Observable {

    public void waitForActions() {
        System.out.println("Wating for two user actions");

        Thread t = new Thread() {
            public void run() {
                while (true) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                    }
                }
                System.out.println("First action received in model : "
                        + message);
                while (true) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                    }
                }
                System.out.println("Second action received in model : "
                        + message);
            }
        };
        fireWaitForActions(true);
        t.start();
        fireWaitForActions(false);
    }
}

public class Controler implements ActionListener {
    private Model model;

    public Controler(Model model) {
        this.model = model;
    }

    public void actionPerformed(ActionEvent e) {
        String message = ((JButton)e.getSource()).getText();
        System.out.println("Action received in controler : "+message);
    }
}

public class View extends JFrame implements Observer {
    private JButton b1 = new JButton("Action 1");
    private JButton b2 = new JButton("Action 2");
    private Controler controler;

    public View(Controler controler) {
        this.controler = controler;

        b1.addActionListener(controler);
        b2.addActionListener(controler);

        b1.setEnabled(false);
        b2.setEnabled(false);

        JPanel container = new JPanel();
        container.add(b1);
        container.add(b2);
        setContentPane(container);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void waitForActions(Boolean b) {
        b1.setEnabled(b);
        b2.setEnabled(b);       
    }
}

public static void main(String[] args) {
    Model model = new Model();
    Controler controler = new Controler(model);
    View view = new View(controler);
    model.addObserver(view);
    model.waitForActions();
}

Observable and Observer classe/interface:

public class Observable {
    private LinkedList<Observer> observers = new LinkedList<Observer>();

    public void addObserver(Observer obs) {
        observers.add(obs);
    }

    public void fireWaitForActions(boolean b) {
        for (Observer obs: observers) obs.waitForActions(b);
    }
}

public interface Observer {
    public void waitForActions(Boolean b);
}
+4
3

. Java .

wait() notify() , . () release() , . , , .

wait()/notify() ()/release(), , .

0

( ), , , .

java.util.concurrent.locks

.

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/package-summary.html

, ReentranceLock

.lock()

.unlock()
0

I think this is a bit confusing explanation given by you, or you misunderstood your own demand. Whenever a request appears in your web container, it will create a thread to process the request. You talk about these streams, or you can explain what you need so that you want to meet in children.

0
source

Source: https://habr.com/ru/post/1569372/


All Articles