diff --git a/chapter21/.DS_Store b/chapter21/.DS_Store new file mode 100644 index 0000000..b94c0ea Binary files /dev/null and b/chapter21/.DS_Store differ diff --git a/chapter21/src/com/packt/java/references/Assignment1.java b/chapter21/src/com/packt/java/references/Assignment1.java new file mode 100644 index 0000000..0a6e92d --- /dev/null +++ b/chapter21/src/com/packt/java/references/Assignment1.java @@ -0,0 +1,26 @@ +package com.packt.java.references; + +import java.util.WeakHashMap; + +public class Assignment1 { + public static void main(String[] args) throws Exception { + Student harry = new Student("Harry"); + Student jenny = new Student("Jenny"); + + WeakHashMap testResults = + new WeakHashMap<>(); + + testResults.put(harry, new Result(harry, 23)); + testResults.put(jenny, new Result(jenny, 25)); + + System.out.println("Test results: " + testResults.size()); + + // HARRY is dereferenced + harry = null; + System.gc(); + + Thread.sleep(5000); + + System.out.println("Test results: " + testResults.size()); + } +} diff --git a/chapter21/src/com/packt/java/references/Assignment2.java b/chapter21/src/com/packt/java/references/Assignment2.java new file mode 100644 index 0000000..dae3026 --- /dev/null +++ b/chapter21/src/com/packt/java/references/Assignment2.java @@ -0,0 +1,42 @@ +package com.packt.java.references; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; + +public class Assignment2 { + public static void main(String[] args) throws Exception { + ShoppingCart cart1 = new ShoppingCart(); + ShoppingCart cart2 = new ShoppingCart(); + ShoppingCart cart3 = new ShoppingCart(); + + ReferenceQueue referenceQueue = new ReferenceQueue<>(); + + WeakReference weakReference1 = + new WeakReference<>(cart1, referenceQueue); + WeakReference weakReference2 = + new WeakReference<>(cart2, referenceQueue); + WeakReference weakReference3 = + new WeakReference<>(cart3, referenceQueue); + + countReferenceQueue(referenceQueue); + + cart1 = null; + cart2 = null; + cart3 = null; + + System.gc(); + + Thread.sleep(100); + + countReferenceQueue(referenceQueue); + } + + private static void countReferenceQueue(ReferenceQueue referenceQueue) { + int count = 0; + while (referenceQueue.poll() != null) { + count++; + } + + System.out.println("There were " + count + " items in the queue"); + } +} diff --git a/chapter21/src/com/packt/java/references/Exercise1.java b/chapter21/src/com/packt/java/references/Exercise1.java new file mode 100644 index 0000000..7a89cb2 --- /dev/null +++ b/chapter21/src/com/packt/java/references/Exercise1.java @@ -0,0 +1,23 @@ +package com.packt.java.references; + +import java.lang.ref.WeakReference; + +public class Exercise1 { + + public static void main(String[] args) { + MyObject myObject = new MyObject(); + System.out.println(myObject); + + WeakReference weakReference = new WeakReference<>(myObject); + MyObject myObject2 = weakReference.get(); + System.out.println(myObject2); + + myObject = null; + myObject2 = null; + + System.gc(); + + MyObject myObject3 = weakReference.get(); + System.out.println(myObject3); + } +} diff --git a/chapter21/src/com/packt/java/references/Exercise2.java b/chapter21/src/com/packt/java/references/Exercise2.java new file mode 100644 index 0000000..226f5a6 --- /dev/null +++ b/chapter21/src/com/packt/java/references/Exercise2.java @@ -0,0 +1,26 @@ +package com.packt.java.references; + +import java.util.WeakHashMap; + +public class Exercise2 { + public static void main(String[] args) throws Exception { + Student harry = new Student("Harry"); + Student jenny = new Student("Jenny"); + + WeakHashMap testResults = + new WeakHashMap<>(); + + testResults.put(harry, 23); + testResults.put(jenny, 25); + + System.out.println("Test results: " + testResults.size()); + + // HARRY is dereferenced + harry = null; + System.gc(); + + Thread.sleep(500); + + System.out.println("Test results: " + testResults.size()); + } +} diff --git a/chapter21/src/com/packt/java/references/Exercise3.java b/chapter21/src/com/packt/java/references/Exercise3.java new file mode 100644 index 0000000..87b877b --- /dev/null +++ b/chapter21/src/com/packt/java/references/Exercise3.java @@ -0,0 +1,30 @@ +package com.packt.java.references; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; + +public class Exercise3 { + public static void main(String[] args) throws Exception { + ShoppingCart shoppingCart = new ShoppingCart(); + + ReferenceQueue referenceQueue = new ReferenceQueue<>(); + + WeakReference weakReference = + new WeakReference<>(shoppingCart, referenceQueue); + + System.out.println(String.format("ShoppingCart has %sbeen cleared.", + (referenceQueue.poll() == null ? "not " : ""))); + + shoppingCart = null; + + System.gc(); + + Thread.sleep(500); + + System.out.println(String.format("ShoppingCart has %sbeen cleared.", + (referenceQueue.poll() == null ? "not " : ""))); + + System.out.println(String.format("ShoppingCart reference %sexist", + (weakReference.get() == null ? "does not " : ""))); + } +} diff --git a/chapter21/src/com/packt/java/references/Exercise4.java b/chapter21/src/com/packt/java/references/Exercise4.java new file mode 100644 index 0000000..e55a156 --- /dev/null +++ b/chapter21/src/com/packt/java/references/Exercise4.java @@ -0,0 +1,29 @@ +package com.packt.java.references; + +import java.lang.ref.SoftReference; + +public class Exercise4 { + public static void main(String[] args) throws Exception { + ShoppingCart shoppingCart = new ShoppingCart(); + + SoftReference softReference = new SoftReference<>(shoppingCart); + + checkSoftReference(softReference); + + shoppingCart = null; + + System.gc(); + + Thread.sleep(5000); + + checkSoftReference(softReference); + + ShoppingCart rescuedCart = softReference.get(); + } + + private static void checkSoftReference(SoftReference softReference) { + ShoppingCart shoppingCart = softReference.get(); + System.out.println(String.format("ShoppingCart was %scleared.", + (shoppingCart == null ? "" : "not "))); + } +} diff --git a/chapter21/src/com/packt/java/references/MyObject.java b/chapter21/src/com/packt/java/references/MyObject.java new file mode 100644 index 0000000..5cff369 --- /dev/null +++ b/chapter21/src/com/packt/java/references/MyObject.java @@ -0,0 +1,9 @@ +package com.packt.java.references; + +public class MyObject { + @Override + protected void finalize() throws Throwable { + super.finalize(); + System.out.println("Finalize called."); + } +} diff --git a/chapter21/src/com/packt/java/references/Result.java b/chapter21/src/com/packt/java/references/Result.java new file mode 100644 index 0000000..d884093 --- /dev/null +++ b/chapter21/src/com/packt/java/references/Result.java @@ -0,0 +1,11 @@ +package com.packt.java.references; + +public class Result { + private Student student; + private Integer score; + + public Result(Student student, Integer score) { + this.student = student; + this.score = score; + } +} diff --git a/chapter21/src/com/packt/java/references/ShoppingCart.java b/chapter21/src/com/packt/java/references/ShoppingCart.java new file mode 100644 index 0000000..cec6206 --- /dev/null +++ b/chapter21/src/com/packt/java/references/ShoppingCart.java @@ -0,0 +1,9 @@ +package com.packt.java.references; + +public class ShoppingCart { + @Override + protected void finalize() throws Throwable { + super.finalize(); + System.out.println("Finalized"); + } +} diff --git a/chapter21/src/com/packt/java/references/Student.java b/chapter21/src/com/packt/java/references/Student.java new file mode 100644 index 0000000..32aee84 --- /dev/null +++ b/chapter21/src/com/packt/java/references/Student.java @@ -0,0 +1,9 @@ +package com.packt.java.references; + +public class Student { + private String name; + + public Student(String name) { + this.name = name; + } +}