aspose file tools*
The moose likes Java in General and the fly likes Thread-based timer versus Timer Class-based timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread-based timer versus Timer Class-based timer" Watch "Thread-based timer versus Timer Class-based timer" New topic
Author

Thread-based timer versus Timer Class-based timer

Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Does anyone suggest me whether I should use Thread-based timer using sleep, start and stuff or Timer Class-based timer from java.util package for the timing applications? My applcations need to run a particular methods for a period of time like 3 seconds or 5 seconds interval, according to the configuration in the file...

Thanks...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
My applcations need to run a particular methods for a period of time like 3 seconds or 5 seconds interval, according to the configuration in the file


In my opinion ....

In your application, you should be use TimerTask to solve your problem. Because this api provide to create process schedule.

Thread Api provide for create concurrent/parallel program/application.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
I would prefer Timer / TimerTask over Threads for this scenario.
From Java Practices.com:
  • if it is to be done later, or periodically, use Timer and TimerTask
  • it it may take a long time, or may block, use a Thread


  • Mani
    Quaerendo Invenietis
    somkiat puisungnoen
    Ranch Hand

    Joined: Jul 04, 2003
    Posts: 1312
    Originally posted by Mani Ram:
    I would prefer Timer / TimerTask over Threads for this scenario.
    From Java Practices.com:
  • if it is to be done later, or periodically, use Timer and TimerTask
  • it it may take a long time, or may block, use a Thread





  • Good link for TimerTask Example..
    Ko Ko Naing
    Ranch Hand

    Joined: Jun 08, 2002
    Posts: 3178
    Originally posted by Mani Ram:
  • it it may take a long time, or may block, use a Thread



  • Thanks a lot for the link, Mani... But I'm not qutie clear with the above sentence... Shouldn't it be if it take a short time, we should use a Thread? because as far as I know, if we let a Thread to be blocked a long time, it would not be good...

    Could you explain a bit details for this issue? Thanks...
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    Originally posted by Ko Ko Naing:


    Thanks a lot for the link, Mani... But I'm not qutie clear with the above sentence... Shouldn't it be if it take a short time, we should use a Thread? because as far as I know, if we let a Thread to be blocked a long time, it would not be good...

    Could you explain a bit details for this issue? Thanks...


    From java docs of Timer class

    Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.


    Groovy
    Rikko Verrijzer
    Ranch Hand

    Joined: Jul 22, 2003
    Posts: 34
    Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.


    That is also true when you are using your timer task to run a lot of small tasks over a long period of time. The timer will tend to slip some time eveytime it executes a task, resulting in inaccurate execution moments.
    For example if you run every 10 mins a task, you might notice a delay of a minute or so over a month time.

    Rikko
    Ko Ko Naing
    Ranch Hand

    Joined: Jun 08, 2002
    Posts: 3178
    One of my doubts is that the internal implementation inside the timer and timer task is still thread-based... I doubt that Timer and TimerTask classes are just to provide convenient usage to the programmers, hiding the internal implementation of threads inside it...

    Thanks a lot for comments, guys...
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    [RV]: That is also true when you are using your timer task to run a lot of small tasks over a long period of time. The timer will tend to slip some time eveytime it executes a task, resulting in inaccurate execution moments.

    I don't think this is true if you are careful to use one of the schedule() methods which specifies fixed-rate execution rather than fixed-delay. See the API for details. Using fixed-rate execution, the only reason a task should be delayed is if the preceding task is still executing at the time the latter task is supposed to start. Well, within the limits of accuracy for methods like sleep(long) and wait(long), which explicitly avoid making guarantees about how soon a thread will wake up before the minimum interval has elapsed. But in practice they're pretty reliable as long as you don't need them accurate within a few milliseconds, and you don't have too much thread contention. (Which is anyother reason you don't want to make more threads than you really need, which is why Timer is useful, etc.)

    [KKN]: One of my doubts is that the internal implementation inside the timer and timer task is still thread-based..

    The biggest strength of Timer is if you have many different tasks to schedule at different times. Timer takes on the job of scheduling everything using a single thread, very efficiently. If you do this yourself it's probably easiest to use a separate thread for each task - but this can be unnecessarily inefficient if you've got a lot of different tasks to schedule. Timer makes this a lot easier to handle efficiently.


    "I'm not back." - Bill Harding, Twister
    Ko Ko Naing
    Ranch Hand

    Joined: Jun 08, 2002
    Posts: 3178
    Originally posted by Jim Yingst:
    The biggest strength of Timer is if you have many different tasks to schedule at different times. Timer takes on the job of scheduling everything using a single thread, very efficiently. If you do this yourself it's probably easiest to use a separate thread for each task - but this can be unnecessarily inefficient if you've got a lot of different tasks to schedule. Timer makes this a lot easier to handle efficiently.


    Thanks a lot, Mr.Jim, for your great and reasonable explanation...

    So the main difference here we can conclude for this thread is EFFICIENCY... So should we be always using Timer class-based timers for the job scheduling? Even though we can use threads to accomplish this kind of job, we should better use Timer and TimerTask classes for efficiency...

    Since Threads can be used in other implementations, rather than job scheduling, Threads won't be deprecated for sure... I do hope others will also get great knowledge from this thread...
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    Threads won't be deprecated for sure


    Deprectaion of threads Why?
    Ko Ko Naing
    Ranch Hand

    Joined: Jun 08, 2002
    Posts: 3178
    Originally posted by Pradeep Bhat:


    Deprectaion of threads Why?

    Don't be shocked, Pradeep... I'm just thinking of the case that threads are used only for job scheduling, which Timer and TimerTask can efficiently handle...

    Of course, we all know that Threads can be used in many other places, rather than job scheduling... So don't be shocked... I'm just thinking about the things that is impossible...
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    Ko ko,

    You may also have a look @ Quartz
    http://www.onjava.com/lpt/a/4637
    Ko Ko Naing
    Ranch Hand

    Joined: Jun 08, 2002
    Posts: 3178
    Originally posted by Pradeep Bhat:
    Ko ko,

    You may also have a look @ Quartz
    http://www.onjava.com/lpt/a/4637


    Great! Thanks for the link, Pradeep... I've just read it and tried it with my old requirements... That works well...
    It's a great time-saving open-source tool for job scheduling...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Thread-based timer versus Timer Class-based timer