Multithreading in Java: Wait until two buttons are pressed? -


I am currently writing a small application with swing-interfaces that contain a group of buttons. Now my problem is : After starting the application, one method is said that I want to wait to press two buttons and then proceed normally. I have two threads, one main thread and one that is designed to wait until just two buttons are pressed. For the button I had added an AppLunistener in general, which increases one variable from one and calls a method which makes the variable two, when the variable is two.
So here's my code:

  int counter = 0; Fixed thread thread1; Fixed thread thread 2; Public Static Zero Main (String [] Elggs) {EventQueue.invokeLater (new Runnabal () (non-public) {try {thread1 = new thread ()} {@Override Public Zero Run () {MainFrame Frame = New Mainframe () ; Frame.Seasible (true); start ();}}; thread 1. start ();} hold (exception e) {e.printstaxtrace ();}}}); } Public Mainframe () {// Other stuff and similar listeners JButton button1 = new pocket (); Button1.addActionListener (New Action Adapter) {@ Override Public Voice Action Perfid (ActionAge AE) {count ++; NotifyIfTwo ();}}); } Public Zero Notified IF Two () {if (count == 2) {synchronized (thread2) {notifyAll (); }}} Public Zero Start () {Threads 2 = New Thread () {@ Override Public Wide Run} {try {synchronized (thread1)} {thread2.wait (); }} Grip (Interrupted e) e.printStackTrace (); }}}; Thread2.start (); // Other stuff}   

No matter how I do it, I always get an invalid Monitor Status Appeal. I also tried with a loop within this thread 2, which will check that the counter was still two, but I got the same results. I think there is something to do with synchronization problems, but I'm new to whole multilevel content so I appreciate it if you can get me some hints in the right direction.
Or you also know that "wait until the two buttons are pressed" - the problem?

Thank you in advance, use the respect

. This allows countdown () to wait for the number of invocations.

Thread.wait (); There is no point in your code, it is not a compilation wait The method can be applied to the object that you are synchronizing.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -