• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timer starts twice when it should start only once

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Timer that runs as a deamon. To this Timer I add one single TimerTask to run each 60 sec. The Timer is instantiated by the init() method of a servlet at application startup.

The problems is that the Timer starts TWO different threads with the TimerTask. Both threads run once every 60 sec, and simingly with about 5 seconds between them.

This is very strange because as far as I can see my code should only start ONE thread. What might be wrong? What can I do to make sure the timer only starts ONE thread?

Here's my code (a bit simplified):



This should be a fairly simple setup of a Timer, and I can't understand why it would start two separate threads with the TimerTask!?

As a note I can mention that in my application there's another Timer in another class (which has nothing to do with this class). It is possible to have more than one Timer within the same JVM, right?

Hope somone can help!
[ November 19, 2006: Message edited by: Egil Poma ]
 
Egil Poma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put some more debug statements into the code, and the servlet init method gets run twice when the application starts up (tomcat).

How on earth can that be?
 
Egil Poma
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found out why the init metod gets run twice ; I'm running tomcat and apache connected, and that causes all init methods to be run twice.

As described here:
http://mail-archives.apache.org/mod_mbox/tomcat-users/200204.mbox/%3CHFEAJGDFPPNOMHMMIKEDMENMCFAA.andy.eastham@gliant.com%3E

So I'm thinking to create a static instance of the MailPoller class in a different class, and then just check if it's null in the init method before making an instance of it.

But will making a static instance of the MailPoller be thread safe?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the case of a Singleton. So, synchronization is applicable as for a singleton. See Double-checked locking and Singleton Pattern

No - using static does not mean synchronized access. You'll have to manually synchronize access to the initialization block, even if the object is static.

If you are not particular that the object should initialize when the servlet loads, initialize the object in a static block. It'll be initialized when the class is loaded.

Be aware of multiple class loaders (as you would, when implementing a Singleton).

An interesting implementation of lazy instantiation for Singleton looks like this:



SingletonWrapper is loaded lazily and "instance" is created synchronously.
[ November 22, 2006: Message edited by: praveen balaji ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic