mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Improved encapsulation of thread synchronisation as follows
- replaced use of Object instance mutex with a private final Lock object - replaced Object signaling with specific condition signalling
This commit is contained in:
@@ -2,6 +2,8 @@ package org.briarproject.lifecycle;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.briarproject.api.lifecycle.ShutdownManager;
|
||||
|
||||
@@ -10,26 +12,42 @@ class ShutdownManagerImpl implements ShutdownManager {
|
||||
protected final Map<Integer, Thread> hooks; // Locking: this
|
||||
|
||||
private int nextHandle = 0; // Locking: this
|
||||
|
||||
private final Lock synchLock = new ReentrantLock();
|
||||
|
||||
ShutdownManagerImpl() {
|
||||
hooks = new HashMap<Integer, Thread>();
|
||||
}
|
||||
|
||||
public synchronized int addShutdownHook(Runnable r) {
|
||||
int handle = nextHandle++;
|
||||
Thread hook = createThread(r);
|
||||
hooks.put(handle, hook);
|
||||
Runtime.getRuntime().addShutdownHook(hook);
|
||||
return handle;
|
||||
public int addShutdownHook(Runnable r) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
int handle = nextHandle++;
|
||||
Thread hook = createThread(r);
|
||||
hooks.put(handle, hook);
|
||||
Runtime.getRuntime().addShutdownHook(hook);
|
||||
return handle;
|
||||
}
|
||||
finally{
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Thread createThread(Runnable r) {
|
||||
return new Thread(r, "ShutdownManager");
|
||||
}
|
||||
|
||||
public synchronized boolean removeShutdownHook(int handle) {
|
||||
Thread hook = hooks.remove(handle);
|
||||
if(hook == null) return false;
|
||||
else return Runtime.getRuntime().removeShutdownHook(hook);
|
||||
public boolean removeShutdownHook(int handle) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
Thread hook = hooks.remove(handle);
|
||||
if(hook == null) return false;
|
||||
else return Runtime.getRuntime().removeShutdownHook(hook);
|
||||
}
|
||||
finally{
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user