Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a thread repeatedly after some interval

I want to run a thread (Which does some time consuming task in background and does NOT update UI) it just downloads some files form the internet and it is independent from the UI.

I want to run this thread repeatedly after some time interval.

How can i do this, I have thread something like below:

boolean mResult =false;

void onCreate()
{
    DownloadThread mDownloadThread = new DownloadThread();
    mDownloadThread.start();
}

class DownloadThread extends Thread implements Runnable
{
    public void run() 
    {
       // My download code 
       mResult  = result;
    }
}

Do i need to use Handler for implementing this?

like image 833
User7723337 Avatar asked Mar 14 '13 10:03

User7723337


People also ask

How do I make one thread run after another?

By using join you can ensure running of a thread one after another.

Is there a way to keep a thread from executing until another thread is finished running?

This can be achieved by calling the join() function on the threading. Thread instance for the new thread. This call will block, meaning that the main thread will not carry on executing until this function call returns, and this function call will only return once the thread that has been joined terminates.

What are some ways to prevent threads from running forever?

The only way to stop an arbitrary thread is by interrupting it. Keep a reference to it then call the interrupt method. Save this answer.

Can make thread to go from running to waiting state?

when sleep() is called on thread it goes from running to waiting state and can return to runnable state when sleep time is up. b. sleep() is a static method, causes the currently executing thread to sleep for the specified number of milliseconds.


2 Answers

Option 1:

volatile boolean flag = true;

public void run() 
{
    while(flag)
    {    
       // Do your task
        try{
            Thread.Sleep(interval);
        } catch(Exception e){

        }

    }
}

Option 2:

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
        // Do your task
    }

}, 0, interval);

Option 3:

volatile boolean flag = true;

public void someMethod(){
     // Do your task
     try{
         Thread.Sleep(interval);
     } catch(Exception e){

     }
     if(flag)
        return;
     else
        someMethod();     
}

Option 4:

final Handler handler = new Handler();
volatile boolean flag = true;

Class A implements Runnable{
    public void run(){
        // Do your Task
    }
    if(!flag)
       handler.postDelayed(a, interval);
}

A a = new A();

handler.postDelayed(a);

There will be many more options. I never tried option 3 and 4. It just came to my mind and I wrote. If I were you I would use any of 1 or 2.

like image 98
stinepike Avatar answered Sep 23 '22 19:09

stinepike


Prefered choice is

java.util.concurrent.ScheduledExecutorService

Newer and robust implementation, More here ScheduledExecutorService

like image 34
ajduke Avatar answered Sep 26 '22 19:09

ajduke