简体   繁体   English

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

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

I want to create long-running application for performing various tasks on different threads. 我想创建一个长时间运行的应用程序,以便在不同的线程上执行各种任务。 Each task should have one-minute timeout. 每个任务应具有一分钟的超时时间。 Here is my implementation: 这是我的实现:

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");

In general case it is working and throwing TimeoutExceptions, but sometimes it is doing nothing after even few hours. 在一般情况下,它正在工作并抛出TimeoutExceptions,但有时即使经过几个小时,它也无济于事。 So the questions is if Thread.join is reliable on Android? 所以问题是Thread.join在Android上是否可靠?

I have an idea to use Thread.wait and notify instead of that, what is the better way in your opinion? 我有一个使用Thread.wait并通知的想法,您认为更好的方法是什么?

I prefer doing all time base task using Timer and TimerTask . 我更喜欢使用TimerTimerTask完成所有时基任务。 Check the following code and probably this should be useful to you: 检查以下代码,这可能对您有用:

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

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

EDIT 编辑

I am giving a try at solving your problem. 我正在尝试解决您的问题。 I am using the code written by @amicngh as my base code and have done some modifications to it. 我将@amicngh编写的代码用作基本代码,并对它进行了一些修改。 I presume that after the TIMEOUT period you want to close the running thread. 我认为在TIMEOUT期过后,您想关闭正在运行的线程。 Check the following code runs fine and the explanation that follows: 检查以下代码是否运行正常,并进行以下解释:

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");

}

} }

The Thread API description says that it is unsafe to destroy or stop (hence both these method has been deprecated) and one of the way to stop a thread is to throw an exception. Thread API描述说destroystop是不安全的(因此这两个方法均已弃用),停止线程的一种方法是引发异常。 Hence I am checking for the Timeout inside the runner thread. 因此,我检查内部的超时runner线程。 Now about making the Main thread wait it is done by the 2 lines of code which uses synchronized to synchronize the access to the thread. 现在,让主线程等待是由两行代码完成的,这两行代码使用synchronized来同步对线程的访问。

Hope this code and explanation solves your problem. 希望此代码和说明能够解决您的问题。

Refer below program. 请参考以下程序。

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");

}

This program never ends that means your logic is incorrect. 该程序永远不会结束,这意味着您的逻辑不正确。

Better to use TimerTask . 最好使用TimerTask

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

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