Re: the right way to kill a java thread on Windows?



john wrote:

> I used NetBeans + JDK 1.5.2 to write a simple program using the
> SocketServer class, on my Windows 2000 pc. The program dies not die when I
> kill it. I kill it by right-clicking on the process in the execution
> window and selecting "terminate." It appears to die within NetBeans but
> when I start it, the SocketServer instance complains that the port is not
> available. The Windows command "netstat -a" confirms that there is still a
> process listening at the port, and Windows Task Manager also shows a
> java.exe process running. If I kill that java.exe process from the Task
> Manager, then everything is ok again -- I can restart my program and
> listen on the port again.

If you kill it, the whole process will just abend and the server socket will
not be properly closed, that is, the OS doesn't realize the socket listener
is gone.

If you want to do this properly what you do is listen on the server socket
for a fixed period of time, handle the InterruptedIOException by ignoring
it, check a flag and continue if that flag is not set. If you get a
connection before timing out, handle the connection in a separate thread.

That leaves you with another chore and that is to set the flag if you tell
the process to do so. You might consider using a ShutdownHook (see the
Runtime class)or opening another server socket that listens to commands for
your server process.

> What is the right way to kill a program like mine, so that it releases the
> port?

Having said the above: that depends on how you implement.

--
Ruurd
..o.
...o
ooo
.



Relevant Pages

  • Re: I Dont Have Enough Faith to Be a Christian
    ... In perl, it's probably executable. ... kill spiders, pop them, chop, split, kill them. ... unlink arms, shift, wait & listen (listening, wait), ... die to reverse the system ...
    (uk.rec.sheds)
  • Re: Funny Jesus Pictures
    ... Suprising right? ... So willing to die or kill by just listening to others. ...
    (soc.culture.malaysia)
  • Re: !EventConnect Problem
    ... the June roll-up is available somewhere, although there's not been the usual ... The socket is not in a listening state. ... The incoming connection queue has no room for connections. ...
    (microsoft.public.windowsce.app.development)
  • Re: !EventConnect Problem
    ... the June roll-up is available somewhere, ... The socket is not in a listening state. ... The incoming connection queue has no room for connections. ...
    (microsoft.public.windowsce.app.development)
  • Re: !EventConnect Problem
    ... poll time of 500mS we get a break in communication approx once a day, ... The socket is not in a listening state. ... There's a problem with the connection address, ...
    (microsoft.public.windowsce.app.development)