简体   繁体   中英

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. So the questions is if Thread.join is reliable on Android?

I have an idea to use Thread.wait and notify instead of that, what is the better way in your opinion?

I prefer doing all time base task using Timer and TimerTask . 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. I presume that after the TIMEOUT period you want to close the running thread. 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. Hence I am checking for the Timeout inside the runner thread. 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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