XML Feeds

.

[java-dev] Re: Crashing after a while...

Owen Green owen at owengreen.net
Fri Aug 10 04:56:08 MDT 2007


Michael,

Not had a chance to look in detail yet, but a couple of comments:

- Your exception handling isn't really handling anything:
* Be more specific than just catch(Exception... (so catch IOException 
when you need to), and work out what you need to happen - just 
swallowing them will lead to grief. In any case, if a socket throws any 
kind of exception, you need to assume it's junked, and close it properly.

*Closing any kind of native resource (file, socket etc.) properly means 
using a finally block;

try{ blah} catch{handle} finally {close}

Not doing this means you can end up with resource leaks, and consequent 
gnarliness.

Because you're sharing a socket between two threads, you'll need some 
mechanism for communicating when the socket has been junked, or some such.

- Those primitive variables that you're sharing between threads 
(heartbeat etc.) should, ideally, be declared 'volatile' to guarantee 
that their value is well defined at all times (similar in effect to 
synchronising).

hth,
Owen





mji wrote:
> Sure. The code is below:
> 


More information about the java-dev mailing list