[Java] Timer processing implementation method


This time, I will explain how to implement Java timer processing. In this sample, we will implement the process of creating a specific task and executing that task after a few seconds.

After that, it will be explained in the following versions and environments.

IDE:eclipse Java version: 8

Then, I will explain it immediately.

1. Folder structure

The folder structure of the sample source created this time is as follows.

スクリーンショット 2018-11-14 0.12.56.png

2. Implementation of timer processing

2-1. Timer processing syntax

First, when implementing timer processing, create an object of the TimerTask class.

Then, put the run () method in the TimerTask class and implement the process you want to execute with the timer.

Finally, create an object of the timer class and specify the task and timer time you want to execute with the schedule method of the timer class.

TimerTask task = new TimerTask() {
  public void run() {
    //The process you want to execute with the timer

Timer timer = new Timer();
timer.schedule(task,Timer time(ms));

2-2. Timer processing sample implementation

Using the above syntax, let's implement the process that outputs the phrase "The task has been executed" to the console after 3 seconds.


package main;

import java.util.Timer;
import java.util.TimerTask;

public class SampleTimer {

    public static void main(String[] args) {
        System.out.println("I have set the task to run after 3 seconds.");
        TimerTask task = new TimerTask() {
            public void run() {
                System.out.println("The task has been executed.");

        Timer timer = new Timer();
        timer.schedule(task, 3000);



After completing the above description, right-click SampleTimer.java> Run Java Application to execute it. It is OK if the message "The task has been executed" is output after 3 seconds.


I have set the task to run after 3 seconds.
The task has been executed.

at the end

I started my personal blog in 2020!

Based on the knowledge and experience gained as a freelance engineer, we plan to distribute content such as information on freelance engineers, IT technical information, industry information, and engineer life hacks.

The number of articles is still small, but it is updated weekly, so if you are interested, I would be grateful if you could take a look.


Recommended Posts

[Java] Timer processing implementation method
Java method
java (method)
Java method
[Java] method
[Java] method
[Java] Processing time measurement method memo
Implementation of clone method for Java Record
Java8 method reference
Java thread processing
Java string processing
[Java] Multi-thread processing
java8 method reference
[Java] Random method
[Java] split method
[Java] Stream processing
java iterative processing
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
Rails6 countdown timer implementation
Interpreter implementation in Java
JAVA DB connection method
Check Java toString () implementation
Java learning 2 (learning calculation method)
Java learning memo (method)
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Studying Java 8 (see method)
Boyer-Moore implementation in Java
Java programming (class method)
Heapsort implementation (in java)
JAVA constructor call processing
Java random, various processing
[Java] Basic method notes
Implementation example of simple LISP processing system (Java version)
[Java] New Thread generation method (2)
[Java] Multi-thread processing --Exclusive control
Java GC method determination conditions
Create a java method [Memo] [java11]
[Java] Implementation of Faistel Network
Java test code method collection
[Java Silver] About equals method
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[Java] Random number generation method (Random)
Measured parallel processing in Java
Java methods and method overloads
Understanding Java Concurrent Processing (Introduction)
Implementation of gzip in java
Benefits of Java static method
[Java Silver] Array generation method
Implementation of tri-tree in Java
Summary of java error processing
[Java] New Thread generation method (1)
[Java] Object-oriented syntax --class method / argument
Automatic photo resizing method in Java
Date processing in Java (LocalDate: Initialization)
Java method list (memorial) (under construction)
[Java] How to use join method
Delegate some Java processing to JavaScript
[Java] Loop processing and multiplication table