diff --git a/briar-core/src/net/sf/briar/db/DatabaseModule.java b/briar-core/src/net/sf/briar/db/DatabaseModule.java index eed102d5e26bf21dd4231a9fb8090444959de950..1f4829d858b84958b6c55d1f7497552cd5d1f84c 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