aspose file tools*
The moose likes Threads and Synchronization and the fly likes Query about ScheduledThreadPoolExecutor.scheduleAtFixedRate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Query about ScheduledThreadPoolExecutor.scheduleAtFixedRate " Watch "Query about ScheduledThreadPoolExecutor.scheduleAtFixedRate " New topic
Author

Query about ScheduledThreadPoolExecutor.scheduleAtFixedRate

K Gupta
Ranch Hand

Joined: Aug 31, 2004
Posts: 43
Hi,

I have used ScheduledThreadPoolExecutor in following example


Now this is what javadocs says about ScheduledThreadPoolExecutor


[javadoc]public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit)Description copied from interface: ScheduledExecutorService
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.

Specified by:
scheduleAtFixedRate in interface ScheduledExecutorService
Parameters:
command - the task to execute.
initialDelay - the time to delay first execution.
period - the period between successive executions.
unit - the time unit of the initialDelay and period parameters
Returns:
a Future representing pending completion of the task, and whose get() method will throw an exception upon cancellation.[/javadoc]


So according to javadocs for every millisecond a thread should be invoked and increment the count and then go for sleep.

But this is what I get in console:

5
1265023803311 pool-1-thread-1 ---> 1
1265023813311 pool-1-thread-1 ---> 2
1265023823311 pool-1-thread-1 ---> 3
1265023833311 pool-1-thread-1 ---> 4
1265023843311 pool-1-thread-1 ---> 5


My question is why another thread in the pool is not getting started. As you can see from time diference , I have to wait for thread 1 to be completed, then only the task is done again ? My expectation given the delay in sleep all 5 threads would be getting started.
Please suggest me if I am correct and how to get second thread started ?


Thanks,
Kshitij Gupta



Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.

I think that only one instance of scheduled task(Runnable) can be executed at a time. Hence, the subsequent task won't run until the former terminates.

http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit)

Docs says:

"... If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute."

Adam


SCJP, SCWCD, SCBCD, SCDJWS
K Gupta
Ranch Hand

Joined: Aug 31, 2004
Posts: 43
Thanks Adam,

But I still don't get the need for a thread pool. If initiation of task depends upon the completion of ongoing task, then only one thread is suffice for this activity. then what is the significance of thread pool that ScheduledThreadPoolExecutor owns ?


Thanks & Regards,
Kshitij Gupta
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.

But you always can "deploy" more tasks than only one. An idle worker thread takes subsequent task and propels it to run.
Sometimes you don't know how many tasks will be scheduled during your program, but you are able to limit resources like threads.
Sometimes you can have more scheduled tasks than established worker threads, because tasks are performed seldom and then some free worker thread takes it.

Results:
task1: pool-1-thread-1
task2: pool-1-thread-2
task3: pool-1-thread-3
task3: pool-1-thread-3
task1: pool-1-thread-1
...
task2: pool-1-thread-3
task3: pool-1-thread-2
task1: pool-1-thread-1
task3: pool-1-thread-2
task2: pool-1-thread-3
task3: pool-1-thread-2
task1: pool-1-thread-1
...

I've altered your example:



Adam
K Gupta
Ranch Hand

Joined: Aug 31, 2004
Posts: 43
Thanks Adam for your explanation.

So if I want a same task to run concurrently, should I resubmit it as many times as the size of pool ?


Thanks & Regards,
Kshitij Gupta
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.

I cannot answer your question unambiguously.
It depends on characteristics of the task, your design needs and requirements i.e. potential possibility of overlapping tasks when they delay.
You can consider other simplified strategies, for example scheduling 2 the same tasks as alternating operations(with different starting and scheduling times).
When the first delays, the next will be started. But it is very simplified model, many problems are omited.

Adam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about ScheduledThreadPoolExecutor.scheduleAtFixedRate
 
Similar Threads
Synchronizing without blocking in a servlet filter
Timeout a thread
basic concept level question regarding threads
Under what conditions does the ScheduledExecutorService start the task on new threads (JDK 5)
ScheduledExecutorService.scheduleAtFixedRate not doing what it is promising?