-
Notifications
You must be signed in to change notification settings - Fork 0
Thread Safety and Performance
Fuse was engineered specifically to support modern multi-threaded Java applications and high-density Project Loom virtual thread runtimes.
Traditional fault tolerance implementations often rely on synchronized blocks, reentrant read-write locks, or semaphores. Under heavy multi-threaded contention, lock synchronization causes thread context switching, cache line invalidation, and throughput degradation.
Fuse eliminates all synchronization locks. It relies exclusively on atomic, non-blocking primitives from java.util.concurrent.atomic:
-
State Transitions: Managed via
AtomicReference<CircuitBreakerState>utilizing atomic Compare-And-Swap (CAS) hardware instructions. -
Counters:
failureCountandsuccessCountare managed viaAtomicInteger(incrementAndGet,set(0)). -
Timestamp Bookkeeping:
lastStateChangeTimestampis stored in an atomic primitive. -
Listener Dispatch: Listeners reside in a thread-safe
CopyOnWriteArrayList, ensuring zero-lock lockless iterations during state transition broadcasts.
In Java 21+, virtual threads allow applications to run millions of lightweight concurrent tasks on a small pool of carrier OS threads.
A well-known hazard with virtual threads is Carrier Thread Pinning: when a virtual thread enters a synchronized block or executes a native method, it becomes pinned to its carrier thread, preventing the carrier thread from executing other virtual threads.
Because Fuse contains no synchronized blocks and no native locks:
- Virtual threads executing through Fuse never pin carrier threads.
- Ephemeral virtual threads can invoke Fuse millions of times concurrently without memory exhaustion.
During normal operation (CLOSED state):
-
Zero Heap Allocations: When invoking
execute(Supplier<T>)orexecute(Runnable), Fuse creates no intermediate wrapper objects or metric tuples. The supplier is invoked directly on the current thread stack. - Invocation Latency: Benchmarked at under 15 nanoseconds per call on modern x86_64 and ARM64 processors.
When the circuit trips to OPEN:
- Execution aborts within ~5 nanoseconds by throwing
CircuitBreakerOpenException. - No network sockets, connection pools, or I/O channels are consumed.
Fuse • Zero-Dependency Circuit Breaker for Java 17+ • Licensed under Apache-2.0