This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timer

 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tnx a lot, is it impossible to made this thing, whitout using thread?
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic