Some students may use timer as an auxiliary class for scheduled tasks during scheduled tasks,
However, using it may pose potential risks as follows,
(1) inaccurate Time Calculation
Because timer calculates scheduled tasks based on absolute time and is affected by the system time. If the system time is changed during task execution
As a result, the time calculation is inaccurate, and the task is not executed at the specified time.
(2) Only one task can be executed.
Simply put, only one task can be executed at a time. If the previous task is not completed, the latter task cannot be executed in parallel and can only wait until the previous task is completed.
Can be executed. It is also possible that the execution time of the previous task is too long, and the time of the last few tasks is too short. It is possible that multiple tasks are executed in one time period.
The task is not executed according to the time we want to execute.
(3) asynchronous task termination due to non-check exceptions
When a non-check exception occurs during the execution of timer, this task will fail and the subsequent tasks cannot be executed. timer will terminate the execution.
We need a recovery mechanism for the results we want.
After Java 5, you can use the following class to replace time and solve the above three problems.
Java. util. Concurrent. scheduledthreadpoolexecutor
On Android, you can use Java. util. Concurrent. scheduledthreadpoolexecutor or handler mechanism,
However, timer is not recommended.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service