jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Timer, Again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Timer, Again" Watch "Timer, Again" New topic
Author

Timer, Again

Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Guys,

Sorry but I have to post another topic on timers. I had a look at threads on timers but could not see a solution to my specific problem.

This is what I'm trying to replicate. In Borland Visual C++ they have a Timer wrapper. It's simply a matter of putting your code inside it. It runs the code every x seconds, depending on what value you put on its interval property.

I want to do this in Java. I need at least 2 Timer objects, each having different intervals. One would be used to display time (so the interva here is once every second), while the other one is user selectable.

Karen


amerzil co-ed student<br />"Praise be the Code"
Mahesh x Bogadi
Ranch Hand

Joined: Jul 06, 2004
Posts: 51
use Threads.

Implement the following class

//Timer.
public abstract class Timer extends Thread
{
protected boolean running = true;
protected long TimeInterval = 0;

public Timer(long timerInterval)
{
TimeInterval = timerInterval;
super.start();
}

public void run()
{
while(running)
{
performThePeriodicTask();
wait();
}
}

protected void performThePeriodicTask();
protected void wait()
{
try
{
sleep(TimeInterval);
}
catch(Exception e)
{
}
}
}
Mahesh x Bogadi
Ranch Hand

Joined: Jul 06, 2004
Posts: 51
sorry forgot to add one member funtion.

public void stop()
{
running = false;
}
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
By using threads when will the other thread come into play? Do I call yield() to let the other thread run?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The Timer in java.util will do the job for you. Put the code you want to execute in the run() method of a little class that implements TimerTask. Create a timer, and schedule your tasks to run at a fixed rate.

Did you try some things with Timer and run into trouble? Post some almost-running code and see if we can help. It ought to look like:

A personal style note on the suggestion above ... I'd avoid giving one of my classes the same name as something in the JDK libraries. MyCustomTimer might be better than Timer if it really is Timer-like.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
You can use swing's timer they are very nice(I never use the one in util). Take a look at this example

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class TimerExample {
public static void main(String[] args) {
Timer t = new Timer(2000, new TimerActionListener());
Timer t2 = new Timer(4000, new TimerActionListener());
t.start(); //start threaded timer
t2.start(); //start threaded timer
while(true);//loop for ever
}
}

class TimerActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
System.out.println("timer" + e.getSource());
}
}


basically the timer will call the funcion actionPerformed of the ActionListener you provided in the constructor of the timer after xxxx millisecond. At the end I added the while(true); because it is runned in the console and once the main is finish the application exit. If you use it in a swing(or awt) app where there is already a thread doing that you do not need it!

Enjoy!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer, Again