Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the TimerTask tool in java

Shulou Source: shulou.com Published: 2022-06-03 05:15:28 09月11日 Update

This article introduces the knowledge of "how to use TimerTask tools in java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. JDK1.3 introduces TimerTask tools into Java language. This tool allows you to perform tasks on a regular or delayed basis.

2. The implementation of Timer is very simple, creating a timer thread and building a queue waiting for events according to the execution time.

The TimerTask thread is a daemon thread, so it does not prevent the program from exiting. Because timing events are executed in the Timer thread, you must ensure the correct synchronization of any data items used in tasks in the Timer thread.

Example

Public static void main (String [] args) {Timer timer = new Timer (); final CalculatePrimes calculator = new CalculatePrimes (); calculator.start (); timer.schedule (new TimerTask () {public void run () {calculator.finished = true) }, TEN_SECONDS);} "how to use the TimerTask tool in java" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Threads tools events tasks content more knowledge practicality learning next dilemma timer instance reality situation data data item article time case Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Microsoft vpn macOS