What happens if the code is not completed on time

If I set a timer to execute code every 3 seconds. If the code is not completed after 3 seconds, what will happen? The computer will run the code or wait for the code to finish or continue the timer and at the same time execute the code with the incomplete code.

If the computer will execute code with incomplete code at the same time, what will happen if the variable is associated with the method. For example, the first line of the launch may do i--, but the last line is i ++. If it starts at the same time when the incomplete code is still running, but a new work cycle begins, the value i is added by the new cycle loop, so when the strong cycle runs to the last line, the value i will be erroneous (because the new work cycle does i--, before the code is finished). If so, how can this be avoided?

int delay = 0; // delay for 0 sec. int period = 3000; // repeat 3 sec. int i = 0; Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { i--; // Task here ... // It may take more than 3 sec to finish, what will happen? i++; } }, delay, period); 
+4
source share
3 answers

Each Timer uses only one thread to service its tasks. The timer thread will run your task until it finishes, and only then try to schedule its next execution. The documentation for Timer confirms this problem and warns its users that tasks are "grouped."

After completing your task, the manager Timer will then try to schedule it again. The next scheduled execution is likely to be in the past, according to the allocation policy Timer$TimerThread#mainLoop() : adds the task period to the last scheduled task execution time. Since in your case the planned execution time will be more than three seconds in the past, adding three seconds to it still gives the next planned execution time in the past.

It's good; the algorithm allows such slippage. Your task will be launched immediately after the last launch. What you do not receive is your desire once every three seconds. Instead, you will receive as often as possible, but not more often than every three seconds.

+5
source

The code will not run at the same time. Read this: http://java.sun.com/javase/6/docs/api/java/util/Timer.html

According to the schedule of AtFixedRate:

In a fixed-rate execution, each execution is planned relative to the planned execution time of the initial execution. If execution is delayed for any reason (for example, garbage collection or other background activity), two or more executions will be executed in quick succession to โ€œcatch upโ€. Ultimately, the execution frequency will exactly match the specified period (provided that the system clock underlying the Object.wait (long) is accurate).

+3
source

When looking at the code, there is only one thread, so if the first start of the task takes more than 3 seconds, it will be restored immediately after completion. Also pay attention to the documentation :

In a fixed-rate execution, each execution is planned relative to the planned execution time of the initial execution. If execution is delayed for any reason (for example, garbage collection or other background activity), two or more executions will be executed in quick succession to โ€œcatch upโ€.

If you need to start another thread, you should look at the ScheduledThreadPoolExecutor .

+1
source

Source: https://habr.com/ru/post/1306143/


All Articles