Implementing a timer thread

Use java.util.Timer. The following examples registers two tasks, executes one after 10 seconds and another one repeatedly every 2 seconds.

Main.java:

import java.util.*;
import java.io.*;
 
public class Main
{
   public static void main(String []args) {
      Task t1 = new Task("Task #1");
      Task t2 = new Task("Task #2");
 
      Timer t = new Timer();
      t.schedule(t1, 10000);
      t.schedule(t2, 1000, 2000);
   }
}
 
class Task extends TimerTask
{
   private String name;
 
   public Task(String name) {
      this.name = name;
   }
  
   public void run() {
      System.out.println("[" + new Date() + "] " + name + ": task executed!");
   }
}

outputs:

[Tue Dec 10 00:28:57 CET 2002] Task #2: task executed!
[Tue Dec 10 00:28:59 CET 2002] Task #2: task executed!
[Tue Dec 10 00:29:01 CET 2002] Task #2: task executed!
[Tue Dec 10 00:29:03 CET 2002] Task #2: task executed!
[Tue Dec 10 00:29:05 CET 2002] Task #2: task executed!
[Tue Dec 10 00:29:06 CET 2002] Task #1: task executed!
[Tue Dec 10 00:29:07 CET 2002] Task #2: task executed!
[Tue Dec 10 00:29:09 CET 2002] Task #2: task executed!
[Tue Dec 10 00:29:11 CET 2002] Task #2: task executed!