• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timer

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
}});

}
}
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic