diff --git a/core-java-modules/core-java-concurrency-simple/src/main/java/com/baeldung/concurrent/startathread/TimerDemoExample.java b/core-java-modules/core-java-concurrency-simple/src/main/java/com/baeldung/concurrent/startathread/TimerDemoExample.java new file mode 100644 index 0000000000..ad59d451d4 --- /dev/null +++ b/core-java-modules/core-java-concurrency-simple/src/main/java/com/baeldung/concurrent/startathread/TimerDemoExample.java @@ -0,0 +1,39 @@ +package com.baeldung.concurrent.startathread; + +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +public class TimerDemoExample { + + private void scheduleOnce() { + TimerTask task = new TimerTask() { + public void run() { + System.out.println("Task performed on: " + new Date() + "\n" + "Thread's name: " + Thread.currentThread() + .getName()); + } + }; + Timer timer = new Timer("Timer"); + long delay = 1000L; + timer.schedule(task, delay); + } + + private void scheduleRecurrently() { + TimerTask task = new TimerTask() { + public void run() { + System.out.println("Recurrent Task performed on: " + new Date() + "\n" + "Thread's name: " + Thread.currentThread() + .getName()); + } + }; + Timer timer = new Timer("Timer"); + long delay = 1000L; + final long period = 1000L; + timer.scheduleAtFixedRate(task, delay, period); + } + + public static void main(String[] args) { + TimerDemoExample timerDemoExample = new TimerDemoExample(); + timerDemoExample.scheduleOnce(); + timerDemoExample.scheduleRecurrently(); + } +}