提交 bcf93904 authored 作者: noelgrandin's avatar noelgrandin

simplify background writer thread implementation, tying the sync object lifetime…

simplify background writer thread implementation, tying the sync object lifetime more closely to the thread object lifetime
上级 97e3d952
...@@ -153,8 +153,7 @@ public class MVStore { ...@@ -153,8 +153,7 @@ public class MVStore {
/** /**
* The background thread, if any. * The background thread, if any.
*/ */
volatile Thread backgroundThread; volatile BackgroundWriterThread backgroundWriterThread;
final Object backgroundThreadSync = new Object();
private volatile boolean reuseSpace = true; private volatile boolean reuseSpace = true;
...@@ -1944,13 +1943,13 @@ public class MVStore { ...@@ -1944,13 +1943,13 @@ public class MVStore {
} }
private void stopBackgroundThread() { private void stopBackgroundThread() {
Thread t = backgroundThread; BackgroundWriterThread t = backgroundWriterThread;
if (t == null) { if (t == null) {
return; return;
} }
backgroundThread = null; backgroundWriterThread = null;
synchronized (backgroundThreadSync) { synchronized (t.sync) {
backgroundThreadSync.notifyAll(); t.sync.notifyAll();
} }
try { try {
t.join(); t.join();
...@@ -1986,11 +1985,9 @@ public class MVStore { ...@@ -1986,11 +1985,9 @@ public class MVStore {
// start the background thread if needed // start the background thread if needed
if (millis > 0) { if (millis > 0) {
int sleep = Math.max(1, millis / 10); int sleep = Math.max(1, millis / 10);
Writer w = new Writer(this, sleep); BackgroundWriterThread t = new BackgroundWriterThread(this, sleep, fileStore.toString());
Thread t = new Thread(w, "MVStore writer " + fileStore.toString());
t.setDaemon(true);
t.start(); t.start();
backgroundThread = t; backgroundWriterThread = t;
} }
} }
...@@ -1999,26 +1996,28 @@ public class MVStore { ...@@ -1999,26 +1996,28 @@ public class MVStore {
} }
/** /**
* A background writer to automatically store changes from time to time. * A background writer thread to automatically store changes from time to time.
*/ */
private static class Writer implements Runnable { private static class BackgroundWriterThread extends Thread {
private final MVStore store; private final MVStore store;
private final int sleep; private final int sleep;
public final Object sync = new Object();
Writer(MVStore store, int sleep) { BackgroundWriterThread(MVStore store, int sleep, String fileStoreName) {
super("MVStore background writer " + fileStoreName);
this.store = store; this.store = store;
this.sleep = sleep; this.sleep = sleep;
setDaemon(true);
} }
@Override @Override
public void run() { public void run() {
while (true) { while (true) {
Thread t = store.backgroundThread; Thread t = store.backgroundWriterThread;
if (t == null) { if (t == null) {
break; break;
} }
Object sync = store.backgroundThreadSync;
synchronized (sync) { synchronized (sync) {
try { try {
sync.wait(sleep); sync.wait(sleep);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论