92 lines
2.9 KiB
Java
Raw Normal View History

2016-12-30 22:22:39 -08:00
// collectiontopics/References.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
// Demonstrates Reference objects
import java.lang.ref.*;
import java.util.*;
class VeryBig {
private static final int SIZE = 10000;
private long[] la = new long[SIZE];
private String ident;
2017-05-01 14:33:10 -06:00
VeryBig(String id) { ident = id; }
2015-06-15 17:47:35 -07:00
@Override
public String toString() { return ident; }
@Override
protected void finalize() {
System.out.println("Finalizing " + ident);
}
}
public class References {
private static ReferenceQueue<VeryBig> rq =
new ReferenceQueue<>();
public static void checkQueue() {
Reference<? extends VeryBig> inq = rq.poll();
if(inq != null)
System.out.println("In queue: " + inq.get());
}
public static void main(String[] args) {
int size = 10;
// Or, choose size via the command line:
if(args.length > 0)
2017-05-01 17:43:21 -06:00
size = Integer.valueOf(args[0]);
2015-06-15 17:47:35 -07:00
LinkedList<SoftReference<VeryBig>> sa =
new LinkedList<>();
for(int i = 0; i < size; i++) {
sa.add(new SoftReference<>(
new VeryBig("Soft " + i), rq));
2017-01-11 09:11:59 -08:00
System.out.println(
"Just created: " + sa.getLast());
2015-06-15 17:47:35 -07:00
checkQueue();
}
LinkedList<WeakReference<VeryBig>> wa =
new LinkedList<>();
for(int i = 0; i < size; i++) {
wa.add(new WeakReference<>(
new VeryBig("Weak " + i), rq));
2017-01-11 09:11:59 -08:00
System.out.println(
"Just created: " + wa.getLast());
2015-06-15 17:47:35 -07:00
checkQueue();
}
SoftReference<VeryBig> s =
new SoftReference<>(new VeryBig("Soft"));
WeakReference<VeryBig> w =
new WeakReference<>(new VeryBig("Weak"));
System.gc();
LinkedList<PhantomReference<VeryBig>> pa =
new LinkedList<>();
for(int i = 0; i < size; i++) {
pa.add(new PhantomReference<>(
new VeryBig("Phantom " + i), rq));
2017-01-11 09:11:59 -08:00
System.out.println(
"Just created: " + pa.getLast());
2015-06-15 17:47:35 -07:00
checkQueue();
}
}
2015-09-07 11:44:36 -06:00
}
2016-07-20 06:32:39 -06:00
/* Output: (First and Last 10 Lines)
Just created: java.lang.ref.SoftReference@15db9742
Just created: java.lang.ref.SoftReference@6d06d69c
Just created: java.lang.ref.SoftReference@7852e922
Just created: java.lang.ref.SoftReference@4e25154f
Just created: java.lang.ref.SoftReference@70dea4e
Just created: java.lang.ref.SoftReference@5c647e05
Just created: java.lang.ref.SoftReference@33909752
Just created: java.lang.ref.SoftReference@55f96302
Just created: java.lang.ref.SoftReference@3d4eac69
Just created: java.lang.ref.SoftReference@42a57993
2016-07-22 14:45:35 -06:00
...________...________...________...________...
Just created: java.lang.ref.PhantomReference@45ee12a7
2015-06-15 17:47:35 -07:00
In queue: null
Just created: java.lang.ref.PhantomReference@330bedb4
2015-06-15 17:47:35 -07:00
In queue: null
Just created: java.lang.ref.PhantomReference@2503dbd3
2015-06-15 17:47:35 -07:00
In queue: null
Just created: java.lang.ref.PhantomReference@4b67cf4d
2016-07-22 14:45:35 -06:00
In queue: null
Just created: java.lang.ref.PhantomReference@7ea987ac
2016-07-22 14:45:35 -06:00
In queue: null
2015-09-07 11:44:36 -06:00
*/