how to kill a thread which is waiting for blocking function call in Java?
I have a thread:
Thread t = new Thread(){
public void run(){
ServerSocketConnection scn = (ServerSocketConnection)
Connector.open("socket://:1234");
// Wait for a connection.
开发者_C百科 SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
//do other operation
}
};
t.start();
Lets say no client is connecting to the Server, so this thread will be blocked.Now I want to kill the above thread t? How can I kill it?
Thread.interrupt()
will not interrupt a thread blocked on a socket. You can try to call Thread.stop()
or Thread.destroy()
, but these methods are deprecated (edit: actually, absent in J2ME) and in some cases non-functional, for reasons you can read about here. As that article mentions, the best solution in your case is to close the socket that you're blocking on:
In some cases, you can use application specific tricks. For example, if a thread is waiting on a known socket, you can close the socket to cause the thread to return immediately. Unfortunately, there really isn't any technique that works in general. It should be noted that in all situations where a waiting thread doesn't respond to Thread.interrupt, it wouldn't respond to Thread.stop either. Such cases include deliberate denial-of-service attacks, and I/O operations for which thread.stop and thread.interrupt do not work properly.
You can call thread.interrupt()
. This will throw a InterruptedException
in your thread
Call Thread.interrupt(). Blocking operations typically throw an InterruptedException if the Thread they are executing on is interrupted.
Make the main Thread MONITOR THREAD (from where you spawn this new Thread) join() for this Thread. If after a given time-out, this thread does not "join" then you can ignore (nullify) this thread. This will give you an added functionality adding time-out for blocking thread.
I think you would just need to close the connection - so simply make scn
a field of the thread and implement:
public void stop() {
scn.close();
}
and code your main method to exit gracefully on IOException
or other indication that you are done (as you probably already have).
In your main thread (when you want to terminate the accept-thread) call scn.close()
. The acceptAndOpen
should then throw an IOException
, which you may catch and then terminate the accept-thread gracefully.
ServerSocketConnection scn =
(ServerSocketConnection) Connector.open("socket://:1234");
Thread t = new Thread(){
public void run(){
// Wait for a connection.
try {
SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
//do other operation
} catch (IOException e) {
// Log the exception
}
}
};
t.start();
// ...
// Somewhere else...
// Cancel the acceptAndOpen.
scn.close();
A good idea would be to introduce a synchronization mechanism so you don't close the socket right after the acceptAndOpen went through.
I just came to this page looking for the same answer. I found a way to close Threads and this is what I do to end the server:
private Thread serverThread;
public void setUp(){
//Run the server
serverThread = new Thread() {
public void run() {
try {
new Server(); //it calls acceptAndOpen() here
} catch (IOException ex) {
ex.printStackTrace();
}
}
};
serverThread.start();
}
public void tearDown() {
serverThread = null;
System.gc();
}
Basically, make your reference to your server thread null, and then let the garbage collector clean up resources (which is your server) for you.
Note though: This is far from perfect, we should never depend on gc to do its job, but it seems to work everytime I've tried it
精彩评论