Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't want to call <code>gameRoom.run()</code> directly. That causes it to execute in the current thread, which ties up your server loop until that <code>run()</code> method completes. Instead, you should run it in a separate Thread. An <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html" rel="nofollow">ExecutorService</a> obtained from the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html" rel="nofollow">Executors</a> factory class would be a simple way of handling that. For instance, create an ExecutorService in your ServerClientThreadPool: </p> <pre><code>public class ServerClientThreadPool { private ExecutorService executorService = Executors.newCachedThreadPool(); </code></pre> <p>and then submit the gameRoom to the service after accepting the socket connection:</p> <pre><code>GameRoom gameRoom = new GameRoom(serverListener.accept()); executorService.submit(gameRoom); </code></pre> <p>Notice that I didn't write <code>extends Thread</code> on the ServerClientThreadPool above. You're not using it as a Thread in any way in the above code, and it probably wouldn't be right to do so unless you need the server to be accepting connections at the same time as other things are happening (besides the game rooms, which will already be handled in separate threads). If you did need that, then it should also be a <code>Runnable</code>, not a <code>Thread</code>, and also be submitted to an <code>ExecutorService</code>.</p> <p>Based on the apparent confusion in your code about how threading works, I'd suggest you revisit the <a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html" rel="nofollow">Java "Concurrency" tutorial</a>, which includes <a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html" rel="nofollow">a section on Executors</a>.</p> <p><strong>Bonus:</strong> I think passing JSON back and forth is a reasonable way of commnunicating. Doing that implies that you're not overly concerned with performance, so I'd suggest you stop handling the sockets yourself and instead use an embedded HTTP server on the server side and a simple HTTP client on the client side. That'll handle all of the socket and threading nonsense for you, freeing you up to worry about the details of the game logic.</p> <p>For example, <a href="http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty" rel="nofollow">embedding Jetty</a> is a trivial undertaking, and there are a number of powerful and concise HTTP/REST clients out there like the <a href="http://hc.apache.org/httpcomponents-client-ga/quickstart.html" rel="nofollow">Apache Http Client</a> and the <a href="http://jersey.java.net/nonav/documentation/latest/user-guide.html#d4e609" rel="nofollow">Jersey REST Client</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload