Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shutdown() function for CleanerThread #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
61 changes: 37 additions & 24 deletions batik-util/src/main/java/org/apache/batik/util/CleanerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.lang.ref.PhantomReference;

/**
Expand All @@ -34,14 +36,16 @@ Licensed to the Apache Software Foundation (ASF) under one or more
*/
public class CleanerThread extends Thread {

static volatile ReferenceQueue queue = null;
static CleanerThread thread = null;
static volatile ReferenceQueue<?> queue = null;
static CleanerThread thread = null;
/* latch to signal CleanerThread shutdown request */
private CountDownLatch shutdownLatch = new CountDownLatch(1);

public static ReferenceQueue getReferenceQueue() {
public static ReferenceQueue<?> getReferenceQueue() {

if ( queue == null ) {
synchronized (CleanerThread.class) {
queue = new ReferenceQueue();
synchronized (CleanerThread.class) {
if ( queue == null ) {
queue = new ReferenceQueue<>();
thread = new CleanerThread();
}
}
Expand Down Expand Up @@ -94,29 +98,38 @@ public PhantomReferenceCleared(Object o) {

protected CleanerThread() {
super("Batik CleanerThread");
setDaemon(true);
start();
}

@Override
public void run() {
while(true) {
try {
Reference ref;
try {
ref = queue.remove();
try {
while(!shutdownLatch.await(100, TimeUnit.SECONDS)) {
Reference<?> ref;
do {
ref = queue.poll();
// System.err.println("Cleaned: " + ref);
} catch (InterruptedException ie) {
continue;
}

if (ref instanceof ReferenceCleared) {
ReferenceCleared rc = (ReferenceCleared)ref;
rc.cleared();
}
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t) {
t.printStackTrace();
if (ref != null && ref instanceof ReferenceCleared) {
ReferenceCleared rc = (ReferenceCleared)ref;
rc.cleared();
}
} while (ref != null);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

/**
* Request CleanerThread shutdown and wait for it to finish.
*/
public static void shutdown() throws InterruptedException {
synchronized (CleanerThread.class) {
if (thread != null) {
thread.shutdownLatch.countDown();
thread.join();
queue = null;
thread = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class HaltingThread extends Thread {
/**
* Boolean indicating if this thread has ever been 'halted'.
*/
protected boolean beenHalted = false;
protected volatile boolean beenHalted = false;

public HaltingThread() { }

Expand Down