title: Java - creating separate thread for sending objects over the Internet
body: <p>I am writing Battleships game in Java for Event-Driven Programming on my studies. The game is supposed to be a network game and i am going to make client and server in one application.</p>
  
  <p>This is an image representing structure of my application:
  <a href="http://dl.dropbox.com/u/41993645/mvc.jpeg" rel="nofollow noreferrer">class diagram http://dl.dropbox.com/u/41993645/mvc.jpeg</a></p>
  
  <p>To get to the point - i want to write Server class that will run as separate thread and are responsible for remote View - Controller communication. So, the Server class will be responsible for:</p>
  
  <ul>
  <li>Reading objects from socket in infinite loop and if any arrives,
  putting them in the BlockingQueue for the Controller.</li>
  <li>Providing method like 'sendActionEventToView()' which will allow Controller to pass object the other way - from Controller to remote View.</li>
  </ul>
  
  <p>Unfortunately, when running the Server thread, whole application stops responding. If anybody would tell me what am i doing wrong, i would be very grateful. This is in my opinion the problematic part of Server code:</p>
  
  <pre><code>/** Main Server method - responsible for reading objects
   *  and putting them in the queue if any arrived */
  public void run() {
    GameEvent event;
    while(true) {
      try {
        event = (GameEvent)objectStream.readObject();
        if(event != null) eventQueue.put(event);
      } catch(ClassNotFoundException e) {
        e.printStackTrace();
      } catch(IOException e1) {
        e1.printStackTrace();
      } catch(InterruptedException e2) {
        e2.printStackTrace(); 
      }
    }
  }
  </code></pre>
  
  <p>I think thread hangs on 'readingObject()' - how can i force it to give processor time to another threads if there are not any objects in the stream?</p>
  
  <p>Rest of Server: <a href="https://github.com/mc-suchecki/Battleships/blob/master/controller/Server.java" rel="nofollow noreferrer">https://github.com/mc-suchecki/Battleships/blob/master/controller/Server.java</a>
  Rest of the app: <a href="https://github.com/mc-suchecki/Battleships" rel="nofollow noreferrer">https://github.com/mc-suchecki/Battleships</a> </p>
  
  <p>Thank you very much in advance, if anything is unclear, please comment. And sorry for my English.</p>