Skip to content

Thread Safety and Performance

Frody edited this page Sep 3, 2026 · 1 revision

Thread Safety & Concurrency Performance

Fuse was engineered specifically to support modern multi-threaded Java applications and high-density Project Loom virtual thread runtimes.


Lock-Free Concurrency Primitives

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: failureCount and successCount are managed via AtomicInteger (incrementAndGet, set(0)).
  • Timestamp Bookkeeping: lastStateChangeTimestamp is stored in an atomic primitive.
  • Listener Dispatch: Listeners reside in a thread-safe CopyOnWriteArrayList, ensuring zero-lock lockless iterations during state transition broadcasts.

Project Loom & Virtual Threads Compatibility

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.

Allocation Profile & Overhead

Steady-State Memory Profile

During normal operation (CLOSED state):

  • Zero Heap Allocations: When invoking execute(Supplier<T>) or execute(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.

Short-Circuit Latency

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.