1

我想创建长时间运行的应用程序来在不同的线程上执行各种任务。每个任务应该有一分钟的超时时间。这是我的实现:

runner = new Thread(new Runnable() { 
   @Override
   public void run() {  }
       // some actions here
});
runner.start();
startJoin = System.currentTimeMillis();            
runner.join(TIMEOUT);    
stopJoin = System.currentTimeMillis();

if ((stopJoin - startJoin) >= TIMEOUT)
            throw new TimeoutException("Timeout when reading the response from   process");

在一般情况下,它正在工作并抛出 TimeoutExceptions,但有时甚至几个小时后它什么也不做。所以问题是 Thread.join 在 Android 上是否可靠?

我有一个使用 Thread.wait 和 notify 的想法,你认为更好的方法是什么?

4

2 回答 2

0

参考下面的程序。

public static void main(String[] args) throws InterruptedException {
    long TIMEOUT=100;
    Thread runner = new Thread(new Runnable() {
           @Override
           public void run() {

               for(;;){
                   System.out.println("running ");
               }

           }
               // some actions here
        });

    runner.start();
    long startJoin = System.currentTimeMillis();
    runner.join(TIMEOUT);
    long stopJoin = System.currentTimeMillis();

    if ((stopJoin - startJoin) >= TIMEOUT)
        try {
            throw new Exception("Timeout when reading the response from   process");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        System.out.println("Running Thread");

}

该程序永远不会结束,这意味着您的逻辑不正确。

更好用TimerTask

于 2012-06-13T09:29:53.793 回答
0

Timer我更喜欢使用and来完成所有时基任务TimerTask。检查以下代码,这可能对您有用:

Timer t =new Timer();
    t.schedule(new TimerTask() {

        @Override
        public void run() {
            //The task you want to perform after the timeout period 
        }
    }, TIMEOUT);

编辑

我正在尝试解决您的问题。我正在使用@amicngh 编写的代码作为我的基本代码,并对其进行了一些修改。我想在TIMEOUT你想关闭正在运行的线程之后。检查以下代码是否正常运行以及以下说明:

public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
    final long TIMEOUT=100;
    final long startJoin = System.currentTimeMillis();

    Thread runner = new Thread(new Runnable() {
        long stopJoin;
           @Override
           public void run() {
               try{
                   for(;;){
                       System.out.println("running "); 
                       stopJoin = System.currentTimeMillis();

                       if ((stopJoin - startJoin) >= TIMEOUT){
                         throw new Exception();  
                       }
                   }
               }
               catch (Exception e) {
                // TODO: handle exception
               }

           }
               // some actions here
        });

    runner.start();

    synchronized (ThreadTest.class) {
        ThreadTest.class.wait(TIMEOUT);
    }


    /*if ((stopJoin - startJoin) >= TIMEOUT)
        try {
            throw new Exception("Timeout when reading the response from   process");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/


        System.out.println("Running Thread");

}

}

Thread API 描述说它是不安全的destroyor stop(因此这两种方法都已被弃用)并且停止线程的方法之一是抛出异常。因此,我正在检查runner线程内的超时。现在关于让主线程等待它是由synchronized用于同步对线程的访问的 2 行代码完成的。

希望这段代码和解释能解决你的问题。

于 2012-06-13T09:17:29.487 回答