fork download
  1. import java.util.concurrent.Executors;
  2. import java.util.concurrent.ScheduledExecutorService;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. class Ideone {
  6.  
  7. public static void main(String[] args) throws InterruptedException {
  8. final ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
  9. final int[] count = { 1 };
  10. final int MAX_COUNT = 3;
  11. final Runnable[] runnable = new Runnable[1];
  12. runnable[0] = () -> {
  13. try {
  14. System.out.println(String.format("count=%d", count[0]));
  15. throw new RuntimeException();
  16. } catch (Exception e) {
  17. if (count[0] < MAX_COUNT) {
  18. ++count[0];
  19. service.schedule(runnable[0], 1, TimeUnit.SECONDS);
  20. }
  21. }
  22. };
  23. service.execute(runnable[0]);
  24. Thread.sleep(10_000L);
  25. service.shutdown();
  26. }
  27. }
Time limit exceeded #stdin #stdout 5s 36048KB
stdin
Standard input is empty
stdout
count=1
count=2
count=3