001 /*
002 * Copyright 2009-2013 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2009-2013 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021 package com.unboundid.util;
022
023
024
025 import java.io.Serializable;
026 import java.util.concurrent.atomic.AtomicBoolean;
027 import java.util.concurrent.atomic.AtomicLong;
028
029
030
031 /**
032 * This class provides a utility that can be used to sleep for a specified
033 * period of time in a manner that allows it to be woken up if necessary. A
034 * single instance of this class may only be used to allow one thread to sleep
035 * at any given time, so if multiple threads need to sleep at the same time then
036 * a separate {@code WakeableSleeper} instance should be used for each.
037 */
038 @ThreadSafety(level=ThreadSafetyLevel.MOSTLY_NOT_THREADSAFE)
039 public final class WakeableSleeper
040 implements Serializable
041 {
042 /**
043 * The serial version UID for this serializable class.
044 */
045 private static final long serialVersionUID = 755656862953269760L;
046
047
048
049 // A flag used to prevent multiple concurrent attempts to sleep.
050 private final AtomicBoolean sleeping;
051
052 // The number of attempts to wake up this sleeper.
053 private final AtomicLong wakeupCount;
054
055
056
057 /**
058 * Creates a new instance of this wakeable sleeper.
059 */
060 public WakeableSleeper()
061 {
062 sleeping = new AtomicBoolean(false);
063 wakeupCount = new AtomicLong(0L);
064 }
065
066
067
068 /**
069 * Attempts to sleep for the specified length of time in milliseconds, subject
070 * to the accuracy available within the JVM and underlying system. It may
071 * wake up prematurely if the wakeup method is called, or if the thread is
072 * interrupted.
073 * <BR><BR>
074 * This method must not be called on the same {@code WakeableSleeper} instance
075 * by multiple threads at the same time.
076 *
077 * @param time The length of time in milliseconds to sleep.
078 *
079 * @return {@code true} if the sleep completed, or {@code false} if it was
080 * woken or interrupted prematurely.
081 */
082 @ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
083 public boolean sleep(final long time)
084 {
085 synchronized (wakeupCount)
086 {
087 Validator.ensureTrue(sleeping.compareAndSet(false, true),
088 "WakeableSleeper.sleep() must not be invoked concurrently by " +
089 "multiple threads against the same instance.");
090
091 try
092 {
093 final long beforeCount = wakeupCount.get();
094 wakeupCount.wait(time);
095 final long afterCount = wakeupCount.get();
096 return (beforeCount == afterCount);
097 }
098 catch (final InterruptedException ie)
099 {
100 Debug.debugException(ie);
101 return false;
102 }
103 finally
104 {
105 sleeping.set(false);
106 }
107 }
108 }
109
110
111
112 /**
113 * Indicates that the sleeper should wake up if it is currently sleeping.
114 * This method will not make any attempt to ensure that the thread had woken
115 * up before returning. If multiple threads attempt to wake up the sleeper at
116 * the same time, then it will have the same effect as a single wakeup
117 * request.
118 */
119 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
120 public void wakeup()
121 {
122 synchronized (wakeupCount)
123 {
124 wakeupCount.incrementAndGet();
125 wakeupCount.notifyAll();
126 }
127 }
128 }