Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,369 members, 7,808,288 topics. Date: Thursday, 25 April 2024 at 09:55 AM

Java Coding Challenge: Task Scheduler - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Coding Challenge: Task Scheduler (8389 Views)

Mini Web Application Coding Challenge For Programmers / Coding Challenge 5: Substring Generator / Appointment Scheduler In Asp (2) (3) (4)

(1) (2) (Reply)

Re: Java Coding Challenge: Task Scheduler by candylips(m): 10:09am On Aug 08, 2011
My solution

ExponentialTimer.java

package challenge;

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

/**
* Implements an exponential timer (recursive definition) with a steady state.
*
*
*/

public class ExponentialTimer
{

    /*
    * By default, this exponential timer reaches steady state at 900 seconds. (15 mins)
    */

    public static final int DEFAULT_STEADY_STATE_SECONDS = 900;

    private int steadyStateInSeconds;

    private Timer timer;

    public ExponentialTimer()
    {
        steadyStateInSeconds = DEFAULT_STEADY_STATE_SECONDS;

    }

    public ExponentialTimer(int seconds)
    {
        steadyStateInSeconds = seconds;
    }

    public void invoke(Task task)
    {
        scheduleIn(task, 1, 1);
    }

    /**
     * Schedules the timer to invoke the task in the specified delay.
     *
     * @param task
     * @param delay
     * @param retryCount
     */

    void scheduleIn(Task task, long delay, int retryCount)
    {
        timer = new Timer(false);
        timer.schedule(new InvokeTaskTimerTask(task, delay, retryCount), delay * 1000);
    }

    /**
     * If specified, this timer reaches steady state in so many seconds.
     *
     * @param steadyStateInSeconds
     *            the steadyStateInSeconds to set
     */

    public void setSteadyStateInSeconds(int steadyStateInSeconds)
    {
        this.steadyStateInSeconds = steadyStateInSeconds;
    }

    /**
     * Responsible for the retry logic.
     */

    class InvokeTaskTimerTask extends TimerTask
    {

        private final Task task;

        private final long currentDelay;

        private final int retryCount;

        /**
         * @param task
         */

        public InvokeTaskTimerTask(Task task, long currentDelay, int retryCount)
        {
            this.task = task;
            this.currentDelay = currentDelay;
            this.retryCount = retryCount;
        }

        /**
         * Invoked by the scheduled timer.
         */

        @Override
        public void run()
        {
            System.out.println("Executing task. . . #" + retryCount);
            try
            {
                task.execute();
                timer.cancel();
            } catch (Exception e)
            {
                System.out.println("Error occurred while invoking task. : " + e);
            }

            if (currentDelay < steadyStateInSeconds)
            {
                long nextDelay = currentDelay * 2 > steadyStateInSeconds ? steadyStateInSeconds : currentDelay * 2;
                System.out.println("Re-scheduling task to run in " + nextDelay + " seconds");
                scheduleIn(task, nextDelay, retryCount + 1);
            } else
            {
                System.out.println(steadyStateInSeconds + " seconds steadystate has been reached. Task Completed!");
            }
        }
    }
}


Task.java

package challenge;

public interface Task
{

    void execute();

}



SampleTask.java

package challenge;

public class SampleTask implements Task
{

    @Override
    public void execute()
    {
        System.out.println("Successfully ran sampleTask!");

    }

}



TimerMain.java

package challenge;

public class TimerMain
{

    public static void main(String[] args) throws InterruptedException
    {

        ExponentialTimer exponentialTimer = new ExponentialTimer(8);
        exponentialTimer.invoke(new SampleTask());

    }

}



Sample Results


Executing task. . . #1
Successfully ran sampleTask!
Re-scheduling task to run in 2 seconds
Executing task. . . #2
Successfully ran sampleTask!
Re-scheduling task to run in 4 seconds
Executing task. . . #3
Successfully ran sampleTask!
Re-scheduling task to run in 8 seconds
Executing task. . . #4
Successfully ran sampleTask!
8 seconds steadystate has been reached. Task Completed!
Re: Java Coding Challenge: Task Scheduler by Fayimora(m): 6:29pm On Aug 09, 2011
[size=18pt]THREAD CLEANED UP![/size]

(1) (2) (Reply)

Java Vs C Sharp / How To Deploy Application With Access Database In Vb.net 2008 / Phpbrowserbox Update - Google Chrome + PHP/MySQL Server In One!

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 31
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.