Cancel Aware Timer : Timer « Development Class « Java






Cancel Aware Timer

      
/*
 * @(#)$Id: codetemplate_xbird.xml 943 2006-09-13 07:03:37Z yui $
 *
 * Copyright 2006-2008 Makoto YUI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Contributors:
 *     Makoto YUI - initial implementation
 */
//package xbird.util.lang;

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

/**
 * 
 * <DIV lang="en"></DIV>
 * <DIV lang="ja"></DIV>
 * 
 * @author Makoto YUI (yuin405+xbird@gmail.com)
 */
public final class CancelAwareTimer extends Timer {

    private int numSched = 0;
    private boolean canceled = false;

    public CancelAwareTimer() {
        super();
    }

    public CancelAwareTimer(boolean isDaemon) {
        super(isDaemon);
    }

    public CancelAwareTimer(String name) {
        super(name);
    }

    public CancelAwareTimer(String name, boolean isDaemon) {
        super(name, isDaemon);
    }

    public int getNumberOfScheduled() {
        return numSched;
    }

    @Override
    public void cancel() {
        boolean cancel = canceled;
        this.canceled = true;
        if(!cancel) {
            super.cancel();
        }
    }

    @Override
    public int purge() {
        if(canceled) {
            return 0;
        }
        numSched++;
        return super.purge();
    }

    @Override
    public void schedule(TimerTask task, Date firstTime, long period) {
        if(canceled) {
            return;
        }
        numSched++;
        super.schedule(task, firstTime, period);
    }

    @Override
    public void schedule(TimerTask task, Date time) {
        if(canceled) {
            return;
        }
        numSched++;
        super.schedule(task, time);
    }

    @Override
    public void schedule(TimerTask task, long delay, long period) {
        if(canceled) {
            return;
        }
        numSched++;
        super.schedule(task, delay, period);
    }

    @Override
    public void schedule(TimerTask task, long delay) {
        if(canceled) {
            return;
        }
        numSched++;
        super.schedule(task, delay);
    }

    @Override
    public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) {
        if(canceled) {
            return;
        }
        numSched++;
        super.scheduleAtFixedRate(task, firstTime, period);
    }

    @Override
    public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
        if(canceled) {
            return;
        }
        numSched++;
        super.scheduleAtFixedRate(task, delay, period);
    }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Timer Skipping BeepTimer Skipping Beep
2.Timer Schedule a task that executes once every secondTimer Schedule a task that executes once every second
3.Use java.util.Timer to schedule a task to execute once 5 seconds have passedUse java.util.Timer to schedule a task to execute once 5 seconds have passed
4.Timer utilities
5.Timer and TimerTask Classes
6.Pause and start a timer task
7.Create a Timer object
8.Swing also provide a Timer class. A Timer object will send an ActionEvent to the registered ActionListener.
9.Schedule a task by using Timer and TimerTask.
10.Scheduling a Timer Task to Run Repeatedly
11.Create a scheduled task using timer
12.extends TimerTask to create your own task
13.A simple implementation of the Java 1.3 java.util.Timer API
14.Scheduling a Timer Task to Run at a Certain Time
15.Class encapsulating timer functionality
16.Timeout Observer
17.A pool of objects that should only be used by one thread at a time
18.A class that allows a programmer to determine the amount of time spend doing certain routines
19.All times in this Timer object are in milliseconds. The timer starts from the moment of it's creation.
20.Thread Timer
21.HRTimer is a simple system-wide timer facility using a singleton Timer, with additional instrumentation.
22.Used for timing events with millisecond precision.
23.A class to wait until a condition becomes true.