changeset 1504:f443542d8650

threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 May 2020 11:13:16 -0600
parents 74c534de211f
children 01e38174d580
files src/goodjava/lucene/backup/BackupIndexWriter.java src/goodjava/lucene/logging/LoggingIndexWriter.java
diffstat 2 files changed, 16 insertions(+), 7 deletions(-) [+]
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) {
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Sun May 10 23:28:16 2020 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Mon May 11 11:13:16 2020 -0600
@@ -13,8 +13,6 @@
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Random;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ExecutorService;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.DirectoryReader;
 import org.apache.lucene.index.IndexReader;
@@ -45,7 +43,6 @@
 	private static final int OP_ADD_DOCUMENT = 3;
 	private static final int OP_UPDATE_DOCUMENT = 4;
 	private static final Random rnd = new Random();
-	protected static final ExecutorService threadPool = Executors.newCachedThreadPool();
 
 	public final LuceneIndexWriter indexWriter;
 	private final File logDir;
@@ -346,7 +343,7 @@
 		}
 		if( logs.size() > 3 ) {
 			isMerging = true;
-			threadPool.execute(mergeLogs);
+			new Thread(mergeLogs).start();
 //			mergeLogs.run();
 		}
 	}