Skip to content
Snippets Groups Projects
Commit 540b98ae authored by akwizgran's avatar akwizgran
Browse files

Limit the number of database threads.

parent 165b5c53
No related branches found
No related tags found
No related merge requests found
package net.sf.briar.db; package net.sf.briar.db;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.sql.Connection; import java.sql.Connection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.clock.Clock; import net.sf.briar.api.clock.Clock;
import net.sf.briar.api.clock.SystemClock; import net.sf.briar.api.clock.SystemClock;
...@@ -17,11 +21,24 @@ import com.google.inject.Singleton; ...@@ -17,11 +21,24 @@ import com.google.inject.Singleton;
public class DatabaseModule extends AbstractModule { public class DatabaseModule extends AbstractModule {
/**
* The maximum number of database threads. When a task is submitted to the
* database executor and no thread is available to run it, the task will be
* queued.
*/
private static final int MAX_DB_THREADS = 10;
/** How many milliseconds to keep idle threads alive. */
private static final int DB_KEEPALIVE = 60 * 1000;
@Override @Override
protected void configure() { protected void configure() {
bind(DatabaseCleaner.class).to(DatabaseCleanerImpl.class); bind(DatabaseCleaner.class).to(DatabaseCleanerImpl.class);
// Use an unbounded queue to prevent deadlock between submitted tasks
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
bind(Executor.class).annotatedWith(DatabaseExecutor.class).toInstance( bind(Executor.class).annotatedWith(DatabaseExecutor.class).toInstance(
Executors.newCachedThreadPool()); new ThreadPoolExecutor(MAX_DB_THREADS, MAX_DB_THREADS,
DB_KEEPALIVE, MILLISECONDS, queue));
} }
@Provides @Provides
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment