如何在一段时间后重复运行一个线程

时间:2021-04-27 02:14:48

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.

我想运行一个线程(在后台执行一些耗时的任务并且不更新UI)它只是从Internet上下载一些文件,它独立于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?

我需要使用Handler来实现这个吗?

3 个解决方案

#1


18  

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.

还有更多选择。我从未尝试过选项3和4.它只是出现在我的脑海里并且我写了。如果我是你,我会使用1或2中的任何一个。

#2


7  

Prefered choice is

首选是

java.util.concurrent.ScheduledExecutorService

Newer and robust implementation, More here ScheduledExecutorService

更新更强大的实现,更多这里ScheduledExecutorService

#3


4  

I would use a Timer to achieve this. Try this:

我会用Timer来实现这个目的。试试这个:

void onCreate()
{
    Timer t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            // Download your stuff
        }

    }, 0, 1000);
}

It starts immediately and the run-Method gets called every second.

它立即启动,每秒调用run-Method。

#1


18  

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.

还有更多选择。我从未尝试过选项3和4.它只是出现在我的脑海里并且我写了。如果我是你,我会使用1或2中的任何一个。

#2


7  

Prefered choice is

首选是

java.util.concurrent.ScheduledExecutorService

Newer and robust implementation, More here ScheduledExecutorService

更新更强大的实现,更多这里ScheduledExecutorService

#3


4  

I would use a Timer to achieve this. Try this:

我会用Timer来实现这个目的。试试这个:

void onCreate()
{
    Timer t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            // Download your stuff
        }

    }, 0, 1000);
}

It starts immediately and the run-Method gets called every second.

它立即启动,每秒调用run-Method。