my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Timer" Watch "Timer" New topic
Author

Timer

Antony Amicone
Ranch Hand

Joined: Mar 11, 2006
Posts: 125
Hi, i can't understand how to use timers, so i've tryed to write a little program that should write "prova" every second. Where is my error? May you help me? Tnx

import java.awt.*;
import javax.swing.*;

public class prova {
public static void main(String[] args) {

timer = new Timer(1000,new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("Prova");
}});

}
}
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
There are two problems with your code, one problem is that you are not starting the timer, and the second problem is that your application thread is terminating perform your Timer thread has been fired.

Therefore you need to have some loop or delay in your code to allow the Timer to fire, otherwise the application finishes before your timer has fired (I've just used Thread.sleep to stop the thread terminating) you would need to do something a bit more robust.


[ March 11, 2006: Message edited by: Jason Moors ]
Antony Amicone
Ranch Hand

Joined: Mar 11, 2006
Posts: 125
tnx a lot, is it impossible to made this thing, whitout using thread?
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
It depends on what you are trying to achieve, the reason the Timer runs in another thread is to enable an application to do other things, e.g. if you wrote a text editor program you could allow the user to type a letter, but use the Timer to automatically save the file after every 5 minutes.

If you only want you application do something after a period of time you could use a while loop and then sleep.

i.e

Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
I just had a quick look at the java.util.Timer which is different than the Swing version you were using, and it keeps a program running as long as its timer threads are running (so you don't need the to use the sleep!).

http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
[ March 11, 2006: Message edited by: Jason Moors ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"kainard,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer
 
Similar Threads
Threads doubt
Timer
Help me please ... Timer
Timer/ TimerTask
JScrollPane and hidden content