views:

662

answers:

5

Question: I'm interested to know the best practice for killing a long standing operation that is running as a background thread (lets call this thread WorkerThread) in Java 1.2.

Scenario

Specifically, I'm developing an application for Blackberry devices whereby I make a HTTP connection. Big picture: a URL request if forwarded to a background thread (WorkerThread), the thread makes the request and returns the result using a call back.

Scenario Details

Now there exists a situation where at connection time, a data connection exists but then for whatever reason (drives through a tunnel) that connection no longer exists. Due to a limitation in Blackberry's design architecture, that actual connection will hang as the time out is fixed to be 2 minutes. As a result, there's a crucial need to kill a connection that has been hanging for a relatively (15 seconds) long period of time.

My Current Solution - 2 Theads?

Right now my current solution is to run WorkerThread inside another thread (lets call this new thread MonitorThead). MonitorThread starts WorkerThread, sleeps for 1000ms and then routinely checks if WorkerThread is still alive. If after 15 seconds WorkerThread is still alive, MonitorThread puts WorkerThread to sleep and exits. Is this really the best possible approach?

Summary of Question & Key points

In summary, below is the core question and key restraints associated with the question. Cheers!

How do I successful kill a java background thread that is stuck in a specific operation?

Scenario Restraints:

  • No control of having operation pause and check the threads requested state
  • Specific to Blackberry's implementation of Java ME 1.2 and its Thread API so no explicit kill() method
  • Most concerned about the best practice and how to most safely kill a holding thread.


Follow Up/Edit
Neil Coffey recommended that I simply hold a reference to the connection object and instead call close() on that object. I am currently looking into this...

+1  A: 

How to kill a Thread is a difficult question. There is no guaranteed way to be able to stop or interrupt a Thread. However, if you take your current architecture and upon timeout, just close the stream (not the Connection), that should cause an I/O Exception to occur on the thread that is stuck in I/O. If it doesn't cause an IOException, then it should at least cause the read or write to return with EOF.

Note that closing the Connection doesn't help, as the JavaDoc says:

Any open streams will cause the connection to be held open until they themselves are closed.

You have to close the stream that was derived from the Connection.

Eddie
As I understand from the question, the poster's problem is that he can't.
Neil Coffey
yes, unfortunatly I'm using HttpConnection to make my connection, not sockets. Within Blackberry's API they decided not to fufill user specified timeout for HttpConnection but they do fufill user specified timeouts for Socket connections. Unfortunatly I'd like to use the features that come along with the HTTP protocol.
AtariPete
I updated my answer. I use the method I talk about here in JavaSE, sometimes. That is, force the socket closed to force all pending I/O operations to quit with IOException.
Eddie
Thanks eddie. So mostly likly I can simply call close() since its one of the methods may be invoked while the connection is in Setup or Connected state (as detailed in the RIM HttpConnection API javadoc: http://www.blackberry.com/developers/docs/4.2api/javax/microedition/io/HttpConnection.html)
AtariPete
+1  A: 

Well, the best practice would normally be to get the connection to close, and then let the consequences of that ripple through to the thread, allowing it to exit cleanly.

How are you making the connection? Rather than waiting for it to time out, what are your chances of forcing it to close? Can you get hold of some connection object? Does the Blackberry have some other command that can be executed to kill a given connection?

Neil Coffey
This is a really interesting idea. Right now my HttpConnection request is wrapped in a static method whereby I wait on a the request.Maybe if I do as you say and keep a reference to HttpConnection and call close via the 'MonitorThread' i can cleanly (or as cleanly as possible) close the connection. I'll try this out this weekend.Thanks!
AtariPete
According to the documentation, closing a Connection won't do anything. You have to close the Stream, not the Connection.
Eddie
A: 

I don't know RIM's code, is there some reason the interrupt() method can't be used?

Charlie Martin
If you are waiting in I/O, then interrupt() won't do anything. That is, only method calls that explicitly declare that they throw an InterruptedException will actually get interrupted when you call interrupt().
Eddie
A: 

I always believed that passing a flag into background thread in an atomic transaction has been the best way to ask a thread to stop. If it doesn't stop for a while, kill it.

Well, to add to that, if you believe 2 minutes is a long time, good for you. I'd let the user decide what's a long time with a cancel button.

GregC