Please help me figure it out. In Java, a complete newbie, this is my test of the "clumsy" MVC implementation. There is a method in which it is necessary to "wait" until the GUI receives information about the pressed button, and only then continue the execution of the method, how to implement the wait method?

import java.util.*; public class KNBGameStarter { public static void main (String[] args){ KNBGameStarter game = new KNBGameStarter(); KNBGui gui = new KNBGui(); gui.guiStarter(); game.gameLogic(); } private void gameLogic() { KNBanalyzer a = new KNBanalyzer(); KNBstrategy s = new KNBstrategy(); KNBController controller = new KNBController(); int round = 0; int lastplm = 0; int lastpcm = 0; while (true) { if (round == 0 || a.whoIsWinning()) { s.sRandom(); } if (round > 1 && a.isLpmRepeat(round-1)){ s.sAntiRepeat (lastplm); } if (round > 1 && !a.whoIsWinning() && !a.isLpmRepeat(round-1)) { s.sSuper(a.PcWonLastRound, lastpcm); } // Тут нужно реализовать wait. Thread th = Thread.currentThread(); synchronized (th){ try { th.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int plm = controller.getPcm(); // ход игрока int pcm = s.getPcm(); System.out.println("\n\r"); a.whoWon(plm, pcm); lastplm = plm; lastpcm = pcm; a.setPlayersMoves(round, plm); // добавляет ход игрока в коллекцию анализатора round++; a.showScore(); } } } 

Here is part of the GUI code where you need to implement notify () and resume the main thread.

 class ButtonListener1 implements ActionListener { public void actionPerformed(ActionEvent event) { controller.setPcm(0); Thread th = Thread.currentThread(); synchronized (th) { th.notify(); System.out.println("Ответ "+controller.getPcm()); } } } class ButtonListener2 implements ActionListener { public void actionPerformed(ActionEvent event) { controller.setPcm(1); Thread th = Thread.currentThread(); synchronized (th) { th.notify(); System.out.println("Ответ "+controller.getPcm()); } } } class ButtonListener3 implements ActionListener { public void actionPerformed(ActionEvent event) { controller.setPcm(2); Thread th = Thread.currentThread(); synchronized (th) { th.notify(); System.out.println("Ответ " + controller.getPcm()); } } } 

And the controller code

 public class KNBController { private int pcm; public int getPcm() { return pcm; } public void setPcm(int pcm) { this.pcm = pcm; } } 

The main thread does not resume after receiving input from the button ... What could be the problem?

  • The GUI is always waiting for events. You can run the desired method on the event "pressed button". If you need to do something in another thread, then you need to start another thread first, and not deal with dildos in Thread.currentThread (); It is surprising that nothing happens. - Sergey

1 answer 1

Using the stream for synchronization is not very good. Moreover, it is clear that you can not even get the desired thread, instead twist the unfortunate currentThread.
In order for notify () wait () to work, you need to call them on the same object. And currentThread as you would guess in different thread-ah will be different. notify () run on one and wait () on another. As a result, FIG.

It is much more profitable to use some kind of global object that everyone knows for sure.

 public class KNBGameStarter { Object sync; // объект для синхронизации public KNBGameStarter(Object sync) { this.sync = sync; } public static void main (String[] args) { Objecy sync = new Object(); // создаём глобальный объект KNBGameStarter game = new KNBGameStarter(sync); // распространяем его через конструктор KNBGui gui = new KNBGui(); gui.guiStarter(); game.gameLogic(); } private void gameLogic() { ... while (true) { ... synchronized(sync) { ... sync.wait(); ... } ... } ... } ... } 

I don’t know how the controller turned out to be in ActionListener , but obviously nothing prevents me from throwing sync in the same way.

 class ButtonListener1 implements ActionListener { public void actionPerformed(ActionEvent event) { controller.setPcm(0); synchronized (sync) { sync.notify(); System.out.println("Ответ "+controller.getPcm()); } } } 

Now notify () and wait () will work on the same sync object.
This is not a tricky way you can use more advanced means of synchronization.

And note - no thread. Let yourself thread-yat imperceptibly.

I can not guess about its purpose controller , but perhaps in this case it would be better to use it instead of sync .