Hi all and thanks for reading this.
i have an application and i want to use multiple timers on it.
it will read from a list of instructions some instructions and will tell you what to do, keeping track of time for each instruction's time and notifying you when timer is over. instruction can include other instructions and goes on.
i m passing the time as : double minutes to each instruction.
So it should work like this.
start instruction1 - start timer1
if it includes other, start instruction1.1 - start timer1.1
start instruction2 - start timer2 .....etc.etc..
every time a timer goes of it should notify me
i would like to get suggestions on how to do this....
any tutorials, examples, links pseudocode, sample code are welcome. Thank you
Introducing a simple generalisation of the Java language's Timer class
How to Use Timers
Job Scheduling using Timer and Quartz
Related
I am writing to see what everyone's way of doing this is.
Example:
I have multiple timers with a start time of potentially anything. E.G, One timer could be 30 seconds, the second could be 10 minutes, the third could be 3mins 32seconds, and so on... You can create infinite timer countdowns.
Now, the question is... What's the best way to detect when a timer hits 0? Java does have a runnable class and I was going to run an async task to check every second but this sounds really inefficient to me.
Are there any other ways to do it? Thanks!
In my server and client main loops, I have custom timers. They're pretty basic things. I'm using System.currentTimeMillis() to get the milliseconds and then comparing it to different variables for different timers. If the timer variable is less than tickCount, it runs the code, then sets the timer to tickCount + UpdateTime.
Here is an example:
long tickCount = System.currentTimeMillis();
if (LastUpdateTime_WoodCutting < tickCount) {
woodcutting();
LastUpdateTime_WoodCutting = tickCount + UpdateTime_WoodCutting;
}
UpdateTime_WoodCutting is set to 10. In theory, this should update this timer every 10ms. I'm sure it's not exactly that accurate, but the problem I'm having is, overall, this timer is meant to be a 10 second timer, which would be 10000ms.
The timer seems to be taking anywhere from 20-30 seconds to get there. The woodcutting method just checks if the timer in the player class is less than 10000, and if so, it adds 10 to it and once it's at 10000 or more, it executes the code for cutting down a tree in the game.
Another problem is that the client uses the exact same code for timers as the server, yet even while running on the same machine, they do not line up. The client's timer seems to finish about halfway through the server's timer. I've tried a bunch of alternatives to System.currentTimeMillis() but they all pretty much work exactly the same, so it hasn't been very fruitful.
Basically, what I'm trying to figure out is how I should handle these timers. It doesn't appear that I'm handling them properly. Before a bunch of changes to my code, these timers worked flawlessly, but all of a sudden, they do not. I don't know if it is the result of updating Gradle or Java (from 1.7 to 1.8), but I'm very frustrated with this and it's a pretty game breaking issue.
My source code is easily almost 40k lines of code and I am unable to share all of my code, but anything someone may need to see in order to better help me with this, I will provide what I can.
How can you implement some kind of timer, to know how much time did user spend for one round of a game?
Getting System.currentTimeMillis will get your system time to the millisecond. If you want to do a Swing event intermittently, such as displaying the current time in a stop watch or a simple animation, then a Swing Timer would work well.
One can easily find the time spent by a user on one round of a game by using System.nanoTime() function. However if you want to implement in a game, you would probably like the timer to also be displayed to the user, you can you use multi-threading for the same.
I have next task: I need to load the same file into my web app several times, for example - twice a day! Suppose in that file I have information, that changes, and I need to load this info into my app to change the statistics for example.
How can I load file several times (twice an hour, or twice a day)?
What should I use? Is any algorithm to do that?
I am not allowed to use external libraries like Quartz Scheduler. So I need to do it with Thread and/or Timer. Can anybody give me some example or algorithm how to do it. Where can I create the entry point to my Thread, can I do it in managed bean or I need some sort of filter/listener/servlet. I works with jsf and richFaces. Maybe in this technologies there are some algorithms to solve my problem.
Any ideas?
Thanks very much for help!
If you cannot use a scheduler, then use a servlet and Timer.
In this article it is described how to do that. It's exactly what you need.
Check java.util.Timer, it should be just enough for what you need
how about this:
http://www.quartz-scheduler.org/overview/index.html
I'd use the Quartz Scheduler. I don't know the architecture of your app so I can't really say if Spring configuration or just code configuration is better. You could use a SimpleTrigger, or even a CronTrigger if you want a more expressive scheduling.
I would expose entry point to 'load' functionality as a servlet in your webapp and then use external scheduler (cron on Unix, Scheduled Tasks on Windows) to call that servlet via wget or any other command line http client.
This approach has advantages of not depending on any third party libraries (adding Qurtz just for one task seems like overkill for me) and also has flexibilty of changing schedule without touching your code as well as triggering 'load' manually if desired.
Thanks all for help - I do this task with help of Timer, TimerTask and ServletContextListener:
servletContext = event.getServletContext();
// create the timer and timer task objects
Timer timer = new Timer();
// get a calendar to initialize the start time
Date startTime = Calendar.getInstance().getTime();
List<Company> companies = CompanyUtils.getInstance().getCompanies();
if (companies.size() == 0)
return;
for (int i = 0; i < companies.size(); i++)
{
FileUpdater task = new FileUpdater(companies.get(i).getUrl());
// schedule the task to run hourly
timer.scheduleAtFixedRate(task, startTime, companies.get(i).getUpdatePeriod());
}
// save our timer for later use
servletContext.setAttribute("timer", timer);
Once more thanks!
With best wishes!
I have an application that runs in JBoss. I have an incoming web service request that will update an ArrayList. I want to poll this list from another class every 60 seconds. What would be the most efficient way of doing this?
Could anyone point me to a good example?
I would also recommend ScheduledExecutorService, which offers increased flexibility over Timer and TimerTask including the ability to configure the service with multiple threads. This means that if a specific task takes a long time to run it will not prevent other tasks from commencing.
// Create a service with 3 threads.
ScheduledExecutorService execService = Executors.newScheduledThreadPool(3);
// Schedule a task to run every 5 seconds with no initial delay.
execService.scheduleAtFixedRate(new Runnable() {
public void run() {
System.err.println("Hello, World");
}
}, 0L, 5L, TimeUnit.SECONDS);
As abyx posted, Timer and TimerTask are a good lightweight solution to running a class at a certain interval. If you need a heavy duty scheduler, may I suggest Quartz. It is an enterprise level job scheduler. It can easily handle thousands of scheduled jobs. Like I said, this might be overkill for your situation though.
You can use Timer and TimerTask. An example is shown here.
See java.util.Timer. You'll need to start a robot in a separate thread when your app comes up and have it do the polling.
Check the answers to the question "How to run a task daily from Java" for a list of resources related to your problem.
The other answers are basically advising you do your own threads. Nothing wrong with that, but it isn't in conformance with the EJB spec. If that is a problem, you can use JBoss' timer facilities. Here is an example of how to do that.
However, if the EJB spec is at issue, storing state like an ArrayList isn't compliant as well, so if you are just reading some static variable anyway, specifically using a container Timer service is likely overkill.