Skip to content

Commit 21d46ab

Browse files
committed
Added explanation of Cleaner lifecycle
1 parent 094aea6 commit 21d46ab

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/com/sun/jna/internal/Cleaner.java

+11
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ private static synchronized boolean deleteIfEmpty(MasterCleaner caller) {
129129
return caller.cleaners.isEmpty();
130130
}
131131

132+
/* The lifecycle of a Cleaner instance consists of four phases:
133+
* 1. New instances are contained in Cleaner.INSTANCES and added to a MasterCleaner.cleaners set.
134+
* 2. At some point, the master cleaner takes control of the instance by removing it
135+
* from Cleaner.INSTANCES and MasterCleaner.cleaners, and then adding it to its
136+
* referencedCleaners and watchedCleaners sets. Note that while it is no longer
137+
* in Cleaner.INSTANCES, a thread may still be holding a reference to it.
138+
* 3. Possibly some time later, the last reference to the cleaner instance is dropped and
139+
* it is GC'd. It is then also removed from referencedCleaners but remains in watchedCleaners.
140+
* 4. The master cleaner continues to monitor the watchedCleaners until they are empty and no
141+
* longer referenced. At that point they are also removed from watchedCleaners.
142+
*/
132143
final Set<Cleaner> cleaners = Collections.synchronizedSet(new HashSet<>());
133144
final Set<CleanerImpl> referencedCleaners = new HashSet<>();
134145
final Set<CleanerImpl> watchedCleaners = new HashSet<>();

0 commit comments

Comments
 (0)