| Primitive / Concept | What is it? | Why use it? (The Problem) | Where to use? (Use-Cases) | Key Notes & Trade-offs |
|---|---|---|---|---|
| Volatile Keyword | Lightweight synchronization that guarantees visibility (reads/writes go directly to main memory). | Prevents threads from reading stale data from CPU caches and prevents instruction reordering by the compiler. | Status Flags: Simple boolean signals like shutdownRequested to stop a task. |
Does not guarantee atomicity. Operations like count++ are still unsafe. |
| Atomics | Non-blocking wrapper classes (AtomicInteger, AtomicReference) using hardware CAS (Compare-And-Swap). |
Solves Read-Modify-Write race conditions (like count++) without the overhead of putting threads to sleep. |
Counters & Accumulators: Request counters, High Score trackers, or updating a single variable based on its previous value. | Faster than locks for single variables, but difficult to manage invariants in |
| Source | The "Hack" | Example |
|---|---|---|
| Enactive Mastery | Small Wins | Build a "Hello World" app before a SaaS. |
| Vicarious | "If they can..." | Follow creators who started where you are. |
| Persuasion | Curate your Circle | Cut out the "Golems"; find "Pygmalions." |
| Somatic | Relabel Arousal | "My heart is racing because I'm ready." |
| Imaginal | Future Self | Spend 2 mins visualizing the process of winning. |
| Source of Efficacy | Potency | Mechanism of Action | Perceptual Mediator |
|---|---|---|---|
| Enactive Mastery | Highest | Direct evidence of performance success. | Attribution: Success must be attributed to internal capability, not luck or ease. |
| Vicarious Experience | Moderate (Context Dependent) | Observation of comparable peers. | Similarity: The model must be perceived as similar to the observer; fails in isolation. |
| Social Persuasion | Variable (Catalytic) | Verbal encouragement or feedback. | Credibility: The persuader must be perceived as credible and the feedback as informational. |
| Affective State |
| Benchmark Name | Time Taken (ms) | Memory Used (KB) |
|---|---|---|
| Baseline (No Cache) | 17687 | 28 |
| UnsafeMemoizer | 17508 | 13 |
| SerializedNaiveSafeMemoizer | 18295 | 14 |
| FineGrainedSafeMemoizer | 16927 | 13 |
| FutureSafeMemoizer | 8539 | 19 |
| ExpiringMemoizer (TTL 5s) - First Run | 8489 | 19 |
| ExpiringMemoizer (TTL 5s) - Second Run | 8666 | 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Iteration 5: Expiring Memoizer | |
| * A thread-safe implementation of a memoizer that caches computed results with expiration. | |
| * This implementation uses ConcurrentHashMap for thread-safe cache access and Future | |
| * to handle ongoing computations. Each cached entry has a time-to-live (TTL) after which | |
| * it is considered expired and will be recomputed upon the next request. | |
| */ | |
| public class ExpiringMemoizer<A, V> implements Computable<A, V> { | |
| // ExpiringFuture: holds a Future AND an expiration timestamp | |
| // Used to track when cached entries should be invalidated |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Iteration 4: Future-Based Safe Memoizer | |
| * A thread-safe implementation of a memoizer that caches computed results using Future. | |
| * This class implements the Computable interface and provides a caching mechanism | |
| * to store and reuse previously computed results. | |
| */ | |
| public class FutureSafeMemoizer<A, V> implements Computable<A, V> { | |
| private final Map<A, Future<V>> cache = new ConcurrentHashMap<>(); | |
| private final Computable<A, V> computable; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Iteration 3: Fine-Grained Safe Memoizer | |
| * A thread-safe implementation of a memoizer that caches computed results using fine-grained locking. | |
| * This implementation uses ConcurrentHashMap to allow concurrent access to the cache, | |
| * enabling multiple threads to compute and cache results simultaneously without blocking each other. | |
| */ | |
| public class FineGrainedSafeMemoizer<A,V> implements Computable<A,V> { | |
| private final ConcurrentHashMap<A,V> cache = new ConcurrentHashMap<>(); | |
| private final Computable<A,V> computable; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Iteration 2: Serialized Naive Safe Memoizer | |
| * A thread-safe implementation of a memoizer that caches computed results. | |
| * This implementation synchronizes the entire compute method, ensuring thread safety | |
| * but potentially creating performance bottlenecks under high contention. | |
| */ | |
| public class SerializedNaiveSafeMemoizer<A,V> implements Computable<A,V> { | |
| @GuardedBy("this") // documents that cache is protected by the intrinsic lock | |
| private final Map<A,V> cache = new HashMap<>(); | |
| private final Computable<A,V> computable; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package concurrentcache.cache; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import concurrentcache.computable.Computable; | |
| /** | |
| * A basic memoization implementation that caches computed results. | |
| * This implementation is not thread-safe and should not be used in concurrent environments. |
| Service | Description | CPU Allocated | Memory Allocated |
|---|---|---|---|
| Service 1 | Compute-intensive (e.g., video processing) | cpu1 | mem1 |
| Service 2 | Memory-intensive (e.g., in-memory caching) | cpu2 | mem2 |
| Service 3 | Balanced (e.g., API server) | cpu3 | mem3 |
NewerOlder