Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Periodically task performing thread

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Status: Thread Newbie

What will be the most efficient way to achieve the following requirement:

-->I have a thread that needs to periodically (period set by user which could run into minutes or hours or maybe even day/s) perform some tasks. Some options that I thought of were:

1. putting this thread to sleep, or
2. having another thread (say a timer thread) send an interrupt to this thread perhaps

The basic performance objective here is to minimize system resource usage.

Thnx for any help.

Shane
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My status: Thread newbie

I not sure but maybe the Quartz system would be a solution:
http://www.opensymphony.com/quartz/

If the status of the object with your run()/execute()-method should be
saved in the objects fields between the executions,
Quartz will not be very helpful, because it creates a new object
for each triggering event.

Best,

Torsten
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My goodness. Talk about killing a gopher with a chaingun.

The class "java.util.Timer" should serve your needs nicely. You package your task as an class that implements Runnable, create a Timer, set the timer, and it takes care of firing the Runnable after specified delay.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like the use the OS job services in cases like this.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If Quartz or not is a chaingun depends upon what need to be done.
If it is simply a matter of delaying a one shot work a timer may be enought.

I found Quartz fairly simple to use and it is really good if you want to set up crontab like scheduling (using a quartz CronTrigger).
(I.e. do this work every 15min between 7 am and 5pm Monday to Friday.)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic