mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
SofterReference
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.util;
|
||||
|
||||
import com.intellij.openapi.util.LowMemoryWatcher;
|
||||
import com.intellij.util.containers.WeakList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
* A reference whose referent may be garbage-collected when there's low free memory, not only when there's none.
|
||||
* Use for objects which retain lots of memory and whose loss is not very expensive.
|
||||
*
|
||||
* @author peter
|
||||
*/
|
||||
public class SofterReference<T> {
|
||||
private volatile Reference<T> myRef;
|
||||
private static final WeakList<SofterReference> ourRegistry = new WeakList<SofterReference>();
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration") private static LowMemoryWatcher ourWatcher = LowMemoryWatcher.register(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("SofterReference.run: " + ourRegistry.size());
|
||||
for (SofterReference reference : ourRegistry.copyAndClear()) {
|
||||
reference.weaken();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public SofterReference(@NotNull T referent) {
|
||||
ourRegistry.add(this);
|
||||
myRef = new SoftReference<T>(referent);
|
||||
}
|
||||
|
||||
private void weaken() {
|
||||
T o = myRef.get();
|
||||
if (o != null) {
|
||||
myRef = new WeakReference<T>(o);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public T get() {
|
||||
Reference<T> ref = myRef;
|
||||
T referent = ref.get();
|
||||
if (referent != null && ref instanceof WeakReference) {
|
||||
ourRegistry.add(this);
|
||||
myRef = new SoftReference<T>(referent);
|
||||
}
|
||||
return referent;
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,15 @@ public class WeakList<T> extends UnsafeWeakList<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public List<T> copyAndClear() {
|
||||
synchronized (myArray) {
|
||||
List<T> result = new ArrayList<T>(myArray.size());
|
||||
myArray.toStrongCollection(result);
|
||||
clear();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private class MySyncIterator extends MyIterator {
|
||||
protected void findNext() {
|
||||
synchronized (myArray) {
|
||||
|
||||
Reference in New Issue
Block a user