fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.concurrent.ScheduledExecutorService;
  8. import java.util.concurrent.Executors;
  9. import static java.util.concurrent.TimeUnit.SECONDS;
  10.  
  11. /**
  12.  *
  13.  * @author Wsl_F
  14.  */
  15. class MyTask implements Runnable {
  16.  
  17. // number of executions
  18. private int executesTimesLeft;
  19. // execution period
  20. private final int periodSeconds;
  21. // task id
  22. private final int id;
  23. // scheduler
  24. private ScheduledExecutorService scheduler;
  25. // field to measure time between executions
  26. private long lastExecution = 0;
  27.  
  28. public MyTask(ScheduledExecutorService scheduler, int executes, int periodSeconds, int id) {
  29. this.executesTimesLeft = executes;
  30. this.id = id;
  31. this.periodSeconds = periodSeconds;
  32. this.scheduler = scheduler;
  33. }
  34.  
  35. private void performAction() {
  36. long beofre = System.currentTimeMillis();
  37. long time = (beofre - lastExecution) % 1_000_000;
  38. lastExecution = beofre;
  39.  
  40. // Simulates useful calculations
  41. try {
  42. Thread.sleep(1000);
  43. } catch (InterruptedException ex) {
  44. }
  45.  
  46. long after = System.currentTimeMillis();
  47. if (id % 100_000 == 0) {
  48. long duration = after - beofre;
  49. System.out.println("Time since prev execution:\t" + time + "\t"
  50. + "Task " + id + ": "
  51. + executesTimesLeft + " executions lefts; "
  52. + "current duration\t" + duration);
  53. }
  54.  
  55. }
  56.  
  57. @Override
  58. public void run() {
  59. // perform useful calculation in another thread
  60. new Thread(() -> performAction()).run();
  61.  
  62. executesTimesLeft--;
  63. if (executesTimesLeft > 0) { // schedule next task execution
  64. scheduler.schedule(this, periodSeconds, SECONDS);
  65. }
  66. }
  67.  
  68. }
  69.  
  70.  
  71. /**
  72.  *
  73.  * @author Wsl_F
  74.  */
  75. class SchedulersCreator {
  76.  
  77. private final ScheduledExecutorService scheduler
  78. = Executors.newScheduledThreadPool(1);
  79.  
  80. public SchedulersCreator(int tasksAmount, int repeatCount) {
  81. for (int taskId = 0; taskId <= tasksAmount; taskId++) {
  82. // create new task, that executes every 2 seconds
  83. MyTask task = new MyTask(scheduler, repeatCount, 2, taskId);
  84. // execute new task
  85. task.run();
  86. }
  87. }
  88.  
  89. public static void main(String[] args) {
  90. System.out.println("Program started");
  91. // create & start 10 tasks, each of the executes 10 times with period 2 seconds
  92. SchedulersCreator scheduler = new SchedulersCreator(10, 3);
  93. System.out.println("All tasks created & started");
  94. }
  95. }
  96.  
Time limit exceeded #stdin #stdout 5s 4837376KB
stdin
Standard input is empty
stdout
Program started
Time since prev execution:	287019	Task 0: 3 executions lefts; current duration	1000
Time since prev execution:	3028	Task 0: 2 executions lefts; current duration	1000
Time since prev execution:	4001	Task 0: 1 executions lefts; current duration	1001