user1906154
user1906154

Reputation: 57

Regarding timer utility

I am developing a class that will schedule a task, and if that task took more than 2 minutes then I will show a message that task is finished forcefully since it took more than 2 minutes for task, and if the task is completed within 2 minutes or before than I will show the message that task is completed before 2 minutes itself, Now the challenge is that I want a dummy task let say a loop first to test it , Please advise how to achieve that , Below is the code that I have tried so far..

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

public class Reminder {
    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);
   }

    class RemindTask extends TimerTask { // Nested Class
        public void run() {
             //hOW TO SET THE any kind of task which takes more than 5 minutes any loop or any sort of thing
        //   If elapsed time is > 50 minutes, something is not right
            System.out.format("Time's up since it takes more than 5 minutes....!%n");
            timer.cancel(); //Terminate the timer thread
        }
    }

    public static void main(String args[]) {
        new Reminder(5);
        System.out.format("Task scheduled.%n");

    }
}

Upvotes: 0

Views: 98

Answers (1)

s.d
s.d

Reputation: 29436

This will get you started :

public abstract class LimitedTask {
    private final long timeOut;
    private final Timer timer;
    private final AtomicBoolean flag;

    protected LimitedTask(long timeOut) {
        this.timeOut = timeOut;
        this.timer = new Timer("Limiter",true);
        this.flag = new AtomicBoolean(false);
    }

    public void execute(){

       //---worker--
       final Thread runner = new Thread(new Runnable() {
           @Override
           public void run() {
               try{
                   doTaskWork();
               }catch (Exception e){
                   e.printStackTrace();
               }finally {
                   if(flag.compareAndSet(false,true)){
                       timer.cancel();
                       onFinish(false);
                   }
               }
           }
       },"Runner");

       runner.setDaemon(true);
       runner.start();

       //--timer--
       this.timer.schedule(new TimerTask() {
           @Override
           public void run() {

               runner.interrupt();

               if(flag.compareAndSet(false,true)){
                   onFinish(true);
               }
           }
       },this.timeOut);
    }

    public abstract void onFinish(boolean timedOut);

    public abstract void doTaskWork() throws Exception;

}

Test implementation:

public class TestTask extends LimitedTask {
    public TestTask() {
        super(10000);
    }

    @Override
    public void onFinish(boolean timedOut) {
        System.out.println(timedOut ? "Task timed out" : "Task completed");
    }

    @Override
    public void doTaskWork() throws Exception {
        for (int i = 0; i <100 ; i++){
            Thread.sleep(1000);
        }
    }
}

Run:

TestTask t = new TestTask();
t.execute();

Upvotes: 1

Related Questions