-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hu Shenghao <[email protected]>
- Loading branch information
1 parent
587ae07
commit 375ead7
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
basic/src/main/java/com/dede/android_eggs/util/GcWatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.dede.android_eggs.util | ||
|
||
import java.lang.ref.WeakReference | ||
|
||
/** | ||
* GC Watcher | ||
* | ||
* @see [com.android.internal.os.BinderInternal] | ||
*/ | ||
class GcWatcher private constructor() { | ||
|
||
companion object { | ||
|
||
private val sInstance = GcWatcher() | ||
|
||
private val sGcWatchers: ArrayList<Runnable> = ArrayList() | ||
private var sTempWatchers: Array<Runnable?> = emptyArray() | ||
|
||
private var sGcWatcherRef: WeakReference<GcWatcherObj> = WeakReference(GcWatcherObj()) | ||
|
||
fun get(): GcWatcher { | ||
return sInstance | ||
} | ||
} | ||
|
||
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") | ||
private class GcWatcherObj : Object() { | ||
override fun finalize() { | ||
synchronized(sGcWatchers) { | ||
sTempWatchers = sGcWatchers.toArray(sTempWatchers) | ||
} | ||
for (gcWatcher in sTempWatchers) { | ||
gcWatcher?.run() | ||
} | ||
sGcWatcherRef = WeakReference(GcWatcherObj()) | ||
} | ||
} | ||
|
||
fun addWatcher(watcher: Runnable) { | ||
synchronized(sGcWatchers) { | ||
sGcWatchers.add(watcher) | ||
} | ||
} | ||
|
||
fun removeWatcher(watcher: Runnable) { | ||
synchronized(sGcWatchers) { | ||
sGcWatchers.remove(watcher) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters