title: Java Multi Threading, make a thread wait for another thread, or joining different thread
body: <p>this is my first time asking here. I have many times helped before when searching and always found this site. But now I can't found the answer for my problem. It has some kind of situation with many other question, but I can't found the one that's in my need.</p>
  
  <p>Okay, so I have class named player, farm, and land. Farm is the kind of controller. Farm have an array of object called map, which is shared with land and player (given when they are constructed), and inside the map is player and land itself.</p>
  
  <p>What I want to do is, </p>
  
  <p>I left-click on farm, and I got the object from map based on where I clicked, then I save that object as selected. Then I right click other object and got the object, lets call it actioned. Actioned have a method, getMenu(selected),  which return a JPopMenu, based on selected. As an Example, if player is the selected, and land is actioned, the getMenu() will return menu "plow", "water" and "move". If I click at "plow", then the land will changed, and of course the map also changed, which shared with other object.
  The problem is, I need the player to walk to grid near the land first, before the real action of plow begin. So in the plow menu I add an ActionListener like this:</p>
  
  <pre><code>new ActionListener() {
          public void actionPerformed(ActionEvent e) {
                  int gx = (int)Point2D.this.X();
                  int gy = (int)Point2D.this.Y();
                  Object lock = new Object();
                  player.move(gx,gy,lock);
                  try {
                      lock.wait();
                  } catch (InterruptedException ex){}
                  // do the real action of plow here
              }
          }
  }
  </code></pre>
  
  <p>And the move method of player</p>
  
  <pre><code>public void move(int gx,int gy,Object lock){
      setDestination(gx, gy);
      this.lock = lock;
  }
  </code></pre>
  
  <p>The farm is a Runnable object, so every time the farm will call player.Update().</p>
  
  <pre><code>public void update(){
      updatePosition();
      if (position==destination) {
          lock.notify();
      }
  }
  </code></pre>
  
  <p>I thought of made an object called lock there, and that object given to player. the Listener  is a running thread itself, and started when the menu is clicked, so I make the thread of Listener to wait, based on the lock object. The notify is given from the farm thread.</p>
  
  <p>But of course, that is not working well, and throw java.lang.IllegalMonitorStateException. I don't really get what the synchronization explanation from the web. <strong>Question 1:</strong> How should I use the synchronization here to make the code work? Or what should I do if the synchronization will never work here?</p>
  
  <p>I have also thought of using other approach.</p>
  
  <p>I make the player as a runnable thread to. Now I have 3 thread, the farm, listener(land), and player.</p>
  
  <p>So instead of make a lock object, I make a new Thread(Player). The run method of player:</p>
  
  <pre><code>while (position != destination) {
         try {
              Thread.sleep(100);
         } catch (InterruptedException e){}
     }
  </code></pre>
  
  <p>The new listener method:</p>
  
  <pre><code>        Player player = (Player) selected;
          int gx = (int)Point2D.this.X();
          int gy = (int)Point2D.this.Y();
  
          player.move(gx,gy);
          Thread waitplayernotmoving = new Thread(player);
          waitplayernotmoving.start();
          try {
              waitplayernotmoving.join();
          } catch (InterruptedException e){}
  </code></pre>
  
  <p>It is work well enough. <strong>Question 2:</strong> is the land thread stop working while waiting the player thread? if not, how to make the land thread to stop while waiting the player thread dead?</p>
  
  <p>And I have other problem. If I plow land1, and then while the player moving, I click at land2 and plow land2, the player is turning to land2! And then after it is not moving again, both of the land is plowed! Meaning the thread of land1 is still active.</p>
  
  <p>I have two question:
  <strong>Question 3:</strong> Is it possible to make the land1 thread die if I set another action to player, such as plow land2?</p>
  
  <p><strong>Question 4:</strong> Is it possible to make the land2 thread executed after land1 thread based on what my program doing now? So the action is like a queue. I plow land1, then plow land2, the action happen will be player move to land 1 and then plow land 1, then move to land2, and plow land 2.</p>
  
  <p><strong>Question 5</strong> Remember my map, the array of object which is shared to all? How to ensure it is safe? I mean, looks like there will be many thread reading and writing the map. I read somewhere if it is immutable it won't be a problem. But it is re-written so often.. won't it be a problem?</p>
  
  <p>Thank you very much for reading this long explanation, And I hope you can give me an answer. :(</p>