How to create a Java timer that repeats at different times

I have stock market data. I want to simulate the stock market by offering prices at intervals that can be determined from the moment the trades take place.

What would be the best way to do this.

Until now, I have a class with static variables and methods in which I store the hour, minutes, millseconds for the last time I trade. Then I use the trade time for the current trade and calculate it from the last saved trade values.

I then store the "interval" in milliseconds as a static member variable in the same class as the temporary variables.

I am using this line:

timer.schedule(new RemindTask(), TimeStore.getNextInterval());

      

where TimeStore.getNextInterval () retrieves the interval that was calculated.

Can you think of a better way, this doesn't seem to work, and it doesn't seem very elegant.

+2


source to share


3 answers


If you don't want to use Quartz take a look at Java ScheduledExecutorService

.



http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html

+5


source


Use Quartz.

From the linked page:



Quartz is a full-featured open source job scheduling system that can be integrated or used across virtually any J2EE or J2SE application, from the smallest standalone application to the largest e-commerce system. Quartz can be used to create simple or complex graphs to complete tens, hundreds, or even tens of thousands of jobs; jobs whose tasks are defined as standard Java or EJB components. The Quartz Scheduler includes many enterprise-class features such as JTA transactions and clustering.

+1


source


Well, for your task, I have a different solution. You can use javax.swing.Timer instead of java.util.Timer; and then you can call the constructor sending the delay that wants and zero for the action's actionListeners, and then you can add both addactionListeners (this) and override the actionPerformed with ur setting. In javax.swing.Timer the actionListeners are notified at the selected interval again

-1


source







All Articles