本文整理了Java中java.lang.ref.Reference.enqueue
方法的一些代码示例,展示了Reference.enqueue
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Reference.enqueue
方法的具体详情如下:
包路径:java.lang.ref.Reference
类名称:Reference
方法名:enqueue
[英]Forces the reference object to be enqueued if it has been associated with a queue.
[中]如果引用对象已与队列关联,则强制其排队。
代码示例来源: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: 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: 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: 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: 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: 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: google/guava
public void testDrainValueReferenceQueueOnRead() {
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();
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();
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.getValueReferenceQueueForTesting().poll());
}
}
}
代码示例来源:origin: com.github.marcosemiao.log4jdbc/log4jdbc-utils
public boolean enqueue() {
return reference.enqueue();
}
代码示例来源:origin: stackoverflow.com
new WeakReference<ObjectX>( referenceObj, refQ ).enqueue();
代码示例来源:origin: ops4j/org.ops4j.pax.logging
private void removeShutdownHook() {
final Thread shutdownThread = shutdownHookRef.get();
if (shutdownThread != null) {
Runtime.getRuntime().removeShutdownHook(shutdownThread);
shutdownHookRef.enqueue();
}
}
代码示例来源:origin: com.globalmentor/globalmentor-core
/**
* Removes all mappings from this map (optional operation).
*
* @throws UnsupportedOperationException clear is not supported by this map.
*/
public void clear() {
for(final Map.Entry<K, R> referenceEntry : map.entrySet()) { //for each reference entry in the map
referenceEntry.getValue().enqueue(); //enqueue this value
}
map.clear(); //clear the map
Reference<? extends V> reference; //remove all the references from the queue
do {
reference = referenceQueue.poll();
} while(reference != null);
}
代码示例来源:origin: stackoverflow.com
public class Main {
public static void main(String[] args) throws InterruptedException{
String weakString = "Help me";
ReferenceQueue<String> refQ = new ReferenceQueue<String>();
WeakReference<String> weakRef = new WeakReference<String>(weakString, refQ);
//there is no more "hard reference to the string"
weakString = null;
//there is no object in ReferenceQueue
System.out.println(refQ.poll());
//put the object in the ReferenceQueue
weakRef.enqueue();
while (true) {
//There will be returned "Help me" until the garbage collector will "kill"
//"weak" string. This will be done after some time because there are no
//more strong references to the string. After that will be returned a null
//object.
System.out.println(weakRef.get());
//only one time there will be returned a refenrece to the String object
System.out.println(refQ.poll());
Thread.sleep(1000);
}
}
}
代码示例来源:origin: org.arakhne.afc.core/references
/** Clean the references that was released.
*/
public final void expurge() {
Reference<? extends E> obj;
final Iterator<R> iter = this.theSet.iterator();
R reference;
while (iter.hasNext()) {
reference = iter.next();
if (reference != null
&& ((reference.isEnqueued()) || (reference.get() == null))) {
reference.enqueue();
reference.clear();
}
}
while ((obj = this.queue.poll()) != null) {
obj.clear();
if (this.referenceType.isInstance(obj)) {
this.theSet.remove(this.referenceType.cast(obj));
}
}
}
代码示例来源:origin: com.globalmentor/globalmentor-core
/**
* Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old
* value is replaced by the specified value. (A map <code>m</code> is said to contain a mapping for a key <code>k</code> if and only if {@link #containsKey(Object)
* m.containsKey(k)} would return <code>true</code>.))
*
* @param key key with which the specified value is to be associated.
* @param value value to be associated with the specified key.
* @return previous value associated with specified key, or <code>null</code> if there was no mapping for key. A <code>null</code> return can also indicate that the
* map previously associated <code>null</code> with the specified key, if the implementation supports <code>null</code> values.
*
* @throws UnsupportedOperationException if the <code>put</code> operation is not supported by this map.
* @throws ClassCastException if the class of the specified key or value prevents it from being stored in this map.
* @throws IllegalArgumentException if some aspect of this key or value prevents it from being stored in this map.
* @throws NullPointerException if this map does not permit <code>null</code> keys or values, and the specified key or value is <code>null</code>.
*/
public V put(final K key, final V value) {
final R reference = map.put(key, createReference(key, value, referenceQueue)); //create a reference and set the value
if(reference != null) { //if there was a reference
final V oldValue = reference != null ? reference.get() : null; //get the old value, if any
reference.enqueue(); //add the reference to the queue for purging
purgeExcept(reference); //purge all references except the one we just removed
return oldValue; //return the old value
} else { //if there was no reference
purge(); //purge everything
return null; //indicate that there was no previous value
}
}
代码示例来源:origin: com.google.guava/guava-tests
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: com.google.guava/guava-tests
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: com.google.guava/guava-tests
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: com.google.guava/guava-tests
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: com.google.guava/guava-tests
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());
}
}
}
内容来源于网络,如有侵权,请联系作者删除!