I have one servlet class that is hitting a url exposed via rest. I have to call it continuously at a interval of 2 -3 minutes and keeping the response in a stack. I think, I have to do this with the help of thread mechanism.
What is best way to do this?
One option is use TimerTask: Timer shedule
TimerTask -- A task that can be scheduled for one-time or repeated execution by a Timer
Regards,
You could use a timer. Simply extend TimerTask and pass it to the Timer. If you want the task to start at application start up you can write a ServletContextListener to schedule the task.
Related
Upon an event, I'd like to run a task/runnable but delay it's execution in 2 seconds.
During these 2 seconds, if the same event occurs, I'd like to remove the previous task and re-post it to run - again delayed by 2 seconds.
An example scenario would be background compilation. When a file is saved, I'm waiting 2 seconds and start compiling the class and other, depending classes. I don't want to do it all the time - especially if there are editors that save files automatically, like IntelliJ IDEA.
So, how can I remove/postDelayed runnables in Java, like Android's Handler (remove / postDelayed)?
you can use the Executors.newScheduledThreadPool in order to schedule the task,
and you can follow this post's answer :
BlockingQueue<Runnable> queue = threadPool.getQueue();
in order to get the queued runnables.
To delay any task and "restart" the delay, you can use Swing timer and if it is running, use its method restart.
I suppose you could use the ScheduledExecutor to accomplish this, by adding a mechanism to replace queued tasks with re-queued ones. You could look into the source code of ScheduledThreadPoolExecutor to give an idea, perhaps overriding the implementation of ScheduledFuture<?> schedule( Runnable command, long delay, TimeUnit unit ) and figuring out to avoid the scheduling, only running the tasks once.
Cheers,
I use the Java's Timer to schedule a task to run after some interval of time.
myTimer.schedule(myTask, delayTime);
At any point in time, is it possible to check if there is any task scheduled to be run (but has not run yet)? If so, how would I do that?
If not, what is the alternative(s) to Timer do I have?
You can (and should) use ScheduledExecutorService instead of Timer.
It handles thread crashes in a robust manner and has more flexible API
You can just add a boolean field to myTask's class, which will be set true at first execute.
Keep it simple.
I'm basically scheduling a task to run every ten seconds, but it seems like it runs every 8 or 9. I use the function schedule (task, 0 , 10000);
I'd say it's pretty accurate. In my app(Tap Counter) I use a timer to start a task, and a seperate timing system to display a countdown, and they sync perfectly.
Feel free to check out my app =)
try the Alarm Service to schedule a task.
Hope this links will be helpful http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
How can I schedule multiple tasks using java.util.Timer. I want to read multiple files using timers. I think I have to give each file a different TimerTask so that one file gets one instance of TimerTask and other file gets another, but I don't know how to do it. Please help. Thanks in advance. Here is what I'm doing:
Timer timer = new Timer();
// repeat the check every second
timer.schedule(fileWatcherTask, new Date(), 1000);
As javadoc of Timer class indicates your tasks should take very few time. In this case you can forget about time clash. If your tasks take more then 0.1 seconds run them in separate thread. I mean use Timer as a trigger that just makes task to start in separate thread.
you can also use quartz scheduler for that refer http://www.mkyong.com/java/quartz-scheduler-example/
if you want to use timer class see the example in following image
refer link for more details
I want to run a command on every 3 seconds in android. I have an application in android which is using a thread. Now in that thread i need to invoke a method but every 3 seconds. i.e when the thread starts
1
2
3 runCommand()
4
5
6 runCommand() and so on
I dont want to use any other thread or runnable nor I want to use Timer for that. What I can use is SystemClock.utilsmillis or System.nanoTime() and a couple of variables, something like that. Kindly help
Thanks
FAS
Android has the Handler mechanism, which is similar to a Timer, but it does not have its own thread, and gets called from the main event loop instead.
But if you use that, do not place code in there that will take a lot of time (or block), as that would hang the whole app.
(Actually, it sounds like you already have another thread for your, you could create a Looper for it, and run the Handler off that one so as to not block the main thread).