001/* 002 * Copyright 2009-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2009-2024 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2009-2024 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util; 037 038 039 040import java.io.Serializable; 041import java.util.concurrent.atomic.AtomicBoolean; 042import java.util.concurrent.atomic.AtomicLong; 043 044 045 046/** 047 * This class provides a utility that can be used to sleep for a specified 048 * period of time in a manner that allows it to be woken up if necessary. A 049 * single instance of this class may only be used to allow one thread to sleep 050 * at any given time, so if multiple threads need to sleep at the same time then 051 * a separate {@code WakeableSleeper} instance should be used for each. 052 */ 053@ThreadSafety(level=ThreadSafetyLevel.MOSTLY_NOT_THREADSAFE) 054public final class WakeableSleeper 055 implements Serializable 056{ 057 /** 058 * The serial version UID for this serializable class. 059 */ 060 private static final long serialVersionUID = 755656862953269760L; 061 062 063 064 // A flag used to prevent multiple concurrent attempts to sleep. 065 @NotNull private final AtomicBoolean sleeping; 066 067 // A flag used to indicate that this WakeableSleeper has been shut down. 068 @NotNull private final AtomicBoolean shutDown; 069 070 // The number of attempts to wake up this sleeper. 071 @NotNull private final AtomicLong wakeupCount; 072 073 074 075 /** 076 * Creates a new instance of this wakeable sleeper. 077 */ 078 public WakeableSleeper() 079 { 080 sleeping = new AtomicBoolean(false); 081 shutDown = new AtomicBoolean(false); 082 wakeupCount = new AtomicLong(0L); 083 } 084 085 086 087 /** 088 * Return {@code true} if this {@code WakeableSleeper} instance has been 089 * shutdown via the {@code shutDown()} method and {@code false} otherwise. 090 * 091 * @return {@code true} if this {@code WakeableSleeper} instance has been 092 * shutdown via the {@code shutDown()} method and {@code false} 093 * otherwise. 094 */ 095 public boolean isShutDown() 096 { 097 return shutDown.get(); 098 } 099 100 101 102 /** 103 * Attempts to sleep for the specified length of time in milliseconds, subject 104 * to the accuracy available within the JVM and underlying system. It may 105 * wake up prematurely if the wakeup method is called, or if the thread is 106 * interrupted. If {@code shutDown()} is called, then any active caller of 107 * this method will return immediately, and subsequent calls will return 108 * without sleeping. 109 * <BR><BR> 110 * This method must not be called on the same {@code WakeableSleeper} instance 111 * by multiple threads at the same time. 112 * 113 * @param time The length of time in milliseconds to sleep. 114 * 115 * @return {@code true} if the sleep completed, or {@code false} if it was 116 * woken or interrupted prematurely. 117 */ 118 @ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE) 119 public boolean sleep(final long time) 120 { 121 synchronized (wakeupCount) 122 { 123 if (isShutDown()) 124 { 125 return false; 126 } 127 128 Validator.ensureTrue(sleeping.compareAndSet(false, true), 129 "WakeableSleeper.sleep() must not be invoked concurrently by " + 130 "multiple threads against the same instance."); 131 132 try 133 { 134 final long beforeCount = wakeupCount.get(); 135 wakeupCount.wait(time); 136 final long afterCount = wakeupCount.get(); 137 return (beforeCount == afterCount); 138 } 139 catch (final InterruptedException ie) 140 { 141 Debug.debugException(ie); 142 return false; 143 } 144 finally 145 { 146 sleeping.set(false); 147 } 148 } 149 } 150 151 152 153 /** 154 * Permanently shuts down this {@code WakeableSleeper} instance. If a thread 155 * is currently blocked in the {@code sleep} method, it will return 156 * immediately, and all subsequent calls to that method will return without 157 * sleeping. It is safe to call this method multiple times. 158 */ 159 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 160 public void shutDown() 161 { 162 shutDown.set(true); 163 wakeup(); 164 } 165 166 167 168 /** 169 * Indicates that the sleeper should wake up if it is currently sleeping. 170 * This method will not make any attempt to ensure that the thread had woken 171 * up before returning. If multiple threads attempt to wake up the sleeper at 172 * the same time, then it will have the same effect as a single wakeup 173 * request. 174 */ 175 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 176 public void wakeup() 177 { 178 synchronized (wakeupCount) 179 { 180 wakeupCount.incrementAndGet(); 181 wakeupCount.notifyAll(); 182 } 183 } 184}