title: How to stop ActionListener from halting all other code?
body: <p>I asked <a href="https://stackoverflow.com/questions/21874923/how-to-properly-wait-until-actionlistener-is-executed/21874997#21874997">this</a> question yesterday and have attempted to implement the top answer I received. So from the code from yesterday I have tried adding <code>synchronized</code> to my methods and using <code>wait()</code> and <code>notifyAll()</code>. I have been looking up a bunch of examples, and reading the documentation but I'm definitely not doing it right. </p>
  
  <p>Basically, once a touchEvent happens my <code>ButtonListener</code> stops all of my other code from executing, and only performs the code contained within the <code>ButtonListener</code>. <strong>This only happens on one of my computers though, my laptop runs the code the way I expected it to, my desktop gets stuck in the <code>ButtonListener</code>.</strong> This was a school assignment I turned in a couple weeks ago and initially received a 70%, but I explained to my teacher that it works on some computers and he ran it on his office desktop, and viola, it worked and I got a 100%. Of course I want to figure out what the problem is though so that is why I'm still working on this.</p>
  
  <p>Here are the offending code snippets:</p>
  
  <pre><code>    public synchronized void playOneTurn(int player)
          throws InterruptedException {
      waiting = true;
      while (waiting) {
          try {
              System.out.println("playOneTurn before wait.");
              wait();
          } catch (InterruptedException e) {
              // e.printStackTrace();
          }
      }
      System.out.println("playOneTurn AFTER wait.");
      }
  </code></pre>
  
  <p>My method here, <code>playOneTurn</code>, is the last bit of code that runs before the first touch event. Previously, as you can tell by looking at my question linked at the top, I used a simple <code>while</code> loop that waited for my <code>ButtonListener</code> to flip a boolean, <code>waiting</code>. Above is my attempt to use <code>synchronized</code>.</p>
  
  <pre><code>    class ButtonListener implements ActionListener {
      @Override
      public synchronized void actionPerformed(ActionEvent e) {
          System.out.println("Entering Action Performed");
          for (i = 0; i &lt; size; i++) {
              for (j = 0; j &lt; size; j++) {
                  if (e.getSource() == cells[i][j]) {
                      if (model[i][j] == 'e') {
                          cells[i][j].setText("");
                          currentSpot[0] = i;
                          currentSpot[1] = j;
                          if (count % 2 == 0) {
                              cells[i][j].setBackground(Color.GREEN);
                              cells[i][j].setIcon(X_MARK);
                              model[i][j] = 'x';
                              count++;
                              waiting = false;
                              System.out.println("Boolean hit");
                              notifyAll();
                          } else {
                              cells[i][j].setBackground(Color.CYAN);
                              cells[i][j].setIcon(O_MARK);
                              model[i][j] = 'o';
                              count++;
                              waiting = false;
                              System.out.println("Boolean hit");
                              notifyAll();
                          }
  
                      } else {
                          System.out
                                  .println("Hey, you can't move there silly!");
                      }
                  }
              }
          }
      }
      }
  </code></pre>
  
  <p>Here is my <code>ButtonListener</code> where my program just chills in and stops doing anything else. Please disregard my random <code>println</code>s everywhere, I was just getting aggravated and trying to find out what this thing is doing.</p>
  
  <p>I also had to add this try/catch block to my controller class:</p>
  
  <pre><code>        while (!game.haveWinner() &amp;&amp; !game.isFull()) {
          player = (player + 1) % 2;
          try {
              game.playOneTurn(player);
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
               e.printStackTrace();
          }
          System.out.println("While loop looped");
      }
  </code></pre>
  
  <p>I have been trying every way I can think of to correctly implement <code>synchronized</code> but I'm obviously doing something wrong.</p>
  
  <p><a href="https://www.dropbox.com/sh/6th0fj8fps6wpgi/33jYrKQICV" rel="nofollow noreferrer">Here is a Dropbox link to the entire program if it would help.</a></p>