Java Timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crossroadsk
    New Member
    • Nov 2006
    • 30

    Java Timer

    Hi ALL,

    I want to print a message for every 5 seconds of the system time

    How should i do it?

    I 've written some code but loop is not correct ....

    Waiting for sugestions ?

    Please help ..

    thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by crossroadsk
    Hi ALL,

    I want to print a message for every 5 seconds of the system time

    How should i do it?

    I 've written some code but loop is not correct ....

    Waiting for sugestions ?

    Please help ..

    thanks
    Let's see your code that's giving problems. Did you use the Thread.sleep method?

    Comment

    • crossroadsk
      New Member
      • Nov 2006
      • 30

      #3
      Originally posted by r035198x
      Let's see your code that's giving problems. Did you use the Thread.sleep method?
      Thanks for reply

      solved the problem almost .... just need to control the loop because it is an continuous loop

      Here is the code:
      import java.io.*;
      import java.util.*;
      class TimerTest extends TimerTask
      {
      public static void main(String[] args) throws IOException
      {
      TimerTask tt = new TimerTest();
      Timer t = new Timer();

      /*while (new BufferedReader( new InputStreamRead er(System.in)). read()!='x')
      {
      t.scheduleAtFix edRate(tt, new Date(),5000);
      }*/
      t.scheduleAtFix edRate(tt, new Date(),5000);
      }
      public void run()
      {
      System.out.prin tln("Timer set for every 5 seconds");
      }
      }

      remove the comments and comment the last line of main() to test the while loop.

      Now i'll describe my actual task ...

      we have to print documents according to the scheduled time.
      That means,
      if we select some files and set some time for that file, it should get printed without any manual interference. Setting time like once a day, once every 1 hour and so on....

      Comment

      Working...