• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread-based timer versus Timer Class-based timer

 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

  •  
    somkiat puisungnoen
    Ranch Hand
    Posts: 1312
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3178
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8927
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Rikko Verrijzer
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3178
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [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.
     
    Ko Ko Naing
    Ranch Hand
    Posts: 3178
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8927
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Threads won't be deprecated for sure


    Deprectaion of threads Why?
     
    Ko Ko Naing
    Ranch Hand
    Posts: 3178
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8927
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ko ko,

    You may also have a look @ Quartz
    http://www.onjava.com/lpt/a/4637
     
    Ko Ko Naing
    Ranch Hand
    Posts: 3178
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic