Package com.mckoi.database
Class WorkerPool
- java.lang.Object
-
- com.mckoi.database.WorkerPool
-
final class WorkerPool extends java.lang.Object
Maintains a pool of worker threads that are used to dispatch commands to a Database sub-system.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
WorkerPool.RunCommand
Structures within the run_queue list.
-
Field Summary
Fields Modifier and Type Field Description private java.util.LinkedList
available_worker_threads
This is a queue of 'WorkerThread' objects that are currently available to process commands from the service providers.private boolean
is_executing_commands
If this is set to false, then no commands will be executed by the 'execute' method.private int
MAXIMUM_WORKER_THREADS
This is the maximum number of worker threads that will be created.private java.util.LinkedList
run_queue
A list of pending Runnable objects that are due to be executed.private TransactionSystem
system
The TransactionSystem that this pool is part of.private int
worker_thread_count
The number of worker threads that have been created in total.
-
Constructor Summary
Constructors Constructor Description WorkerPool(TransactionSystem system, int max_worker_threads)
Constructs the worker thread pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DebugLogger
Debug()
Returns a DebugLogger object that we can use to log debug messages.(package private) void
execute(User user, DatabaseConnection database, java.lang.Runnable runner)
Executes database functions from the 'run' method of the given runnable instance on a worker thread.private WorkerThread
getFirstWaitingThread()
This returns the first available WorkerThread object from the thread pool.(package private) void
notifyWorkerReady(WorkerThread worker_thread)
This is called by a WorkerThread when it is decided that it is ready to service a new command.(package private) void
setIsExecutingCommands(boolean status)
Controls whether the database is allowed to execute commands or not.(package private) void
shutdown()
Shuts down the WorkerPool object stopping all worker threads.(package private) void
waitUntilAllWorkersQuiet()
Waits until all executing commands have stopped.
-
-
-
Field Detail
-
system
private TransactionSystem system
The TransactionSystem that this pool is part of.
-
MAXIMUM_WORKER_THREADS
private int MAXIMUM_WORKER_THREADS
This is the maximum number of worker threads that will be created.
-
available_worker_threads
private java.util.LinkedList available_worker_threads
This is a queue of 'WorkerThread' objects that are currently available to process commands from the service providers.
-
worker_thread_count
private int worker_thread_count
The number of worker threads that have been created in total.
-
run_queue
private java.util.LinkedList run_queue
A list of pending Runnable objects that are due to be executed. This is a queue of events to be run.
-
is_executing_commands
private boolean is_executing_commands
If this is set to false, then no commands will be executed by the 'execute' method.
-
-
Constructor Detail
-
WorkerPool
WorkerPool(TransactionSystem system, int max_worker_threads)
Constructs the worker thread pool.
-
-
Method Detail
-
Debug
public final DebugLogger Debug()
Returns a DebugLogger object that we can use to log debug messages.
-
notifyWorkerReady
void notifyWorkerReady(WorkerThread worker_thread)
This is called by a WorkerThread when it is decided that it is ready to service a new command.
-
getFirstWaitingThread
private WorkerThread getFirstWaitingThread()
This returns the first available WorkerThread object from the thread pool. If there are no available worker threads available then it returns null. This method must execute fast and must not block.
-
execute
void execute(User user, DatabaseConnection database, java.lang.Runnable runner)
Executes database functions from the 'run' method of the given runnable instance on a worker thread. All database functions should go through a worker thread. If we ensure this, we can easily stop all database functions from executing. Also, we only need to have a certain number of threads active at any one time rather than a unique thread for each connection.
-
setIsExecutingCommands
void setIsExecutingCommands(boolean status)
Controls whether the database is allowed to execute commands or not. If this is set to true, then calls to 'execute' will make commands execute.
-
waitUntilAllWorkersQuiet
void waitUntilAllWorkersQuiet()
Waits until all executing commands have stopped. This is best called right after a call to 'setIsExecutingCommands(false)'. If these two commands are run, the database is in a known state where no commands can be executed.NOTE: This can't be called from the WorkerThread. Deadlock will result if we were allowed to do this.
-
shutdown
void shutdown()
Shuts down the WorkerPool object stopping all worker threads.
-
-