• 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

Query about ScheduledThreadPoolExecutor.scheduleAtFixedRate

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
K Gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic