With reference to Java Timer Class or ScheduledExecutorService interface ,Can I set a scheduler(or timer) inside run method (or TimerTask) of the executor thread(other scheduler)?
Case Study:
I have a database containing a list of songs(10,000) and schedule time to play the song.
So I thought of creating a scheduler(say 1)(of period 1 hour) which will search the database and create scheduler for all songs which are scheduled to be played within one hour.
After one hour the scheduler1 will delete all the threads and again search the database and create scheduler for other threads.
Is it a good idea?Possible to create?
Or should I create 10000 scheduler at once?
In this case which one will be better timer or scheduler?
Why not just call ScheduledExecutorService.scheduleAtFixedRate or ScheduledExecutorService.scheduleWithFixedDelay?
UPDATE
This is one means of implementing what (I believe) you want:
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
void start(final Connection conn)
{
executor.scheduleWithFixedDelay(new Runnable(){ public void run(){ try { poll(conn); } catch (Exception e) { e.printStackTrace(); } } }, 0, 1, TimeUnit.HOURS);
}
private void poll(Connection conn) throws SQLException
{
final ResultSet results = conn.createStatement().executeQuery("SELECT song, playtime FROM schedule WHERE playtime > GETDATE() AND playtime < GETDATE() + 1 HOUR");
while (results.next())
{
final String song = results.getString("song");
final Time time = results.getTime("playtime");
executor.schedule(new Runnable(){ public void run() { play(song); } }, time.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
}
Related
I have a requirement in which, I will plan multiple tasks for certain interval. And for each task a notification has to be pushed at certain interval of time.
I have two approaches, Either to write a single scheduler which will poll at each one minute and push the notification accordingly. Or I can schedule initialize a scheduler for each task. With the former method the solution is pretty much simple, with the later I can get more control on the scheduler, for instance I can put initial delay for each task specifically(Which is a requirement), then stop an individual task, resume etc. So far I am proceeding with the latter method. But I would like to know If it could be better to use so many scheduler in a single application. Or is it better to go with a single scheduler with 1 minute polling?. On an average I will have about 200+ tasks live at a time. Or for this can I depend on any other library?
So far my code
Sheduler which is an ExecutorService
//Constructor
public TaskScheduler(String taskName) {
this.taskName = taskName;
this.taskResult = new TaskResult();
this.taskResult.setStartTime(getNewDate());
scheduledExecutorService = Executors.newScheduledThreadPool(1);
//DB Operation
}
// To stop an individual task
public TaskResult stop() throws InterruptedException {
try {
System.out.println("Stopping : " + this.taskName);
this.taskResult.setTaskName(this.taskName);
this.taskResult.setEndTime(new Date());
scheduledFuture.cancel(false);
scheduledExecutorService.shutdown();
//DB Operation
System.out.println("Stopping : finished - " + this.taskName + " # "+ new Date());
} catch (Exception e) {
e.printStackTrace();
}
return this.taskResult;
}
//Portion to add task
public TaskScheduler schedule(Runnable task, long initialDelay, long frequency) throws Exception{
this.taskResult.setFrequencyInSeconds(frequency);
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(task, initialDelay, frequency, TimeUnit.SECONDS);
return this;
}
Task thread with business logic
public class TaskModel implements Runnable {
private String taskName;
public TaskModel() {
}
public TaskModel(String taskName) {
this.taskName = taskName;
}
#Override
public void run() {
// DB operations
.
.
It is better to use a single scheduler, but you don't need to write your own scheduler for this. You could use single instance Executors.newScheduledThreadPool(1) with some number of threads to schedule all your tasks.
Think about the following code:
class TaskScheduler {
private ScheduledExecutorService scheduledExecutorService;
public TaskScheduler(int threads) {
this.scheduledExecutorService = Executors.newScheduledThreadPool(threads);
}
//Portion to add task
public TaskExecutionContext schedule(String taskName, Runnable task, long initialDelay, long frequency) {
TaskExecutionContext context = new TaskExecutionContext(taskName);
context.getTaskResult().setFrequencyInSeconds(frequency);
ScheduledFuture scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(task, initialDelay, frequency, TimeUnit.SECONDS);
context.setScheduledFuture(scheduledFuture);
return context;
}
}
class TaskExecutionContext {
private String taskName;
private TaskResult taskResult;
private ScheduledFuture scheduledFuture;
public TaskExecutionContext(String taskName) {
this.taskName = taskName;
this.taskResult = new TaskResult();
this.taskResult.setTaskName(taskName);
this.taskResult.setStartTime(new Date());
//DB Operation on creation
}
public TaskResult stop() {
try {
System.out.println("Stopping : " + this.taskName);
this.taskResult.setTaskName(this.taskName);
this.taskResult.setEndTime(new Date());
scheduledFuture.cancel(false);
//DB Operation on stopping
System.out.println("Stopping : finished - " + this.taskName + " # " + new Date());
} catch (Exception e) {
e.printStackTrace();
}
return this.taskResult;
}
public TaskResult getTaskResult() {
return this.taskResult;
}
public void setScheduledFuture(ScheduledFuture scheduledFuture) {
this.scheduledFuture = scheduledFuture;
}
}
If you need some extra operations on scheduling, creation and stopping tasks - probably, it's better to have separate TaskExecutionContext, which will perform all your needs.
Create a context when you need to schedule a task and pass it to scheduler.
I want to run a task at every particular interval of time regardless of completion of previous thread. And I've used ScheduledExecutorService with the schedule time at every one second. But the problem is, in my Runnable, If I make thread to sleep for 5 seconds, My ScheduledExecuterService also getting executed in every 5 seconds while it supposed to run each thread at 1 second.
It seems like it ScheduledExecuterService is waiting for previous thread to completion. But I want, The task to be triggered at every 1 second no matter what if job inside the task waits for longer time.
Here's is my code.
public class MyTask implements Runnable {
public void run() {
System.out.println("hi there at: "+ new java.util.Date());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
And here's my ScheduledExecutorService Code.
public class JavaScheduledExecutorServiceExample {
public static void main(String[] args) {
ScheduledExecutorService execService = Executors.newScheduledThreadPool(5);
execService.scheduleAtFixedRate(new MyTask(), 0, 1000, TimeUnit.MILLISECONDS);
}
}
Correct me If I'm doing something wrong. And If I'm wrong, is there any alternative to achieve the same? Providing Any best practices could be more helpful :)
"If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute." The behavior you are seeing is consistent with the javadocs
I believe this will perform the way you specified:
public class JavaScheduledExecutorServiceExample {
private static ScheduledExecutorService execService = null;
private static int timesAsleep = 0;
public static class MyTask implements Runnable {
public void run() {
System.out.println("hi there at: "+ new java.util.Date());
// schedule again
execService.schedule(new MyTask(), 1000, TimeUnit.MILLISECONDS);
try {
int i = timesAsleep;
timesAsleep++;
System.out.println("asleep " + i + "----------------------");
Thread.sleep(5000);
System.out.println("awoke " + i + "----------------------");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
execService = Executors.newScheduledThreadPool(5);
execService.schedule(new MyTask(), 1000, TimeUnit.MILLISECONDS);
}
}
Notice the use schedule() instead of scheduleAtFixedRate() on the ScheduledExecutorService instance. It also schedules the next task as soon as it starts the new task.
I have some type of task for example in a loop with same method but different arguments,
I need to execute these tasks in one after another in some intervals,
and all this activity need to be execute in again and again in a particular schedule,
e.g. let say I have a method called
public void GetData(String tablename){
}
so first time I will provide table1 then table2 then table3.....
similar to for loop but need some interval in between,
and same above all execution need to execute in each 10 min,
sample code I have implemented as
final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
final Runnable runner = new Runnable() {
public void run() {
getData(String table);
}
};
final ScheduledFuture<?> taskHandle =
scheduler.scheduleAtFixedRate(runner, 1, 10, SECONDS);
its working fine for one table for need help and best way to implement for multiple tables.
tryjava.util.Timer to schedule a task to execute
public class ReminderBeep {
Toolkit toolkit;
Timer timer;
public ReminderBeep(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(), seconds * 1000);
}
class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
toolkit.beep();
//timer.cancel(); //Not necessary because we call System.exit
System.exit(0); //Stops the AWT thread (and everything else)
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
new ReminderBeep(5);
System.out.println("Task scheduled.");
}
}
You can just use Thread.sleep() in your loop (if it's not on the main thread). Something like this (the code is not tested so may contain errors):
ExecutorService executorService = Executors.newFixedThreadPool(4);
for (int i = 0; i < 10; i++) {
executorService.execute(new Runnable() {
public void run() {
getData(String table);
}
Thread.sleep(10000);
}
I am writing a code in Java, that has three main methods which are called in succession. Today, my prof, told me to improve the code by setting a frequency to code which means it that there should be a specific time allocated for each method. If it is done before that time, it should wait for that much time and then call should go to next method.
For example: If the first method has time of 3 seconds, for three seconds it should wait there even if it is done, then move to next method sense.
I am already using a timer in my code to slow down the movement as it is a GUI. I was planning to use another timer, but I feel it will only slow it down. How can I fix the period for that method for a specific time say 3 seconds.
Please let me know if you have any ideas.
If you cannot sleep on your own thread, run the task on another thread. A single executor thread will do nicely, to which you can submit tasks to be executed. Sleeping in the executor thread should not impact the GUI thread.
private Executor executor = Executors.newFixedThreadPool(1);
public void method1() {
System.out.println("Method 1 called");
}
public void method2() {
System.out.println("Method 2 called");
}
public void method3() {
System.out.print("Method 3 called");
}
public void start() {
executor.execute(new Runnable() {
private final long WAIT_TIME = 3000; // ms
#Override
public void run() {
long startTime = System.currentTimeMillis();
method1();
sleep(startTime);
startTime = System.currentTimeMillis();
method2();
sleep(startTime);
startTime = System.currentTimeMillis();
method3();
sleep(startTime);
}
private void sleep(long startTime) {
long timeTaken = System.currentTimeMillis() - startTime;
long timeLeft = WAIT_TIME - timeTaken;
if(timeLeft < 0) return;
try {
Thread.sleep(timeLeft);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
I'm a newbie in Java and I haven't work with threads in past. Now I have a really tricky issue for my level of knowledge.
I have developed two functionalities the "Send mail" and the "Check mail". Each user register his preferences and the system creates two catalogues with many instructions for periodic work like
A) send message every 1500 msec
B) send message every 1800 msec
C) send message every 3000 msec
And
A) check message every 2000 msec
B) check message every 6000 msec
C) check message every 8000 msec
I tried many ways with threads but I fail to do it work.
My question is which is the best way to synchronize them? Below is code from my last effort using threads.
public class MailCreatorThread extends Thread {
#Override
public void run() {
CreateMail(_date); //creates a mail with _date as subject
}
}
public class GPSThread extends Thread {
#Override
public void run() {
// TODO Auto-generated method stub
while (!_isTimeToStop) {
try {
while (_servicesToUpdate.size() == 0) {
Thread.sleep(500);
}
_currentService = (MyService) _servicesToUpdate.get(0)
.clone();
_servicesToUpdate.remove(0);
MailCreatorThread mailCreatorThread = new MailCreatorThread();
mailCreatorThread.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class CheckServicesThread extends Thread {
#Override
public void run() {
// TODO Auto-generated method stub
while (!_isTimeToStop) {
try {
Thread.sleep(1000);
for (int j = 0; j < _servicesList.length; j++) {
MyService currentService = ((MyService) _servicesList[j]);
if (myService.getTimeToNextUpdate() - 1000 <= 0) {
_servicesToUpdate
.add((MyService) currentService
.clone());
currentService
.setTimeToNextUpdate(currentService
.getUpdatePeriod());
} else {
currentService
.setTimeToNextUpdate(currentService
.getTimeToNextUpdate() - 1000);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopSelf();
}
}
You can use a ScheduledExecutorService to run thos periodic tasks. The syntax is fairly straightforward:
Runnable check = new Runnable() {
public void run() {
checkMessage();
}
}
Runnable send = new Runnable() {
public void run() {
sendMessage();
}
}
//since what you are doing is mostly I/O you probably want to have
//more than one thread available so that if one operation blocks,
//the other ones can be launched in parallel
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
scheduler.scheduleAtFixedRate(check, 0, 1500, MILLISECONDS);
scheduler.scheduleAtFixedRate(send, 0, 6000, MILLISECONDS);
Note: Timer mentioned in Ozzy's answer should not be used any more as it has been improved in Java 1.5 by ScheduledThreadPoolExecutor, as explained in Timer's javadoc :
Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.
In java you have built-in Timer and TimerTask classes to help you repeat a task in a separate thread.
This will create a Timer which will create its own background Thread:
Timer t = new Timer();
You can then schedule as many tasks as you want to that timer, they will share the timer's own thread.
This is how you can schedule a single task on the timer thread, after a delay of 2000ms = 2s:
t.schedule(new TimerTask() {
#Override
public void run() {
//task to perform
}
}, 2000);
This is how you can schedule a repeated task on the timer thread, after a delay of 1s, and repeat the task at 1.5s intervals:
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//task to perform every 1.5 seconds
}
}, 1000, 1500);
Now you have a choice to schedule both tasks (checkMail, sendMail) to the same Timer (same thread) or give them each their own Timer (separate threads).
For more info, refer to the java docs (http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html)
Hope this helps.