Architecture
Producer -> RateLimiter -> BoundedTaskQueue -> Worker threads -> MetricsCollector
Design Decisions
- ReentrantLock + Condition used for precise control and explicit signaling; avoids implicit locking of
synchronizedand allows multiple conditions (notEmpty / notFull). - PriorityQueue + explicit lock chosen over
PriorityBlockingQueueto demonstrate explicit concurrency control and backpressure behavior when bounded. - RetryPolicy: FIXED vs LINEAR vs EXPONENTIAL tradeoffs — FIXED is simple and predictable; LINEAR spaces retries linearly to reduce retry storms; EXPONENTIAL rapidly increases backoff to avoid thundering retries at scale (capped to 30s here).
- Shutdown:
shutdown()marks queue as closed and signals all waiting conditions;dequeue()returns null when shutdown and empty so workers can exit cleanly;WorkerPool.shutdown()requests workers stop and joins threads.
Usage
See src/main/java/dev/meettak/flowq/demo/Demo.java for a runnable example. Typical facade usage:
FlowQ q = new FlowQ(100, 4, new TokenBucketRateLimiter(10,10)); q.start(); q.submit(new Task("name", () -> {/* work */}, 5, 0, 3)); q.shutdown();
Benchmark Notes
- Benchmarking outputs are produced to the console by
LiveDashboardwhile running the demo.