Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,342 members, 7,954,398 topics. Date: Friday, 20 September 2024 at 06:06 PM |
Nairaland Forum / Science/Technology / Programming / Java Coding Challenge: Task Scheduler (8487 Views)
Mini Web Application Coding Challenge For Programmers / Coding Challenge 5: Substring Generator / Appointment Scheduler In Asp (2) (3) (4)
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] |
Having Consistent Headaches Because Of Programming / Data Science Or Cyber Security? / Creating Apps In Kivy By Dusty Phillips. Ebook Download
(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. 28 |