Results 1 to 6 of 6

Thread: Help for Timer examples in Java

  1. #1
    Join Date
    Apr 2010
    Posts
    81

    Help for Timer examples in Java

    Hi everybody. I have recently started studying the Java programming language. I have done very basic coding in Java. Now I am eager to know about the timer in java. Since, I am not having any knowledge about it, I thought that taking suggestions (help) from you guys would be helpful.!! Please tell me some information about the examples of timer in Java. Hoping that someone hanging out there will help me soon.!!

  2. #2
    Join Date
    Dec 2008
    Posts
    161

    Re: Help for Timer examples in Java

    Since, you are new to Java, I think that you should know the basic things that are required for timer examples. When programming you may see the need to perform a task every so often. For example, if you draw a clock on the screen every second we move or repainting the second. Perhaps you want to see when a file is created, which can, for example, every ten seconds to see if it exists. In short, there are plenty of possible applications that you need to perform regular tasks.

  3. #3
    Join Date
    Jan 2006
    Posts
    211

    Re: Help for Timer examples in Java

    Java provides two distinct classes that help us to this task. Javax.swing.Timer are classes and java.util.Timer. In both classes we say we notice how often (eg every second notice in the case of clock) and they are responsible for calling a method that we have implemented. The result is that this method (which paints the seconds on the screen), will be called every so often (once per second for the clock).

  4. #4
    Join Date
    Jan 2009
    Posts
    140

    Re: Help for Timer examples in Java

    You will have to use the javax.swing.Timer for doing the Timer examples in Java. This class is easier to use. Just instantiate it passing each how long (in milliseconds) we want to let us know and ActionListener, which method actionPerformed () will run periodically. Then just call the method start () when we want the timer starts counting. The code may look like this:
    Code:
    Timer timer = new Timer (TimeInMilliseconds, new ActionListener () 
    ( 
    public void actionPerformed (ActionEvent e) 
    ( 
    // Here you have to execute the code. 
    ) 
    )); 
    ... 
    timer.start ();

  5. #5
    Join Date
    Jan 2009
    Posts
    143

    Re: Help for Timer examples in Java

    According to me, you should know more about the java.util.Timer. This class is more general, has more options, but is somewhat more complex to use. To start it you must call either of the schedule (). I will not detail them all because that's what the API, but give us a good chance. We can say things complex, for example, that "as of July 22, 2010, 17:00 begins to notice me every 5 minutes "Or we can say simple things like" tell me once in 5 minutes".

  6. #6
    Join Date
    Mar 2008
    Posts
    232

    Re: Help for Timer examples in Java

    Within the repetitive warnings are even two options of the java.util.Timer :
    • With the methods schedule (), for example, if the notice is repeated every second, it is possible that the computer is busy doing other things, so we can notice a certain delay. This option accumulates the delay between calls. If the computer is very busy and gives us warnings every 1.1 seconds instead of 1, the first notice we received in the second 1.1, the second at 2.2, the third in the 3.3, etc, etc. If we do our clock This way, you have to take advances or delays in no time.
    • The other option is scheduleAtFixedRate methods (). With these methods, the warnings are related to the notice, in this way, if there is delay in the notice, does not affect when the following occurs. As before, if the computer is very busy and gives warnings every 1.1 seconds instead of every second, the first warning will be received in the second 1.1, the second 2.1, third in the 3.1, etc. The delay is not being collected. It is clear that to make a clock, as in our example, we use the second form (scheduleAtFixedRate methods () instead of schedule ()).

Similar Threads

  1. Error while using the timer class in Java
    By ScarFace 01 in forum Software Development
    Replies: 4
    Last Post: 27-07-2010, 01:47 AM
  2. Timer and alarm in C / Java
    By Izek in forum Software Development
    Replies: 4
    Last Post: 20-07-2010, 03:24 PM
  3. Execute timer service in java
    By Messenger in forum Software Development
    Replies: 4
    Last Post: 20-07-2010, 01:27 PM
  4. Java - System Tray with timer
    By Anthony12 in forum Software Development
    Replies: 4
    Last Post: 20-07-2010, 11:58 AM
  5. Sources of Java Application Examples
    By Steyn in forum Software Development
    Replies: 5
    Last Post: 09-01-2010, 12:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •