AtomicBoolean is not needed.

This commit is contained in:
akwizgran
2011-09-27 16:29:41 +01:00
parent dea77b22d7
commit 90e32ac906

View File

@@ -1,14 +1,13 @@
package net.sf.briar.db; package net.sf.briar.db;
import java.util.concurrent.atomic.AtomicBoolean;
class DatabaseCleanerImpl implements DatabaseCleaner, Runnable { class DatabaseCleanerImpl implements DatabaseCleaner, Runnable {
private final AtomicBoolean stopped = new AtomicBoolean(false); private final Object lock = new Object();
private final Thread cleanerThread = new Thread(this); private final Thread cleanerThread = new Thread(this);
private volatile Callback callback; private volatile Callback callback;
private volatile long msBetweenSweeps; private volatile long msBetweenSweeps;
private volatile boolean stopped = false; // Locking: lock
public void startCleaning(Callback callback, long msBetweenSweeps) { public void startCleaning(Callback callback, long msBetweenSweeps) {
this.callback = callback; this.callback = callback;
@@ -17,10 +16,10 @@ class DatabaseCleanerImpl implements DatabaseCleaner, Runnable {
} }
public void stopCleaning() { public void stopCleaning() {
stopped.set(true);
// If the cleaner thread is waiting, wake it up // If the cleaner thread is waiting, wake it up
synchronized(stopped) { synchronized(lock) {
stopped.notifyAll(); stopped = true;
lock.notifyAll();
} }
try { try {
cleanerThread.join(); cleanerThread.join();
@@ -33,10 +32,10 @@ class DatabaseCleanerImpl implements DatabaseCleaner, Runnable {
if(callback.shouldCheckFreeSpace()) { if(callback.shouldCheckFreeSpace()) {
callback.checkFreeSpaceAndClean(); callback.checkFreeSpaceAndClean();
} else { } else {
synchronized(stopped) { synchronized(lock) {
if(stopped.get()) break; if(stopped) break;
try { try {
stopped.wait(msBetweenSweeps); lock.wait(msBetweenSweeps);
} catch(InterruptedException ignored) {} } catch(InterruptedException ignored) {}
} }
} }