Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added chapter21/.DS_Store
Binary file not shown.
26 changes: 26 additions & 0 deletions chapter21/src/com/packt/java/references/Assignment1.java
Original file line number Diff line number Diff line change
@@ -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<Student, Result> 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());
}
}
42 changes: 42 additions & 0 deletions chapter21/src/com/packt/java/references/Assignment2.java
Original file line number Diff line number Diff line change
@@ -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<ShoppingCart> referenceQueue = new ReferenceQueue<>();

WeakReference<ShoppingCart> weakReference1 =
new WeakReference<>(cart1, referenceQueue);
WeakReference<ShoppingCart> weakReference2 =
new WeakReference<>(cart2, referenceQueue);
WeakReference<ShoppingCart> 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<ShoppingCart> referenceQueue) {
int count = 0;
while (referenceQueue.poll() != null) {
count++;
}

System.out.println("There were " + count + " items in the queue");
}
}
23 changes: 23 additions & 0 deletions chapter21/src/com/packt/java/references/Exercise1.java
Original file line number Diff line number Diff line change
@@ -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<MyObject> 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);
}
}
26 changes: 26 additions & 0 deletions chapter21/src/com/packt/java/references/Exercise2.java
Original file line number Diff line number Diff line change
@@ -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<Student, Integer> 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());
}
}
30 changes: 30 additions & 0 deletions chapter21/src/com/packt/java/references/Exercise3.java
Original file line number Diff line number Diff line change
@@ -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<ShoppingCart> referenceQueue = new ReferenceQueue<>();

WeakReference<ShoppingCart> 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 " : "")));
}
}
29 changes: 29 additions & 0 deletions chapter21/src/com/packt/java/references/Exercise4.java
Original file line number Diff line number Diff line change
@@ -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<ShoppingCart> softReference = new SoftReference<>(shoppingCart);

checkSoftReference(softReference);

shoppingCart = null;

System.gc();

Thread.sleep(5000);

checkSoftReference(softReference);

ShoppingCart rescuedCart = softReference.get();
}

private static void checkSoftReference(SoftReference<ShoppingCart> softReference) {
ShoppingCart shoppingCart = softReference.get();
System.out.println(String.format("ShoppingCart was %scleared.",
(shoppingCart == null ? "" : "not ")));
}
}
9 changes: 9 additions & 0 deletions chapter21/src/com/packt/java/references/MyObject.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
11 changes: 11 additions & 0 deletions chapter21/src/com/packt/java/references/Result.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions chapter21/src/com/packt/java/references/ShoppingCart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.packt.java.references;

public class ShoppingCart {
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("Finalized");
}
}
9 changes: 9 additions & 0 deletions chapter21/src/com/packt/java/references/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.packt.java.references;

public class Student {
private String name;

public Student(String name) {
this.name = name;
}
}