Edward
Edward

Reputation: 139

Timer task is misbehaving in service

I am using timer task to execute asynctask at fixed interval of time the time five minutes. but timer task sometimes started to misbehave like it doesn't get at fixed delay in fact it get executed after 10 minutes twice instead of getting every 5 minute why so?

Here is my code

    Timer timer = new Timer();
            mytimer Mytimer = new mytimer();

            timer.scheduleAtFixedRate(Mytimer, 300000, 300000);


class Mytimer extends TimerTask {
        @Override
        public void run() {
            // TODO Auto-generated method stub

            new DetailPosition().execute();
        }
    }

So help me in this

Upvotes: 0

Views: 93

Answers (1)

George Simms
George Simms

Reputation: 4060

If the timer task is scheduled on a thread pool where all the threads are in use, then it will have to wait for one to become available before running your task.

Consider the following -

 ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();    
ses.scheduleAtFixedRate(() -> {Thread.sleep(500); System.out.println(new Date());}, 0, 200, TimeUnit.MILLISECONDS);

Here the task takes longer than the interval, so the tasks will just queue up until you run out of memory.

Upvotes: 2

Related Questions