本文整理了Java中java.lang.ref.Reference
类的一些代码示例,展示了Reference
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Reference
类的具体详情如下:
包路径:java.lang.ref.Reference
类名称:Reference
[英]Provides an abstract class which describes behavior common to all reference objects. It is not possible to create immediate subclasses of Reference in addition to the ones provided by this package. It is also not desirable to do so, since references require very close cooperation with the system's garbage collector. The existing, specialized reference classes should be used instead.
Three different type of references exist, each being weaker than the preceding one: java.lang.ref.SoftReference, java.lang.ref.WeakReference, and java.lang.ref.PhantomReference. "Weakness" here means that less restrictions are being imposed on the garbage collector as to when it is allowed to actually garbage-collect the referenced object.
In order to use reference objects properly it is important to understand the different types of reachability that trigger their clearing and enqueueing. The following table lists these, from strongest to weakest. For each row, an object is said to have the reachability on the left side if (and only if) it fulfills all of the requirements on the right side. In all rows, consider the root set to be a set of references that are "resistant" to garbage collection (that is, running threads, method parameters, local variables, static fields and the like).
Strongly reachable
代码示例来源:origin: netty/netty
private V unfold(Reference<V> ref) {
if (ref == null) {
return null;
}
return ref.get();
}
代码示例来源:origin: commons-collections/commons-collections
boolean purge(Reference ref) {
boolean r = (keyType > HARD) && (key == ref);
r = r || ((valueType > HARD) && (value == ref));
if (r) {
if (keyType > HARD) ((Reference)key).clear();
if (valueType > HARD) {
((Reference)value).clear();
} else if (purgeValues) {
value = null;
}
}
return r;
}
}
代码示例来源:origin: google/guava
public void testDrainKeyReferenceQueueOnWrite() {
for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
if (map.usesKeyReferences()) {
Segment<Object, Object> segment = map.segments[0];
Object keyOne = new Object();
int hashOne = map.hash(keyOne);
Object valueOne = new Object();
Object keyTwo = new Object();
Object valueTwo = new Object();
map.put(keyOne, valueOne);
ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) entry;
reference.enqueue();
map.put(keyTwo, valueTwo);
assertFalse(map.containsKey(keyOne));
assertFalse(map.containsValue(valueOne));
assertNull(map.get(keyOne));
assertEquals(1, map.size());
assertNull(segment.keyReferenceQueue.poll());
}
}
}
代码示例来源:origin: google/guava
public void testDrainKeyReferenceQueueOnWrite() {
for (MapMaker maker : allWeakKeyStrengthMakers()) {
MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
if (maker.getKeyStrength() == Strength.WEAK) {
Segment<Object, Object, ?, ?> segment = map.segments[0];
Object keyOne = new Object();
int hashOne = map.hash(keyOne);
Object valueOne = new Object();
Object keyTwo = new Object();
Object valueTwo = new Object();
map.put(keyOne, valueOne);
InternalEntry<Object, Object, ?> entry = segment.getEntry(keyOne, hashOne);
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) entry;
reference.enqueue();
map.put(keyTwo, valueTwo);
assertFalse(map.containsKey(keyOne));
assertFalse(map.containsValue(valueOne));
assertNull(map.get(keyOne));
assertEquals(1, map.size());
assertNull(segment.getKeyReferenceQueueForTesting().poll());
}
}
}
代码示例来源:origin: redisson/redisson
private V unfold(Reference<V> ref) {
if (ref == null) {
return null;
}
return ref.get();
}
代码示例来源:origin: GitLqr/LQRWeChat
public void detachView() {
if (mViewRef != null) {
mViewRef.clear();
mViewRef = null;
}
}
代码示例来源:origin: google/guava
public void testDrainValueReferenceQueueOnWrite() {
for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
if (map.usesValueReferences()) {
Segment<Object, Object> segment = map.segments[0];
Object keyOne = new Object();
int hashOne = map.hash(keyOne);
Object valueOne = new Object();
Object keyTwo = new Object();
Object valueTwo = new Object();
map.put(keyOne, valueOne);
ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
ValueReference<Object, Object> valueReference = entry.getValueReference();
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) valueReference;
reference.enqueue();
map.put(keyTwo, valueTwo);
assertFalse(map.containsKey(keyOne));
assertFalse(map.containsValue(valueOne));
assertNull(map.get(keyOne));
assertEquals(1, map.size());
assertNull(segment.valueReferenceQueue.poll());
}
}
}
代码示例来源:origin: nostra13/Android-Universal-Image-Loader
@Override
public View getWrappedView() {
return viewRef.get();
}
代码示例来源:origin: GitLqr/LQRWeChat
public void detachView() {
if (mViewRef != null) {
mViewRef.clear();
mViewRef = null;
}
}
代码示例来源:origin: google/guava
public void testDrainKeyReferenceQueueOnRead() {
for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
if (map.usesKeyReferences()) {
Segment<Object, Object> segment = map.segments[0];
Object keyOne = new Object();
int hashOne = map.hash(keyOne);
Object valueOne = new Object();
Object keyTwo = new Object();
map.put(keyOne, valueOne);
ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) entry;
reference.enqueue();
for (int i = 0; i < SMALL_MAX_SIZE; i++) {
map.get(keyTwo);
}
assertFalse(map.containsKey(keyOne));
assertFalse(map.containsValue(valueOne));
assertNull(map.get(keyOne));
assertEquals(0, map.size());
assertNull(segment.keyReferenceQueue.poll());
}
}
}
代码示例来源:origin: nostra13/Android-Universal-Image-Loader
@Override
public boolean isCollected() {
return viewRef.get() == null;
}
代码示例来源:origin: wildfly/wildfly
boolean purge(Reference ref) {
boolean r = (keyType > HARD) && (key == ref);
r = r || ((valueType > HARD) && (value == ref));
if (r) {
if (keyType > HARD) ((Reference)key).clear();
if (valueType > HARD) {
((Reference)value).clear();
} else if (purgeValues) {
value = null;
}
}
return r;
}
}
代码示例来源:origin: google/guava
public void testDrainValueReferenceQueueOnWrite() {
for (MapMaker maker : allWeakValueStrengthMakers()) {
MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
if (maker.getValueStrength() == Strength.WEAK) {
Segment<Object, Object, ?, ?> segment = map.segments[0];
Object keyOne = new Object();
int hashOne = map.hash(keyOne);
Object valueOne = new Object();
Object keyTwo = new Object();
Object valueTwo = new Object();
map.put(keyOne, valueOne);
@SuppressWarnings("unchecked")
WeakValueEntry<Object, Object, ?> entry =
(WeakValueEntry<Object, Object, ?>) segment.getEntry(keyOne, hashOne);
WeakValueReference<Object, Object, ?> valueReference = entry.getValueReference();
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) valueReference;
reference.enqueue();
map.put(keyTwo, valueTwo);
assertFalse(map.containsKey(keyOne));
assertFalse(map.containsValue(valueOne));
assertNull(map.get(keyOne));
assertEquals(1, map.size());
assertNull(segment.getValueReferenceQueueForTesting().poll());
}
}
}
代码示例来源:origin: commons-collections/commons-collections
/**
* Gets the key from the entry.
* This method dereferences weak and soft keys and thus may return null.
*
* @return the key, which may be null if it was garbage collected
*/
public Object getKey() {
return (parent.keyType > HARD) ? ((Reference) key).get() : key;
}
代码示例来源:origin: google/guava
/**
* Repeatedly dequeues references from the queue and invokes {@link
* FinalizableReference#finalizeReferent()} on them until the queue is empty. This method is a
* no-op if the background thread was created successfully.
*/
void cleanUp() {
if (threadStarted) {
return;
}
Reference<?> reference;
while ((reference = queue.poll()) != null) {
/*
* This is for the benefit of phantom references. Weak and soft references will have already
* been cleared by this point.
*/
reference.clear();
try {
((FinalizableReference) reference).finalizeReferent();
} catch (Throwable t) {
logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
}
}
}
代码示例来源:origin: google/guava
public void testDrainKeyReferenceQueueOnRead() {
for (MapMaker maker : allWeakKeyStrengthMakers()) {
MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1));
if (maker.getKeyStrength() == Strength.WEAK) {
Segment<Object, Object, ?, ?> segment = map.segments[0];
Object keyOne = new Object();
int hashOne = map.hash(keyOne);
Object valueOne = new Object();
Object keyTwo = new Object();
map.put(keyOne, valueOne);
InternalEntry<Object, Object, ?> entry = segment.getEntry(keyOne, hashOne);
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) entry;
reference.enqueue();
for (int i = 0; i < SMALL_MAX_SIZE; i++) {
Object unused = map.get(keyTwo);
}
assertFalse(map.containsKey(keyOne));
assertFalse(map.containsValue(valueOne));
assertNull(map.get(keyOne));
assertEquals(0, map.size());
assertNull(segment.getKeyReferenceQueueForTesting().poll());
}
}
}
代码示例来源:origin: commons-collections/commons-collections
/**
* Gets the value from the entry.
* This method dereferences weak and soft value and thus may return null.
*
* @return the value, which may be null if it was garbage collected
*/
public Object getValue() {
return (parent.valueType > HARD) ? ((Reference) value).get() : value;
}
代码示例来源:origin: commons-collections/commons-collections
public Object setValue(Object object) {
Object old = getValue();
if (valueType > HARD) ((Reference)value).clear();
value = toReference(valueType, object, hash);
return old;
}
代码示例来源:origin: google/guava
public void testDrainValueReferenceQueueOnRead() {
for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
if (map.usesValueReferences()) {
Segment<Object, Object> segment = map.segments[0];
Object keyOne = new Object();
int hashOne = map.hash(keyOne);
Object valueOne = new Object();
Object keyTwo = new Object();
map.put(keyOne, valueOne);
ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
ValueReference<Object, Object> valueReference = entry.getValueReference();
@SuppressWarnings("unchecked")
Reference<Object> reference = (Reference) valueReference;
reference.enqueue();
for (int i = 0; i < SMALL_MAX_SIZE; i++) {
map.get(keyTwo);
}
assertFalse(map.containsKey(keyOne));
assertFalse(map.containsValue(valueOne));
assertNull(map.get(keyOne));
assertEquals(0, map.size());
assertNull(segment.valueReferenceQueue.poll());
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@SuppressWarnings("unchecked")
final V dereferenceValue(Object value) {
if ( value instanceof KeyReference ) {
return ( (Reference<V>) value ).get();
}
return (V) value;
}
内容来源于网络,如有侵权,请联系作者删除!