File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Jconsole showing multiple java.util.Timer threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Jconsole showing multiple java.util.Timer threads" Watch "Jconsole showing multiple java.util.Timer threads" New topic

Jconsole showing multiple java.util.Timer threads

Mukesh Negi

Joined: Jul 25, 2012
Posts: 17
I am using java.util.Timer in my code. I have two Timer. 1st runs after each 10 seconds and the other after each 6 hours. When I check the no. of threads in the jconsole its showing 4 threads for timer.
Actually I am not getting why its showing so. I dont want multiple threads for a single Timer to start as it can corrupt my data.
I have googled it a lot, as per my understanding it dosent start different threads to complete the tasks. It keeps the pending tasks in the queue.
But I am not getting why jconsole is showing it.
Please help me....

Thanks in advance.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

And does your code run correctly, i.e. does it do what you expect? If the answer is "Yes" then why are you concerned about that? If the answer is "No", however, then your strategy should be to look at your code and find the problem.
Mukesh Negi

Joined: Jul 25, 2012
Posts: 17
Even if its currently working correctly without any problem, in threading its not guarented in future, as i have coded considring 1 thread/timer. But if its wrong it may give unwanted result. I am using other apis i.e log4j for loging and c3p0 for connection pooling. I have found that c3p0 uses a timer. Thanks 4 your reply..
Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2746

I would suggest that you read java.util.Timer javadocs carefully. Espescially this part

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. 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.

After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the timer's cancel method.
Mukesh Negi

Joined: Jul 25, 2012
Posts: 17
thanks for all your replies. Actually, their were no issues. It is the C3P0 which makes a timer for each datasource object created and therefore I was getting extra Timer threads.
I agree. Here's the link:
subject: Jconsole showing multiple java.util.Timer threads
It's not a secret anymore!