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?