Re: the right way to kill a java thread on Windows?
- From: "R.F. Pels" <spamtrap@xxxxxxxxxx>
- Date: Sat, 30 Apr 2005 03:57:55 +0200
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
.
- References:
- Prev by Date: Re: Java Newbie... Read file at HTTP
- Next by Date: File remains in use after calling close
- Previous by thread: Re: the right way to kill a java thread on Windows?
- Next by thread: File remains in use after calling close
- Index(es):
Relevant Pages
|