/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testscheduleatfixrate;

import java.io.File;
import java.util.Date;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class TestScheduleAtFixRate {

    

    private static class Runner implements Runnable {

        @Override
        public void run() {

            System.out.println(new Date() + " " + Thread.currentThread().getId() + " " + hashCode() + " inside runnable");

            if (new File("/tmp/tank_it").exists()) {
                System.out.println(new Date() + " " + Thread.currentThread().getId() + " gonna hang");
                try {
                    Thread.currentThread().sleep(Long.MAX_VALUE);
                } catch (InterruptedException ex) {
                    System.out.println(new Date() + " " + Thread.currentThread().getId() + " got interrupted");
                    return;
                }

            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException {
        

        ScheduledThreadPoolExecutor scheduleExecutor = new ScheduledThreadPoolExecutor(8, new RejectHandler());

        ScheduledFuture sceduled = scheduleExecutor.scheduleWithFixedDelay(
                new Runner(), 0, 5, TimeUnit.SECONDS);
 
        while (true) {
            try {
                if (scheduleExecutor.awaitTermination(10, TimeUnit.SECONDS) == true) {
                    System.exit(0);
                }
            } catch (InterruptedException ex) {
                System.exit(1);
            }
        }


    }
}

