|
| ||||||||||
| Tags: class files, java, schedule tasks, source code, task handling, timer |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Problem in java timer Class
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);
} |
|
#2
| |||
| |||
| 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); |
|
#3
| |||
| |||
| 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); Code: Timer tm = new Timer("monTimer",true); |
|
#4
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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." ) ;
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |