Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timer, Again

 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry forgot to add one member funtion.

public void stop()
{
running = false;
}
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic