From 3fd23830c9cd8190e83f90f7e46626a44061ba10 Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Fri, 15 Mar 2013 16:34:00 +0000
Subject: [PATCH] Use an unbounded executor for DB tasks, which may depend on
 each other.

---
 .../src/net/sf/briar/db/DatabaseModule.java   | 24 +++++++++----------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/briar-core/src/net/sf/briar/db/DatabaseModule.java b/briar-core/src/net/sf/briar/db/DatabaseModule.java
index eed102d5e2..1f4829d858 100644
--- a/briar-core/src/net/sf/briar/db/DatabaseModule.java
+++ b/briar-core/src/net/sf/briar/db/DatabaseModule.java
@@ -1,7 +1,12 @@
 package net.sf.briar.db;
 
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+
 import java.sql.Connection;
+import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.Executor;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
 
 import net.sf.briar.api.clock.Clock;
 import net.sf.briar.api.clock.SystemClock;
@@ -9,7 +14,6 @@ import net.sf.briar.api.db.DatabaseComponent;
 import net.sf.briar.api.db.DatabaseConfig;
 import net.sf.briar.api.db.DatabaseExecutor;
 import net.sf.briar.api.lifecycle.ShutdownManager;
-import net.sf.briar.util.BoundedExecutor;
 
 import com.google.inject.AbstractModule;
 import com.google.inject.Provides;
@@ -17,27 +21,23 @@ import com.google.inject.Singleton;
 
 public class DatabaseModule extends AbstractModule {
 
-	// FIXME: Determine suitable values for these constants empirically
-
-	/**
-	 * The maximum number of database tasks that can be queued for execution
-	 * before submitting another task will block.
-	 */
-	private static final int MAX_QUEUED_DB_TASKS = 1000;
-
 	/** The minimum number of database threads to keep in the pool. */
 	private static final int MIN_DB_THREADS = 1;
 
 	/** The maximum number of database threads. */
 	private static final int MAX_DB_THREADS = 10;
 
+	/** The time in milliseconds to keep unused database threads alive. */
+	private static final int DB_KEEPALIVE = 60 * 1000;
+
 	@Override
 	protected void configure() {
 		bind(DatabaseCleaner.class).to(DatabaseCleanerImpl.class);
-		// The executor is bounded, so tasks must be independent and short-lived
+		// Database tasks may depend on each other, so use an unbounded queue
+		BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
 		bind(Executor.class).annotatedWith(DatabaseExecutor.class).toInstance(
-				new BoundedExecutor(MAX_QUEUED_DB_TASKS, MIN_DB_THREADS,
-						MAX_DB_THREADS));
+				new ThreadPoolExecutor(MIN_DB_THREADS, MAX_DB_THREADS,
+						DB_KEEPALIVE, MILLISECONDS, queue));
 	}
 
 	@Provides
-- 
GitLab