繁体   English   中英

Thread.join()在Android中不起作用?

[英]Thread.join() not working in Android?

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

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并通知的想法,您认为更好的方法是什么?

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

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描述说destroystop是不安全的(因此这两个方法均已弃用),停止线程的一种方法是引发异常。 因此,我检查内部的超时runner线程。 现在,让主线程等待是由两行代码完成的,这两行代码使用synchronized来同步对线程的访问。

希望此代码和说明能够解决您的问题。

请参考以下程序。

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

暂无
暂无

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

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