This project implements a concurrent task execution system using Java 17+, Spring Boot, and Spring MVC.
It allows clients to:
- Submit long-running tasks asynchronously
- Execute tasks using a fixed-size worker thread pool
- Maintain FIFO task execution order
- Check task status at any time
- Stop queued or running tasks safely
The system is designed to demonstrate backend concurrency fundamentals, thread safety, and robust REST API design.
- Fixed-size ExecutorService
- FIFO task queue using
BlockingQueue - Thread-safe task state using
AtomicReference - Task cancellation using thread interruption
- Non-blocking REST request handling using
CompletableFuture - Safe concurrent access using
ConcurrentHashMap
Client (Postman / API Client)
|
v
TaskController (REST APIs)
|
v
TaskManagerService
├── ExecutorService (Fixed Thread Pool)
├── BlockingQueue<TaskWrapper> (FIFO Queue)
└── ConcurrentHashMap<String, TaskWrapper> (Task Store)
|
v
Worker Threads (execute tasks)
Each task can be in exactly one of the following states:
| State | Description |
|---|---|
| QUEUED | Accepted, waiting for a worker |
| RUNNING | Currently executing |
| DONE | Completed successfully |
| STOPPED | Cancelled before or during execution |
State transitions are thread-safe and strictly controlled.
-
A fixed-size thread pool limits concurrent execution
-
FIFO order is guaranteed using a
BlockingQueue -
Each task:
- Owns a
CompletableFuture<TaskStatus> - Owns a
Future<?>for interruption
- Owns a
-
REST request threads never execute tasks directly
-
Worker threads are reused efficiently
Endpoint
POST /queueTask
Request Body
{
"id": "task-123",
"task": "example-task",
"taskParams": {
"key": "value"
},
"time": 5
}Behavior
- Enqueues task in FIFO order
- Executes immediately if a worker is free
- Blocks response until task is DONE or STOPPED
- Rejects duplicate task IDs
Response
{
"id": "task-123",
"status": "DONE"
}Endpoint
POST /checkStatus
Request Body
{
"id": "task-123"
}Behavior
- Returns immediately
- Reflects current task state
Response
{
"id": "task-123",
"status": "RUNNING"
}Endpoint
POST /stopTask
Request Body
{
"id": "task-123"
}Behavior
- QUEUED → removed from queue
- RUNNING → interrupted
- DONE → no operation
Response
{
"id": "task-123",
"status": "STOPPED"
}- No more than N tasks run concurrently
- FIFO order preserved for queued tasks
- Thread-safe state transitions
- Safe task cancellation without race conditions
- No worker thread leakage
task.worker-pool-size=1
server.port=8080task.worker-pool-sizecontrols concurrency- Increasing value increases parallelism
src/main/java/com/Pranshu/TaskExecutionSystem
├── config
│ └── ExecutorConfig.java
├── controller
│ └── TaskController.java
├── service
│ └── TaskManagerService.java
├── model
│ ├── TaskRequest.java
│ ├── TaskResponse.java
│ ├── TaskStatus.java
│ └── TaskWrapper.java
└── TaskExecutionSystemApplication.java
- Task IDs are client-generated and unique
- Task duration is simulated using
Thread.sleep - In-memory storage is sufficient for assessment scope
- No persistence (data lost on restart)
- Blocking response for
/queueTask(per requirements) - No retry mechanism
- Persist tasks using a database
- Add rate limiting
- Add retry / timeout policies
- Use message queues (Kafka / RabbitMQ)
- Add metrics and monitoring
- Global exception handling
This project demonstrates:
- Strong understanding of concurrency
- Correct use of Java threading primitives
- Clean REST API design
- Robust backend engineering fundamentals