Showing posts with label java-timer-task. Show all posts
Showing posts with label java-timer-task. Show all posts

Thursday, November 2, 2017

Java Timer TimerTask Example | Use java.util.Timer to schedule a task to execute every 1 second interval

Java Timer TimerTask Example | Use java.util.Timer to schedule a task to execute every 1 second interval



package com.pkm;

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

/**
 * Created by pritom on 31/10/2017.
 */
public class JavaTimerTask {
    static ThreadLocal threadLocal = new ThreadLocal();

    public static void main(String[] args) throws Exception {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Integer count = threadLocal.get() != null ? Integer.parseInt(threadLocal.get().toString()) : 0;
                threadLocal.set((count + 1));
                System.out.println("Executed-at=" + (new Date()) +
                        ", Name=" + Thread.currentThread().getName() +
                        ", ThreadLocal=" + threadLocal.get());
                if (count >= 9) {
                    this.cancel();
                }
            }
        };
        new Timer().schedule(task, 1000L, 1000L);

        task = new TimerTask() {
            @Override
            public void run() {
                Integer count = threadLocal.get() != null ? Integer.parseInt(threadLocal.get().toString()) : 0;
                threadLocal.set((count + 1));
                System.out.println("Executed-another-at=" + (new Date()) +
                        ", Name=" + Thread.currentThread().getName() +
                        ", ThreadLocal=" + threadLocal.get());
                if (count >= 9) {
                    this.cancel();
                }
            }
        };
        new Timer().schedule(task, 5000L, 1000L);
        /**
         * First parameter is task
         * Second parameter is delay time
         * Third parameter (optional) if you want some periodic execution of task
         *
         * And you finally noticed that though "ThreadLocal" is a static field in main class
         * but value for each thread is different, that means "ThreadLocal" is a variable
         * that is unique for each thread, its scope is thread wise, not class wise
         */

        /* You can cancel timer if you need */
        /* timer.cancel(); */
    }
}


And output be like below:


Executed-at=Thu Nov 02 08:12:51 BDT 2017, Name=Timer-0, ThreadLocal=1
Executed-at=Thu Nov 02 08:12:52 BDT 2017, Name=Timer-0, ThreadLocal=2
Executed-at=Thu Nov 02 08:12:53 BDT 2017, Name=Timer-0, ThreadLocal=3
Executed-at=Thu Nov 02 08:12:54 BDT 2017, Name=Timer-0, ThreadLocal=4
Executed-at=Thu Nov 02 08:12:55 BDT 2017, Name=Timer-0, ThreadLocal=5
Executed-another-at=Thu Nov 02 08:12:55 BDT 2017, Name=Timer-1, ThreadLocal=1
Executed-at=Thu Nov 02 08:12:56 BDT 2017, Name=Timer-0, ThreadLocal=6
Executed-another-at=Thu Nov 02 08:12:56 BDT 2017, Name=Timer-1, ThreadLocal=2
Executed-at=Thu Nov 02 08:12:57 BDT 2017, Name=Timer-0, ThreadLocal=7
Executed-another-at=Thu Nov 02 08:12:57 BDT 2017, Name=Timer-1, ThreadLocal=3
Executed-at=Thu Nov 02 08:12:58 BDT 2017, Name=Timer-0, ThreadLocal=8
Executed-another-at=Thu Nov 02 08:12:58 BDT 2017, Name=Timer-1, ThreadLocal=4
Executed-another-at=Thu Nov 02 08:12:59 BDT 2017, Name=Timer-1, ThreadLocal=5
Executed-at=Thu Nov 02 08:12:59 BDT 2017, Name=Timer-0, ThreadLocal=9
Executed-at=Thu Nov 02 08:13:00 BDT 2017, Name=Timer-0, ThreadLocal=10
Executed-another-at=Thu Nov 02 08:13:00 BDT 2017, Name=Timer-1, ThreadLocal=6
Executed-another-at=Thu Nov 02 08:13:01 BDT 2017, Name=Timer-1, ThreadLocal=7
Executed-another-at=Thu Nov 02 08:13:02 BDT 2017, Name=Timer-1, ThreadLocal=8
Executed-another-at=Thu Nov 02 08:13:03 BDT 2017, Name=Timer-1, ThreadLocal=9
Executed-another-at=Thu Nov 02 08:13:04 BDT 2017, Name=Timer-1, ThreadLocal=10