Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Problem in java timer Class

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 17-02-2010
Member
 
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.

Reply With Quote
  #2  
Old 17-02-2010
Member
 
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.
Reply With Quote
  #3  
Old 17-02-2010
Member
 
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);
Reply With Quote
  #4  
Old 17-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 17-02-2010
Member
 
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.
Reply With Quote
  #6  
Old 17-02-2010
Member
 
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." ) ;
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem in java timer Class"
Thread Thread Starter Forum Replies Last Post
Problem using Java.util.Timer Gokul20 Software Development 7 09-09-2010 10:20 PM
Converting java class to java bean class Ucchal Software Development 6 09-08-2010 10:24 AM
Error while using the timer class in Java ScarFace 01 Software Development 4 27-07-2010 01:47 AM
Help for Timer examples in Java Feng Software Development 5 24-07-2010 03:36 AM
How to use Swing Timer class in java program? KALANI84 Software Development 4 25-01-2010 03:37 PM


All times are GMT +5.5. The time now is 03:59 AM.