Skip to content

A custom thread pool implementation in Java, built from scratch to explore and understand the internal workings of thread pools.

License

Notifications You must be signed in to change notification settings

brendanddev/custom-thread-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom Thread Pool

A custom thread pool implementation in Java, built from scratch to explore and understand the internal workings of thread pools. This project focuses on implementing the core mechanisms manually, rather than relying on Java’s built-in concurrency utilities.


Features

  • Custom TaskQueue – A thread-safe FIFO queue implemented from scratch using synchronized, wait(), and notifyAll().
  • WorkerThread Management – Multiple worker threads continuously dequeue and execute tasks.
  • Graceful Shutdown – Supports a clean shutdown where queued tasks are completed using the poison pill pattern.
  • Immediate Shutdown – Can stop all active tasks and return unexecuted tasks immediately.
  • Rejection Policies – Handles tasks submitted after shutdown with configurable strategies:
    • ABORT_POLICY – Throws a RuntimeException.
    • DISCARD_POLICY – Silently discards the task.
    • CALLER_RUNS_POLICY – Runs the task in the calling thread.
    • DISCARD_OLDEST_POLICY – Removes the oldest queued task to make room for the new task.
  • Exception Handling – Worker threads catch exceptions from tasks to prevent thread death.
  • Custom Thread Factory – Allows naming threads and configuring priorities.

Project Structure

custom-thread-pool/
├─ pom.xml
├─ README.md
├─ src/
│ ├─ main/java/com/brendanddev/threadpool/
│ │ ├─ CustomThreadPool.java
│ │ ├─ TaskQueue.java
│ │ ├─ WorkerThread.java
│ │ ├─ CustomThreadFactory.java
│ │ ├─ Main.java
│ │ └─ policies/
│ │ ├─ RejectionHandler.java
│ │ └─ RejectionHandlers.java
├─ target/...

Usage

  1. Compile and run:
mvn clean compile exec:java

References & Inspiration

About

A custom thread pool implementation in Java, built from scratch to explore and understand the internal workings of thread pools.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages