From ba38b2af4f73595c9dfc43e3b1ef95510cad400f Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 2 Nov 2014 12:25:35 +0200 Subject: [PATCH] timer and timertask examples --- .../org/baeldung/java/JavaTimerUnitTest.java | 93 ++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/JavaTimerUnitTest.java b/core-java/src/test/java/org/baeldung/java/JavaTimerUnitTest.java index 3bdedc81bd..086d5b8ffe 100644 --- a/core-java/src/test/java/org/baeldung/java/JavaTimerUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/JavaTimerUnitTest.java @@ -3,6 +3,9 @@ package org.baeldung.java; import java.util.Date; import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import org.junit.Test; @@ -15,11 +18,11 @@ public class JavaTimerUnitTest { final TimerTask timerTask = new TimerTask() { @Override public void run() { - System.out.println("Task performed on " + new Date() + "\n" + "Thread's name: " + Thread.currentThread().getName()); + System.out.println("Task performed on: " + new Date() + "\n" + "Thread's name: " + Thread.currentThread().getName()); } }; - final Timer timer = new Timer("Timer"); + final long delay = 1000L; timer.schedule(timerTask, delay); @@ -27,4 +30,90 @@ public class JavaTimerUnitTest { timer.cancel(); } + @Test + public void givenUsingTimer_whenSchedulingRepeatedTask_thenCorrect() throws InterruptedException { + final TimerTask repeatedTask = new TimerTask() { + @Override + public void run() { + System.out.println("Task performed on " + new Date()); + } + }; + final Timer timer = new Timer("Timer"); + + final long delay = 1000L; + final long period = 1000L; + timer.scheduleAtFixedRate(repeatedTask, delay, period); + + Thread.sleep(delay * 2); + timer.cancel(); + } + + @Test + public void givenUsingTimer_whenSchedulingDailyTask_thenCorrect() throws InterruptedException { + final TimerTask repeatedTask = new TimerTask() { + @Override + public void run() { + System.out.println("Task performed on " + new Date()); + } + }; + final Timer timer = new Timer("Timer"); + + final long delay = 1000L; + final long period = 1000L * 60L * 60L * 24L; + timer.scheduleAtFixedRate(repeatedTask, delay, period); + + Thread.sleep(delay * 2); + timer.cancel(); + } + + @Test + public void givenUsingTimer_whenCancelingTimerTask_thenCorrect() throws InterruptedException { + final TimerTask task = new TimerTask() { + @Override + public void run() { + System.out.println("Task performed on " + new Date()); + cancel(); + } + }; + final Timer timer = new Timer("Timer"); + + final long delay = 1000L; + final long period = 1000L; + timer.scheduleAtFixedRate(task, delay, period); + + Thread.sleep(delay * 3); + } + + @Test + public void givenUsingTimer_whenCancelingTimer_thenCorrect() throws InterruptedException { + final TimerTask task = new TimerTask() { + @Override + public void run() { + System.out.println("Task performed on " + new Date()); + } + }; + final Timer timer = new Timer("Timer"); + + timer.scheduleAtFixedRate(task, 1000L, 1000L); + + Thread.sleep(1000L * 2); + timer.cancel(); + } + + @Test + public void givenUsingExecutorService_whenSchedulingRepeatedTask_thenCorrect() throws InterruptedException { + final TimerTask repeatedTask = new TimerTask() { + @Override + public void run() { + System.out.println("Task performed on " + new Date()); + } + }; + final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + final long delay = 1000L; + final long period = 1000L; + executor.scheduleAtFixedRate(repeatedTask, delay, period, TimeUnit.MILLISECONDS); + Thread.sleep(delay + period * 3); + executor.shutdown(); + } + }