This may be a case of me just misunderstanding what I've read, but all the examples for killing a thread in Java seem to indicate that you have to signal a thread to kill itself; you can't kill it from the outside without some serious risks. The problem is, all the examples of how to "politely" ask a thread to die have some kind of looping so all you have to do is watch a flag on every iteration.
So, what I've got is a thread that does something that just takes a while (a series of SQL queries). It's certainly possible for me to just have a check after each step, but they aren't in a loop and there isn't a very elegant way that I'm aware of to get around that. Here's an example of what I'm doing:
new Thread(new Runnable(){
public void run(){
//query 1
Connection conn = db.getConnection();
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT ...");
while(rs.next()){
//do stuff
}
//query 2
rs = s.executeQuery("SELECT ...");
while(rs.next()){
//do stuff
}
//query 3
rs = s.executeQuery("SELECT ...");
while(rs.next()){
//do stuff
}
}
}).start();
This is an example, I don't use anonymous inner classes but it illustrates that my run() method can't elegantly stop itself. Futhermore, even I check after each step, if a particular query takes a very long time to run, this code would not be able to stop until after the query was complete.
This code is for a GUI application, and I would really like to find a good way to kill a thread quickly without using Thread.stop().
EDIT - yshavit's answer was a big help, as I wasn't aware that Statement.cancel()
existed. If you're curious, the answer to my particular problem was to build out a more abstracted database access class. The class had to create a child thread to execute the query and loop while it was running, checking on every iteration if the current thread (not the child) was interrupted. If it does get interrupted, it just calls Statement.cancel() and the child thread will throw an exception and die. Not all JDBC drivers support Statement.cancel()
, but Oracle 11g does.