WatchdogTimer.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.concurrent;
00018 
00019 import java.util.concurrent.ScheduledExecutorService;
00020 import java.util.concurrent.ScheduledFuture;
00021 import java.util.concurrent.TimeUnit;
00022 
00031 public class WatchdogTimer {
00032 
00033   private final ScheduledExecutorService scheduledExecutorService;
00034   private final long period;
00035   private final TimeUnit unit;
00036   private final Runnable runnable;
00037 
00038   private boolean pulsed;
00039   private ScheduledFuture<?> scheduledFuture;
00040 
00041   public WatchdogTimer(ScheduledExecutorService scheduledExecutorService, long period,
00042       TimeUnit unit, final Runnable runnable) {
00043     this.scheduledExecutorService = scheduledExecutorService;
00044     this.period = period;
00045     this.unit = unit;
00046     this.runnable = new Runnable() {
00047       @Override
00048       public void run() {
00049         try {
00050           if (!pulsed) {
00051             runnable.run();
00052           }
00053         } finally {
00054           pulsed = false;
00055         }
00056       }
00057     };
00058     pulsed = false;
00059   }
00060 
00061   public void start() {
00062     scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(runnable, period, period, unit);
00063   }
00064 
00065   public void pulse() {
00066     pulsed = true;
00067   }
00068 
00069   public void cancel() {
00070     scheduledFuture.cancel(true);
00071   }
00072 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49