Re: Strange Socket problem
Knute Johnson wrote:
The volatiles exist because the methods that access them can be called from
other threads. I could have synchronized the start() stop() methods but not
easily the socket variable in the run() method. I thought it was cleaner to
just use volatile.
I see a problem right there.
public void disconnect() {
if (isConnected())
if (socket != null)
try {
socket.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
Since these are controlled by separate synchronization (different 'volatile'
variables) there's a race condition trying to work with both at once.
Also, 'socket' can become 'null' between the check for not 'null' and the
'close()' call.
You need to synchronize with 'synchronized' or other strong mechanism.
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg