1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * ident "%Z%%M% %I% %E% SMI" 27 */ 28 29 package com.sun.solaris.service.timer; 30 31 /** 32 * A recurring event timer. Like all Java time consumers, which rely on 33 * wall time, adjustments to the time of day will adversely affect the 34 * accuracy of the period. 35 */ 36 public class SimpleRecurringEventTimer implements RecurringEventTimer 37 { 38 /** 39 * Next time the event is to occur. 40 */ 41 private long nextFiring; 42 43 /** 44 * Period between recurrences of the event, in milliseconds. 45 */ 46 private long period; 47 48 /** 49 * Multiplier to convert seconds to milliseconds. 50 */ 51 public static final int SEC = 1000; 52 53 /** 54 * Establishes a timer which will fire every 'period' 55 * milliseconds starting from now. 56 */ SimpleRecurringEventTimer(long period)57 public SimpleRecurringEventTimer(long period) 58 { 59 this.period = period; 60 nextFiring = System.currentTimeMillis() + period; 61 } 62 63 /** 64 * Cause the current thread to wait until at least the time of 65 * the next event, as near as possible, unless interrupted. 66 * 67 * @throws InterruptedException if the thread is interrupted 68 * while waiting for the next firing. Subsequent calls to this 69 * method will wait for the same firing. 70 */ waitUntilNextFiring()71 public void waitUntilNextFiring() throws InterruptedException 72 { 73 long delta; 74 75 while ((delta = nextFiring - System.currentTimeMillis()) > 0) 76 Thread.sleep(delta); 77 78 nextFiring += period; 79 } 80 } 81