hguser
hguser

Reputation: 36068

How to execute a certain method after a time delay?

I have a class like this Render:

public class Render {
    private Timer timer;
    private TimerTask timerTask;


    @Override
    public void refresh() {
        if (timerTask != null)
            timerTask.cancel();
        timerTask = new LoadTask();
        timer.schedule(timerTask, 1000);
    }

    private class LoadTask extends TimerTask {
        @Override
        public void run() {
            //request the server
        }
    }
}

The refresh method may be called very frequently, but the job to be done inside the refresh is a little heavy, it have to request something from the server, so I tried to make this method execute after a delay.

But as shown, a new LoadTask instance will be created once this method got called, is this a waste of memory in android?

So I wonder if any idea to fix it?

Upvotes: 1

Views: 6949

Answers (3)

carlosvin
carlosvin

Reputation: 979

You can use the java Executors, included in version 7. You reuse the thread instance and the LoadTask instance.

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class Render {

    private final LoadTask yourDelayedTask;
    private ScheduledExecutorService scheduler;
    private static final long DELAY_MS = 1000;

    public Render() {
        scheduler = Executors.newScheduledThreadPool(1);
        yourDelayedTask = new LoadTask();
    }

    private ScheduledFuture<?> lastScheduledTask;

    public void refresh() {
        if (lastScheduledTask != null && !lastScheduledTask.isCancelled() || !lastScheduledTask.isDone()) { // Review this logic
            lastScheduledTask.cancel(true);
        }
        lastScheduledTask = scheduler.schedule(yourDelayedTask, DELAY_MS, TimeUnit.MILLISECONDS);

    }

    private class LoadTask implements Runnable {
        @Override
        public void run() {
            // request the server
        }
    }
}

Upvotes: 0

Aryan
Aryan

Reputation: 1877

Why dont you just initialise the TimerTask in the constructor of Render Class. Like this

public class Render {
    private Timer timer;
    private TimerTask timerTask;

    public Render()
    {
        timerTask = new LoadTask();         
    }

    //....

}

And then just use the reference in Refresh function. This should solve your problem :)

Upvotes: 0

Naveen Kumar Kuppan
Naveen Kumar Kuppan

Reputation: 1442

You will try this one to execute the task with the delay

    Handler handler = new Handler();
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                //Your task it will execute at 1 time only...

                }
            }, 5000);//5 seconds delay and you can change the delay  time...

It will execute the thread at ever 10 seconds like as a loop function...

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

            @Override
            public void run() {
                //Your logic it will repeating you task ever 10 seconds....

            }
        }, 5000, 5000);

Upvotes: 2

Related Questions