diff src/goodjava/lucene/backup/BackupIndexWriter.java @ 1504:f443542d8650

threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 May 2020 11:13:16 -0600
parents 8a7b6b32c691
children d80395468b4e
line wrap: on
line diff
--- a/src/goodjava/lucene/backup/BackupIndexWriter.java	Sun May 10 23:28:16 2020 -0600
+++ b/src/goodjava/lucene/backup/BackupIndexWriter.java	Mon May 11 11:13:16 2020 -0600
@@ -9,6 +9,8 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Arrays;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
 import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.SSLSocket;
 import goodjava.io.IoUtils;
@@ -29,6 +31,7 @@
 	private final String name;
 	private final File dir;
 	private boolean isSyncPending = false;
+	private final ExecutorService exec = Executors.newSingleThreadExecutor();
 
 	public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,String name) throws IOException {
 		super(indexWriter,logDir);
@@ -40,21 +43,30 @@
 		IoUtils.mkdirs(dir);
 	}
 
+	public synchronized void close() throws IOException {
+		super.close();
+		exec.shutdown();
+	}
+
 	public synchronized void commit() throws IOException {
 		super.commit();
 		//sync();
 		if( !isSyncPending ) {
-			threadPool.execute(sync);
+			exec.execute(sync);
 			isSyncPending = true;
 		}
 	}
 
 	public void runSync() {
-		sync.run();
+		try {
+			exec.submit(sync).get();
+		} catch(Exception e) {
+			throw new RuntimeException(e);
+		}
 	}
 
 	private final Runnable sync = new Runnable() {
-		public synchronized void run() {
+		public void run() {
 			try {
 				sync();
 			} catch(IOException e) {