Results 1 to 6 of 6

Thread: Problem in java timer Class

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Problem in java timer Class

    Hello,
    I wish I could execute a scheduled tasks. I came across and sheduleAtFixedRate Timer () but I do TimerTask occurs only once.
    Here is my code:
    Code:
    Public class Scheduler{
     
       Public Scheduler(){
    
           Timer tm = new Timer("monTimer",true);
           TimerTask tsk = new TimerTask() {
               Public void run() {
                   System.out.System.out.println("Execution of the Action Timer");
                   
               }
           };
           tsk.
           tm.scheduleAtFixedRate(tsk,new Date(),100);
           
       }
    Any help on this is appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Problem in java timer Class

    Hello,
    I was a little lost because the builder you use is not in the jdk1.4.2
    you probably use a different versions (I am not looking for 5) in short, we can still see that there is a constructor that takes a boolean parameter isDeamon in which a prior declare your timer in deamon which will then stops when your main program will stop, I suggest, therefore, tried to build your timer as follows:
    Code:
    Timer tm = new Timer("monTimer",false);
    That said I never use these classes, I pretend it is not the solution but my suggestion is not very long to put in place so why not try.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Problem in java timer Class

    Hello,
    Even I was trying a similar kind of a problem. You're right, the timer set demon stops with applic. By my applic a thread in the timer works well, my message appears every second. It is quite logical too. If applicable does not turn over, there is no reason that the timer continues to make these actions.
    By cons I can see the difference
    Code:
    Timer tm = new Timer("monTimer",false);
    Or this
    Code:
    Timer tm = new Timer("monTimer",true);

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Problem in java timer Class

    Hello,
    The constructor Timer (boolean) allows to specify if the thread is created type daemon. A thread type daemon running in the background of its parent thread and it automatically ceases to exist when its parent terminates. While a thread "no daemon continues to live even if its parent is completed. I hope you are understanding what I am tying to say.

  5. #5
    Join Date
    Nov 2009
    Posts
    347

    Re: Problem in java timer Class

    Hello,
    Basically if you put "false" in the constructor, you no longer need to put in a thread it turn by itself as a great application that stops or not. I think you can use the both, it depends on how you want your program to run and how the program should perform. I think you should try using the both and use while is the better. If you want more information on this then feel free to post back.

  6. #6
    Join Date
    Nov 2009
    Posts
    330

    Re: Problem in java timer Class

    Hello,
    I have a code with me, I think you should see this
    Here is the code
    Code:
    public class test  {
      Timer tm;
    
      public test ( int seconds )   {
        tm = new Timer (  ) ;
        tm.schedule ( new testTask (  ) , seconds*1000 ) ;
      }
    
    
      class testTask extends TimerTask  {
        public void run (  )   {
          System.out.println ( "OK, It's time to do something!" ) ;
          tm.cancel (  ) ; //Terminate the thread
        }
      }
    
    
      public static void main ( String args [  ]  )   {
        System.out.println ( "Schedule something to do in 5 seconds." ) ;
        new test ( 5 ) ;
        System.out.println ( "Waiting." ) ;
      }
    }

Similar Threads

  1. Problem using Java.util.Timer
    By Gokul20 in forum Software Development
    Replies: 7
    Last Post: 09-09-2010, 10:20 PM
  2. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  3. 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
  4. Help for Timer examples in Java
    By Feng in forum Software Development
    Replies: 5
    Last Post: 24-07-2010, 03:36 AM
  5. How to use Swing Timer class in java program?
    By KALANI84 in forum Software Development
    Replies: 4
    Last Post: 25-01-2010, 03:37 PM

Posting Permissions

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