File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Timer Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Timer Question" Watch "Timer Question" New topic
Author

Timer Question

Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
Hi,

I am using a timer that runs every 10 seconds. The TimerTask functionality
I implemented starts three threads every time the timer runs.

If those threads did not finish executing within 10 second, another
timer interval will start and three new threads will start. As over all,
I will have at least six threads in the JVM.

My question : How can i prevent overlapping of timer intervals.Which
I do not the second interval to start if there is a thread from the
first interval still running.

As an initial solution I am using Thread.activeCount to check
wether the number of active threads exceend a certain limit. Of so, I
end run function of the TimerTask by a return statement.

What do you think
[ September 18, 2007: Message edited by: Khaled Mahmoud ]

SCJP, SCJD,SCWCD,SCDJWS,SCEA 5 MCP-C#, MCP-ASP.NET - http://www.khaledinho.com/
Life is the biggest school
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

My question : How can i prevent overlapping of timer intervals.Which I do not the second interval to start if there is a thread from the first interval still running.


A Timer is single threaded -- which means the timer will not be able to call the repeated task again til the previous call finishes. If your timer task performs a join() of the three threads that it starts, the timer will not be able to call the task again, until the three threads finish.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

What henry told is perfect.
Actually, creating 3 new threads every time the scheduled task is executed, does not look like a good choice. You can use a thread pool instead and restrict the maximum size to 3.
If you are using jdk 5 then, instead of timer you can use a ScheduledThreadPoolExecutor that extends the features of a Timer to provide a thread pool also.
[ September 18, 2007: Message edited by: Nitesh Kant ]

apigee, a better way to API!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See the Timer doc for fixed delay vs fixed interval. Do you really want to start a new task before the previous one finishes? You can if you like, or you can choose not to.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Timer Question