Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Under what conditions does the ScheduledExecutorService start the task on new threads (JDK 5)

 
Robin Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!

I'm playing around with the ScheduledExecutorService scheduler. In the code i first init the scheduler with a fixed thread pool size and then pass it a simple task which keeps refreshing a shared list with new data, periodically. At random, the task might throw a runtime exception, in which case the scheduler should start a new thread, but i don't see it doing that. So, how do i simulate this scenario? I'm running this code on a single CPU m/c, using JDK 1.5.



I just get the following log messages and then nothing happens:

[18:41:45,150] Using a pool size of: 1
[18:41:45,160] Original list... [33, 39, 4, 82, 22]
[18:41:45,160] Starting scheduler...
[18:41:45,160] Setting shutdown hook to cleanup thread pools...
[18:41:49,156] pool-1-thread-1 > Refreshing list with new data: [25, 42, 73, 17, 17]
[18:41:49,166] pool-1-thread-1 > zzzZZZ, gotta sleep before updating...
[18:41:51,169] Oops, gotta throw an exception


Thanks.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your task ended up with an exception, it will not be executed anymore.
 
Max Galushka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just don't throw an exception inside your task
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why not catch any exceptions here ? If you catch and log them, the executor will not be terminated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic