Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your <code>Server</code> constructor blocks, potentially indefinitely, in <code>accept()</code>.</p> <p>Two things about Swing programs:</p> <ol> <li>Never do any long task in the Swing event thread, and,</li> <li>Never do manipulate any Swing object outside the Swing event thread unless the method being used is explicitly documented as thread-safe.</li> </ol> <p>This means that if the server is being started from the Swing event thread -- that is, if it is being started in response to a button click or the like -- then yes you must spawn another thread for your Server object. Otherwise you guarantee that the Swing event thread will be blocked until your thread exits.</p> <p>You say that your application still stops responding even when you spawn another thread for your server? Ensure that you're calling <code>Thread.start()</code> and not <code>run()</code>, or you will accidentally still block yourself by running the "new Thread" actually in your own Thread.</p> <p>NOTES:</p> <ol> <li>I see that you do a <code>Thread.sleep(0);</code> in your run() loop. This is not guaranteed to do anything whatsoever. If you have a single CPU machine, this may be fairly be implemented as a no-op, allowing the same thread to keep running.</li> <li>You really want <code>isConnected</code> to be <code>volatile</code> -- otherwise there is no guarantee that changes to this variable will be seen by any thread other than the one where it is changed.</li> <li>You don't set <code>isConnected</code> to false, anywhere, so your <code>run()</code> will run until the JVM is stopped or until that Thread takes a RuntimeException.</li> <li><a href="http://www.ibm.com/developerworks/java/library/j-jtp0618.html" rel="nofollow noreferrer">It is discouraged</a> to start Threads in constructors. (See <a href="http://rads.stackoverflow.com/amzn/click/0321349601" rel="nofollow noreferrer">Java Concurrency In Practice</a>.)</li> <li>You don't want to <code>accept</code> on your <code>ServerSocket</code> until you are in your Thread's <code>run()</code> method! Otherwise your constructor will block waiting for a connection and will not return control to the event thread!</li> <li>You have the following code in your constructor:</li> </ol> <p>your code is:</p> <pre><code>thread = new Thread(this); thread.setDaemon(true); //thread.run(); </code></pre> <p>When you had <code>thread.run()</code> not commented out, you were <strong>not</strong> starting a new Thread! To do that, you need to do <code>thread.start()</code>. Instead, you were running this new Thread (which will never stop, for reason #3 above) in the same thread that invoked the constructor. The way your code is written right now, all IOExceptions are logged, but otherwise swallowed. You probably want to set <code>isConnected</code> to <code>false</code> on any <code>IOException</code>, as well as in <code>closeConnection()</code>.</p>
 

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