Questions tagged [scheduledexecutorservice]

A system library object that manages its own thread pool and can schedule actions to run on these threads (after a given delay or periodically).

419 questions
128
votes
7 answers

scheduleAtFixedRate vs scheduleWithFixedDelay

What's the main difference between scheduleAtFixedRate and scheduleWithFixedDelay methods of ScheduledExecutorService? scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { …
Sawyer
  • 14,431
  • 26
  • 79
  • 119
99
votes
5 answers

How to run a background task in a servlet based web application?

I'm using Java and I want to keep a servlet continuously running in my application, but I'm not getting how to do it. My servlet has a method which gives counts of the user from a database on a daily basis as well as the total count of the users…
95
votes
12 answers

How to run certain task every day at a particular time using ScheduledExecutorService?

I am trying to run a certain task everyday at 5 AM in the morning. So I decided to use ScheduledExecutorService for this but so far I have seen examples which shows how to run task every few minutes. And I am not able to find any example which…
AKIWEB
  • 16,538
  • 58
  • 164
  • 276
68
votes
2 answers

How to remove a task from ScheduledExecutorService?

I have a ScheduledExecutorService that times a few different task periodically with scheduleAtFixedRate(Runnable, INIT_DELAY, ACTION_DELAY, TimeUnit.SECONDS); I also have a different Runnable that I'm using with this scheduler. the problem starts…
thepoosh
  • 12,193
  • 14
  • 68
  • 129
27
votes
4 answers

Thread.sleep() VS Executor.scheduleWithFixedDelay()

Goal: Execute certain code every once in a while. Question: In terms of performance, is there a significant difference between: while(true) { execute(); Thread.sleep(10 * 1000); } and executor.scheduleWithFixedDelay(runnableWithoutSleep, 0,…
26
votes
1 answer

Where do I create and use ScheduledThreadPoolExecutor, TimerTask, or Handler?

I need to make my RSS Feed reader check the feed every 10 minutes for new posts, and then parse them if there are new ones. I also need to update the UI about every minute. I have read and heard different things from various sources. My current…
zr00
  • 518
  • 1
  • 7
  • 19
25
votes
2 answers

How do I schedule a task to run once?

I want to delay doing something, along the lines of setting a countdown timer that will "do a thing" after a certain amount of time. I want the rest of my program to keep running while I wait, so I tried making my own Thread that contained a…
azurefrog
  • 9,994
  • 7
  • 36
  • 51
13
votes
4 answers

ScheduledExecutorService or ScheduledThreadPoolExecutor

I'm building an Android App which have to periodically do something in a Service. And I found that using ScheduledThreadPoolExecutor and ScheduledExecutorService is preferable to Timer. Can anyone explain the difference between…
changbenny
  • 358
  • 6
  • 17
12
votes
1 answer

How do I change the rate or period of a repeating task using ScheduledExecutorService?

I have a modified version of the bluetooth chat sample app. I have set up a ScheduledExecutorService which sends a command over bluetooth at a predefined rate using scheduleAtFixedRate. I have set up a PreferenceActivity to allow the period to be…
12
votes
1 answer

ScheduledExecutorService and uncaught Error

A discussion in Code Review chat determined the following behaviour from a ScheduledExecutorService: A task scheduled to run fails with a 'serious' problem, but there's no report, exception, or log of the problem. In other contexts, the application…
rolfl
  • 16,851
  • 6
  • 38
  • 72
10
votes
1 answer

Java "scheduleAtFixedRate" alternative solution?

I have a Java application that is used to communicate with an embedded device over a UART connection (RS422). The host queries the microcontroller for data in 5 millisecond intervals. Up until recently I've been using ScheduledExecutorService…
Ben
  • 1,092
  • 1
  • 14
  • 33
10
votes
4 answers

Testing code which uses ScheduledExecutorService (without using Sleep)

I have a validation object which runs input through a series of checks. If an input fails any of the checks, the validation ends. Inputs which pass all the checks get grouped based on a sliding time window. This window kicks off when the first piece…
9
votes
2 answers

Why is java ExecutorService newSingleThreadExecutor spawning two threads?

I have a sample java code below which if run as a console application behaves as I expected ( spawning a single thread to execute the runnable). The strange behavior (spawning two threads - sample below) I see is when I run this sample as a service…
7
votes
1 answer

Resubmitting/scheduling task from the task itself - is it a good practice?

Consider we have a scheduled executor service: ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(...); And for some logic we want to retry a task execution. The following approach seems to be smelling for me, but I can't…
7
votes
1 answer

Which is Better ScheduledExecutorService or AlarmManager in android?

I am a beginner and I am developing an android application which will keep on sending SMS to the user after a certain delay (which is in days).I want that the user once registered should receive the SMS irrespective of the fact that he is logged in…
R_2293
  • 101
  • 1
  • 12
1
2 3
27 28