204 return cleaner; 205 } 206 207 /** 208 * Registers an object and a cleaning action to run when the object 209 * becomes phantom reachable. 210 * Refer to the <a href="#compatible-cleaners">API Note</a> above for 211 * cautions about the behavior of cleaning actions. 212 * 213 * @param obj the object to monitor 214 * @param action a {@code Runnable} to invoke when the object becomes phantom reachable 215 * @return a {@code Cleanable} instance 216 */ 217 public Cleanable register(Object obj, Runnable action) { 218 Objects.requireNonNull(obj, "obj"); 219 Objects.requireNonNull(action, "action"); 220 return new CleanerImpl.PhantomCleanableRef(obj, this, action); 221 } 222 223 /** 224 * {@code Cleanable} represents an object and a 225 * cleaning action registered in a {@code Cleaner}. 226 * @since 9 227 */ 228 public interface Cleanable { 229 /** 230 * Unregisters the cleanable and invokes the cleaning action. 231 * The cleanable's cleaning action is invoked at most once 232 * regardless of the number of calls to {@code clean}. 233 */ 234 void clean(); 235 } 236 237 } | 204 return cleaner; 205 } 206 207 /** 208 * Registers an object and a cleaning action to run when the object 209 * becomes phantom reachable. 210 * Refer to the <a href="#compatible-cleaners">API Note</a> above for 211 * cautions about the behavior of cleaning actions. 212 * 213 * @param obj the object to monitor 214 * @param action a {@code Runnable} to invoke when the object becomes phantom reachable 215 * @return a {@code Cleanable} instance 216 */ 217 public Cleanable register(Object obj, Runnable action) { 218 Objects.requireNonNull(obj, "obj"); 219 Objects.requireNonNull(action, "action"); 220 return new CleanerImpl.PhantomCleanableRef(obj, this, action); 221 } 222 223 /** 224 * Processes next Cleanable that has been waiting in the queue. 225 * 226 * @return {@code true} if a Cleanable was found in the queue and 227 * was processed or {@code false} if the queue was empty. 228 */ 229 boolean cleanNextEnqueued() { 230 return impl.cleanNextEnqueued(); 231 } 232 233 /** 234 * {@code Cleanable} represents an object and a 235 * cleaning action registered in a {@code Cleaner}. 236 * @since 9 237 */ 238 public interface Cleanable { 239 /** 240 * Unregisters the cleanable and invokes the cleaning action. 241 * The cleanable's cleaning action is invoked at most once 242 * regardless of the number of calls to {@code clean}. 243 */ 244 void clean(); 245 } 246 247 } |