201 return cleaner;
202 }
203
204 /**
205 * Registers an object and a cleaning action to run when the object
206 * becomes phantom reachable.
207 * Refer to the <a href="#compatible-cleaners">API Note</a> above for
208 * cautions about the behavior of cleaning actions.
209 *
210 * <p>The given object is kept strongly reachable (and therefore not eligible
211 * for cleaning) during the register() method.
212 *
213 * <p>{@linkplain java.lang.ref##MemoryConsistency Memory consistency effects}:
214 * Actions in a thread prior to calling {@code Cleaner.register()}
215 * <a href="{@docRoot}/java.base/java/util/concurrent/package-summary.html#MemoryVisibility"><i>happen-before</i></a>
216 * the cleaning action is run by the Cleaner's thread.
217 *
218 * @param obj the object to monitor
219 * @param action a {@code Runnable} to invoke when the object becomes phantom reachable
220 * @return a {@code Cleanable} instance
221 */
222 public Cleanable register(@jdk.internal.RequiresIdentity Object obj, Runnable action) {
223 Objects.requireNonNull(obj, "obj");
224 Objects.requireNonNull(action, "action");
225 return new CleanerImpl.PhantomCleanableRef(obj, this, action);
226 }
227
228 /**
229 * {@code Cleanable} represents an object and a
230 * cleaning action registered in a {@code Cleaner}.
231 * @since 9
232 */
233 public interface Cleanable {
234 /**
235 * Unregisters the cleanable and invokes the cleaning action.
236 * The cleanable's cleaning action is invoked at most once
237 * regardless of the number of calls to {@code clean}.
238 */
239 void clean();
240 }
241
242 }
|
201 return cleaner;
202 }
203
204 /**
205 * Registers an object and a cleaning action to run when the object
206 * becomes phantom reachable.
207 * Refer to the <a href="#compatible-cleaners">API Note</a> above for
208 * cautions about the behavior of cleaning actions.
209 *
210 * <p>The given object is kept strongly reachable (and therefore not eligible
211 * for cleaning) during the register() method.
212 *
213 * <p>{@linkplain java.lang.ref##MemoryConsistency Memory consistency effects}:
214 * Actions in a thread prior to calling {@code Cleaner.register()}
215 * <a href="{@docRoot}/java.base/java/util/concurrent/package-summary.html#MemoryVisibility"><i>happen-before</i></a>
216 * the cleaning action is run by the Cleaner's thread.
217 *
218 * @param obj the object to monitor
219 * @param action a {@code Runnable} to invoke when the object becomes phantom reachable
220 * @return a {@code Cleanable} instance
221 * @throws IdentityException if the object is not an
222 * {@link java.util.Objects#hasIdentity(Object) identity object}
223 */
224 public Cleanable register(@jdk.internal.RequiresIdentity Object obj, Runnable action) {
225 Objects.requireIdentity(obj, "obj");
226 Objects.requireNonNull(action, "action");
227 return new CleanerImpl.PhantomCleanableRef(obj, this, action);
228 }
229
230 /**
231 * {@code Cleanable} represents an object and a
232 * cleaning action registered in a {@code Cleaner}.
233 * @since 9
234 */
235 public interface Cleanable {
236 /**
237 * Unregisters the cleanable and invokes the cleaning action.
238 * The cleanable's cleaning action is invoked at most once
239 * regardless of the number of calls to {@code clean}.
240 */
241 void clean();
242 }
243
244 }
|