2

How can I stop a thread running after it has been running for 5 seconds ?

I am unable to use java 5+ threading features (must work in j2me).


Possible solution -

Schedule two threads. One which performs the actual work(T1) and the other which acts as monitor of T1(T2).

Once T1 has started then start T2. T2 calls the isalive() method on T1 every second and if after 10 seconds T2 has not died then T2 invokes an abort on T1, which kills the T1 thread.

is this viable ?

Timer timer = new Timer();

TimerTask timerTask  = new TimerTask() {

public void run() {
        getPostData();
    }
};

timer.schedule(timerTask, 0);

public void abortNow() {
    try {
        _httpConnection.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
}
blue-sky
  • 51,962
  • 152
  • 427
  • 752

1 Answers1

1

There is no unique answer to your question. It depends on what the thread does. First of all, how do you plan to stop that thread?

  1. If it elaborates some stuff, and this stuff is made of "tokens" such as single "tasks" to do, and the thread grabs them from a BlockingQueue for example, then you can stop the thread with a so-called "poison pill", like a mock task that the thread reads as: "Hey man, I have to shut down".
  2. If the thread does something which cannot be "divided" into "steps" then you should actually tell us how do you think it can be shut down. If it blocks on read()s on a socket, you can only close the socket to unblock it.

Please note interrupt() is not meant to stop a Thread in normal circumstances, and if you interrupt() a thread, in most cases it'll just go on doing its stuff. In 99.9% of cases, it is just bad design to stop a thread with interrupt().

Anyway, to stop it after 5 seconds, just set a Timer which does so. Or better join it with a timeout of 5 seconds, and after that, stop it. Problem is "how" to stop it. So please tell me how do you think the thread should be stopped so that I can help you in better way.

Tell me what the thread does.

EDIT: in reply to your comment, just an example

class MyHTTPTransaction extends Thread {
    public MyHTTPTransaction(.....) {
        /* ... */
    }
    public void run() {
        /* do the stuff with the connection */
    }
    public void abortNow() {
        /* roughly close the connection ignoring exceptions */
    }
}

and then

MyHTTPTransaction thread = new MyHTTPTransaction(.....);
thread.start();
thread.join(5000);
/* check if the thread has completed or is still hanging. If it's hanging: */
    thread.abortNow();
/* else tell the user everything went fine */

:)

You can also set a Timer, or use Condition on Locks because I bet there can be some race condition.

Cheers.

gd1
  • 11,300
  • 7
  • 49
  • 88
  • 1
    The thread is sending an http post, which is run once. Problem is depending on network connectivity this post can take anywhere between 2 and 10 seconds. If it takes more than 5 seconds I should just stop. So I dont know how I can stop the thread running. Perhaps I can set a timeout on the http post. – blue-sky Sep 29 '11 at 10:19
  • 1
    Yeah, in that case you should try to set a timeout in the HTTP post. But I don't think there's a timeout if for example, the upload goes 1 byte / second. The timeout is for connection handshaking and inactivity, "slow activity" just goes on. So you can however set a `Timer`, which brutally shuts down the connection after 5 secs, causing your thread to stop as a side effect. – gd1 Sep 29 '11 at 10:19
  • How can I use Timer in this way ? do you mean using the cancel method on Timer after a period of time ? – blue-sky Sep 29 '11 at 10:29
  • I prefer joining the thread with a fixed timeout. However if you find it boring, just create a Timer that runs only once, and in the "task" procedure (the stuff which is run when the timer fires) you do something to close() the connection with the server. You will have to give the "task" a reference to some object that exposes some method to close the connection. I'll post some code... – gd1 Sep 29 '11 at 10:32
  • I;ve posted some sample code, problem im now facing is that since I can't wait for thread to die using join(millisecond) I have no way of knowing when to call the abortNow method ? – blue-sky Sep 29 '11 at 11:25
  • Why can't you wait using join(millisecond)? – gd1 Sep 29 '11 at 12:05
  • That method is not in j2me spec , just join() on a thread can be used – blue-sky Sep 29 '11 at 13:56
  • 1
    also it makes sense to drop `BlockingQueue`, `Condition` and `Lock` since j2me is based on Java 1.3 while concurrent util was introduced only in Java 5. By the way current Java Memory Model doesn't apply to j2me either - worth keeping in mind in case if you would want to use some `volatile` or `final` to synchronize – gnat Sep 29 '11 at 14:50
  • Please see my possible solution in original question and let me know what you think. Thanks – blue-sky Sep 29 '11 at 20:41