简体   繁体   English

一段时间后停止线程

[英]Stop a thread after period of time

How can I stop a thread running after it has been running for 5 seconds ? 在运行5秒钟后如何停止运行线程?

I am unable to use java 5+ threading features (must work in j2me). 我无法使用Java 5+线程功能(必须在j2me中工作)。


Possible solution - 可能的解决方案-

Schedule two threads. 安排两个线程。 One which performs the actual work(T1) and the other which acts as monitor of T1(T2). 一个执行实际工作(T1),另一个充当T1(T2)的监视器。

Once T1 has started then start T2. 一旦T1开始,则启动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. T2每秒在T1上调用isalive()方法,如果10秒钟后T2没有死亡,则T2在T1上调用中止,这将杀死T1线程。

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();
    }
}

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". 如果它详细说明了某些东西,而这些东西是由诸如要执行的单个“任务”之类的“令牌”组成的,并且该线程从例如BlockingQueue抓取了它们,那么您可以使用所谓的“毒药”来停止线程,就像线程读取的模拟任务一样:“嘿,我必须关闭”。
  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. 如果它在套接字的read()上阻塞,则只能关闭该套接字才能解除阻塞。

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. 请注意, interrupt()在正常情况下并不意味着要停止Thread ,并且如果您interrupt()一个线程,则在大多数情况下它只会继续执行其工作。 In 99.9% of cases, it is just bad design to stop a thread with interrupt() . 在99.9%的情况下,使用interrupt()停止线程是不好的设计。

Anyway, to stop it after 5 seconds, just set a Timer which does so. 无论如何,要在5秒钟后停止它,只需设置一个Timer Or better join it with a timeout of 5 seconds, and after that, stop it. 或者最好以5秒的超时时间join它,然后将其停止。 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 Lock s because I bet there can be some race condition. 您还可以设置Timer ,或在Lock上使用Condition ,因为我敢打赌,可能存在一些比赛条件。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM