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.
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.
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);
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.
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.
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." ) ;
}
}