CloseGuard 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。
[Android.Runtime.Register("android/util/CloseGuard", ApiSince=30, DoNotGenerateAcw=true)]
public sealed class CloseGuard : Java.Lang.Object
[<Android.Runtime.Register("android/util/CloseGuard", ApiSince=30, DoNotGenerateAcw=true)>]
type CloseGuard = class
inherit Object
- 繼承
- 屬性
備註
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。
簡單的範例:
{@code
class Foo {
private final CloseGuard guard = new CloseGuard();
...
public Foo() {
...;
guard.open("cleanup");
}
public void cleanup() {
guard.close();
...;
if (Build.VERSION.SDK_INT >= 28) {
Reference.reachabilityFence(this);
}
// For full correctness in the absence of a close() call, other methods may also need
// reachabilityFence() calls.
}
protected void finalize() throws Throwable {
try {
// Note that guard could be null if the constructor threw.
if (guard != null) {
guard.warnIfOpen();
}
cleanup();
} finally {
super.finalize();
}
}
}
}
在物件建構後明確分配資源時,CloseGuard 保護可以延後。 例如:
{@code
class Bar {
private final CloseGuard guard = new CloseGuard();
...
public Bar() {
...;
}
public void connect() {
...;
guard.open("cleanup");
}
public void cleanup() {
guard.close();
...;
if (Build.VERSION.SDK_INT >= 28) {
Reference.reachabilityFence(this);
}
// For full correctness in the absence of a close() call, other methods may also need
// reachabilityFence() calls.
}
protected void finalize() throws Throwable {
try {
// Note that guard could be null if the constructor threw.
if (guard != null) {
guard.warnIfOpen();
}
cleanup();
} finally {
super.finalize();
}
}
}
}
在建構子中使用時,呼叫 open 應發生在建構子的結尾,因為導致建構子突然終止的例外將意味著使用者無法明確參考該物件來清理。 在方法中使用時,呼叫 open 應在資源取得後立即發生。
Java 文件 android.util.CloseGuard。
本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。
建構函式
| 名稱 | Description |
|---|---|
| CloseGuard() |
建立一個新的 CloseGuard 實例。 |
屬性
| 名稱 | Description |
|---|---|
| Class |
回傳此 |
| Handle |
底層 Android 實例的帳號。 (繼承來源 Object) |
| JniIdentityHashCode |
取得由互通執行時指派給此 Java 對等端的身份雜湊碼。 (繼承來源 Object) |
| JniManagedPeerState |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| JniPeerMembers |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 |
| PeerReference |
取得這個 Java 節點的 JNI 物件參考。 (繼承來源 Object) |
| ThresholdClass |
此 API 支援 Mono for Android 基礎架構,並非直接從你的程式碼中使用。 (繼承來源 Object) |
| ThresholdType |
此 API 支援 Mono for Android 基礎架構,並非直接從你的程式碼中使用。 (繼承來源 Object) |
方法
| 名稱 | Description |
|---|---|
| Clone() |
建立並回傳此物件的副本。 (繼承來源 Object) |
| Close() |
將此 CloseGuard 實例標記為已關閉,以避免在完成時收到警告。 |
| Construct(JniObjectReference, JniObjectReferenceOptions) |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| Dispose() |
釋放該 Java 節點所持有的資源。 (繼承來源 Object) |
| Dispose(Boolean) |
釋放該 Java 節點所持有的資源。 (繼承來源 Object) |
| DisposeUnlessReferenced() |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| Equals(Object) |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| Equals(Object) |
表示是否有其他物體「等同」於此物。 (繼承來源 Object) |
| GetHashCode() |
回傳物件的雜湊碼值。 (繼承來源 Object) |
| JavaFinalize() |
已淘汰.
當垃圾回收判定該物件不再有相關參考時,由垃圾回收器呼叫。 (繼承來源 Object) |
| Notify() |
喚醒一個正在該物件監視器上等待的執行緒。 (繼承來源 Object) |
| NotifyAll() |
喚醒所有等待該物件監視器的執行緒。 (繼承來源 Object) |
| Open(String) |
初始化實例時會警告呼叫者應該明確呼叫該 |
| SetHandle(IntPtr, JniHandleOwnership) |
設定 Handle 屬性。 (繼承來源 Object) |
| SetPeerReference(JniObjectReference, JniObjectReferenceOptions) |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| ToArray<T>() |
從這個 Java 陣列包裝器建立一個受管理陣列。 (繼承來源 Object) |
| ToString() |
回傳物件的字串表示。 (繼承來源 Object) |
| UnregisterFromRuntime() |
將此 Java 節點從互通執行時中取消註冊。 (繼承來源 Object) |
| Wait() |
導致目前執行緒等待被喚醒,通常是透過 <em>通知</><em 或 em>中斷</em> 來喚醒。 (繼承來源 Object) |
| Wait(Int64, Int32) |
會讓目前執行緒等待喚醒,通常是透過 <em>通知</><em 或 em>中斷</em>,或是經過一定的真實時間。 (繼承來源 Object) |
| Wait(Int64) |
會讓目前執行緒等待喚醒,通常是透過 <em>通知</><em 或 em>中斷</em>,或是經過一定的真實時間。 (繼承來源 Object) |
| WarnIfOpen() |
若呼叫者未在最終完成前呼叫明確關閉方法而妥善清理,則會記錄警告。 |
明確介面實作
| 名稱 | Description |
|---|---|
| IJavaPeerable.Disposed() |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| IJavaPeerable.Finalized() |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| IJavaPeerable.JniObjectReferenceControlBlock |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| IJavaPeerable.SetJniIdentityHashCode(Int32) |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
| IJavaPeerable.SetPeerReference(JniObjectReference) |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 (繼承來源 JavaObject) |
擴充方法
| 名稱 | Description |
|---|---|
| GetJniTypeName(IJavaPeerable) |
取得實例 |
| JavaAs<TResult>(IJavaPeerable) |
試著強制 |
| JavaCast<TResult>(IJavaObject) |
執行 Android 執行時檢查型別轉換。 |
| JavaCast<TResult>(IJavaObject) |
CloseGuard 是一種用來標記資源隱式終結子清理的機制,這些資源本應由明確的 Close 方法(在 Effective Java 中的「明確終止方法」)來清理。 |
| TryJavaCast<TResult>(IJavaPeerable, TResult) |
試著強制 |