the right way to kill a java thread on Windows?

Discussion in 'Java' started by john, Apr 29, 2005.

  1. john

    john Guest

    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.

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

    Thanks,

    John
    (remove the pachyderm to reply)
     
    john, Apr 29, 2005
    #1
    1. Advertising

  2. john

    Ross Bamford Guest

    On Fri, 2005-04-29 at 15:08 -0700, 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.
    >
    > What is the right way to kill a program like mine, so that it releases the
    > port?
    >
    > Thanks,
    >
    > John
    > (remove the pachyderm to reply)
    >
    >


    Why not listen for a specific 'shutdown' message over your socket?

    Forcibly killing a process isn't guaranteed (or even likely) to release
    resources like your socket, and in the case of a virtual machine (such
    as the JVM) it doesn't even kill the real process, just the process
    inside the VM, as you note in your post.

    --
    [Ross A. Bamford] [ross AT the.website.domain]
    Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
    http://www.roscopec0.f9.co.uk/ + in
     
    Ross Bamford, Apr 30, 2005
    #2
    1. Advertising

  3. john

    R.F. Pels Guest

    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
     
    R.F. Pels, Apr 30, 2005
    #3
  4. R.F. Pels coughed up:
    > 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 ignore the interrupted exception, then what checks the flag?


    > 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.




    --
    Enough is enough. It is /not/ a requirement that someone must google
    relentlessly for an answer before posting in usenet. Newsgroups are
    for discussions. Discussions do /not/ necessitate prior research. If
    you are bothered by someone asking a question without taking time to
    look something up, simply do not respond.
     
    Thomas G. Marshall, Apr 30, 2005
    #4
  5. john

    john Guest

    > Why not listen for a specific 'shutdown' message over your socket?

    This would mean that I'd have to write a little client program to send my
    server program a "shutdown message," right? I guess I was hoping that there
    was an easy & clean way to do it from the OS. But what you're suggesting is
    reasonable. That's what I'll do.

    -John
     
    john, May 2, 2005
    #5
  6. john

    Esmond Pitt Guest

    R.F. Pels wrote:
    > 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.


    OS's are not that dumb (except Netware!) and this is entirely untrue,
    but if the ServerSocket had any connections active the socket will be
    unusable for 2*MSS usually = 2 minutes. If you wait two minutes you will
    be able to restart OK, or you can do ServerSocket.setReuseAddress(true)
    before you bind it to the port.

    > 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.


    It is simpler just to close the ServerSocket if you can: this will cause
    accept() to throw a SocketException("socket closed") which is a signal
    to stop accepting. Anyway you can't do anything with any socket that has
    had a SocketException except close it.
     
    Esmond Pitt, May 3, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bm
    Replies:
    11
    Views:
    6,165
  2. Jerry Sievers

    thread, threading; how to kill a thread?

    Jerry Sievers, Nov 17, 2004, in forum: Python
    Replies:
    12
    Views:
    1,154
    Mustafa Demirhan
    Nov 19, 2004
  3. Replies:
    7
    Views:
    1,560
  4. Jason Tahaney
    Replies:
    2
    Views:
    104
    Jason Tahaney
    Jul 20, 2006
  5. Charles Oliver Nutter
    Replies:
    43
    Views:
    626
    Roger Pack
    Mar 25, 2008
Loading...

Share This Page