Chapter 6 - JDK cuncurrency libraries (java.util.package)

0.0(0)
Studied by 1 person
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/52

encourage image

There's no tags or description

Looks like no tags are added yet.

53 Terms

1
New cards

What is an Atomic action?

an atomic action is an operation that happens all at once or not at all. To the rest of the system, it is instantaneous. There is no "in-between" state where another thread can peek in and see the work half-done.

2
New cards

What’s the purpose of an Atomic Action

Without atomicity, concurrent programs suffer from race conditions—where two or more threads manipulate shared data at the same time, and the final outcome depends unpredictably on the exact timing of the threads.

The purpose of making an action atomic is to guarantee data integrity without the massive performance cost of freezing your entire program.

Without atomic actions, if Thread A and Thread B try to increment a counter at the exact same millisecond, they will overlap, overwrite each other's data, and corrupt the final count. An atomic action forces them to take turns implicitly at the CPU hardware level.

3
New cards

What’s the purpose of java.util.concurrent.atomic

Before this package was introduced (in Java 5), if you wanted to make a variable thread-safe, you had to use the synchronized keyword or explicit Lock objects:

Java

// The old, heavy way
public synchronized void increment() {
    this.count++;
}

While synchronized works perfectly, it relies on OS-level locking. When a thread hits a synchronized block that is already in use, that thread is blocked. It gets put to sleep, and the operating system has to context-switch it out of the CPU. This is computationally expensive and slow.

The java.util.concurrent.atomic package was created to provide a faster, lock-free alternative for single variables.

4
New cards

How Atomic classes work?

Instead of locking other threads out, classes like AtomicInteger, AtomicBoolean, and AtomicReference use an ultra-fast CPU hardware instruction called CAS (Compare-And-Swap).

A CAS operation takes three arguments:

  1. The memory location to update.

  2. The expected current value.

  3. The new value you want to set.

The CPU will only update the value if the current value matches your expected value. It does this check-and-update in a single, unbreakable hardware step.

If another thread snuck in and changed the value first, the CAS operation simply fails, returns false, and the atomic variable tries again in a loop (called a spin-lock). Because threads are never put to sleep by the OS, this is incredibly fast.

5
New cards

How to spot an Atomic action?

Look for "Check-Then-Act" Patterns (The Biggest Trap)


An operation is truly atomic if it happens as a single, indivisible unit of work from the perspective of other threads. It either completely happens, or it doesn't happen at all, with no half-baked state visible in between.

Whenever you see an if statement checking a condition, followed by a line of code that changes that same condition, you are looking at individual, non-atomic operations.

Take a look at this snippet from your original code:

Java

if (count.get() != 1_000) {      // Operation 1: Check
    count.getAndAdd(1);          // Operation 2: Act
}

Even though count.get() is thread-safe on its own, and count.getAndAdd(1) is thread-safe on its own, the space between them is not.

Imagine count is currently at 999:

  1. Thread 1 executes the if statement. 999 is not 1,000, so it enters the block.

  2. Context Switch: The CPU pauses Thread 1 right before it increments.

  3. Thread 2 executes the if statement. count is still 999! Thread 2 enters the block.

  4. Both threads now execute their getAndAdd(1) operations. count becomes 1,001. Your limit failed.

Rule of Thumb: If a thread can be paused between the decision and the action, the block is not atomic.


6
New cards

How to spot Read-Modify-Write operations?

Standard Java operations that look like a single step on screen are actually broken down into three distinct CPU instructions under the hood.

Consider standard integer incrementation:

Java

count++; // Looks like 1 operation, but it's actually 3!

Behind the scenes, the CPU does this:

  1. Read: Fetch the current value of count from memory into a local register.

  2. Modify: Add 1 to that value inside the register.

  3. Write: Write the new value back to memory.

If Thread A reads the value (say, 5) and gets paused, Thread B can read the same value (5), increment it to 6, and write it back. When Thread A wakes up, it still thinks the value is 5, increments it to 6, and overwrites Thread B's work. One increment is lost.

7
New cards

What is the notion of what a lock is?

The block-structured approach to synchronization is based on a simple notion of what a lock is. This approach has a number of shortcomings, as follows:

  • Only one type of lock exists.

  • It applies equally to all synchronized operations on the locked object.

  • The lock is acquired at the start of the synchronized block or method.

  • The lock is released at the end of the block or method.

  • Either the lock is acquired or the thread blocks indefinitely—no other out-comes are possible.


<p>The block-structured approach to synchronization is based on a simple notion of what a lock is. This approach has a number of shortcomings, as follows:</p><ul><li><p>Only one type of lock exists.</p></li><li><p>It applies equally to all synchronized operations on the locked object.</p></li><li><p>The lock is acquired at the start of the synchronized block or method.</p></li><li><p>The lock is released at the end of the block or method.</p></li><li><p>Either the lock is acquired or the thread blocks indefinitely—no other out-comes are possible.</p></li></ul><p></p>
8
New cards
  • How the lock interface can be used to completely replicate any functionality that is offered by block-structured concurrency?

  • And learning the Lock ordering (img)


listing 6.1 shows the example from chapter 5 for how to avoid deadlock rewritten to use ReentrantLock. We need to add a lock object as a field to the class, because we will no longer be relying on the intrinsic lock on the object. We also need to maintain the principle that locks are always acquired in the same order. In our example the simple protocol we maintain is that the lock on the object with the lowest account ID is acquired first.

<p>listing 6.1 shows the example from chapter 5 for how to avoid deadlock rewritten to use <em>ReentrantLock</em>. We need to add a lock object as a field to the class, because we will no longer be relying on the intrinsic lock on the object. We also need to maintain the principle that locks are always acquired in the same order. In our example the simple protocol we maintain is that the lock on the object with the lowest account ID is acquired first.</p>
9
New cards

What are Condition objects in Atomics package & What do they do?

Another aspect of the API provided by java.util.concurrent are the condition objects.

These objects play the same role in the API as wait() and notify() do in the original intrinsic API but are more flexible. They provide the ability for threads to wait indefinitely for some condition and to be woken up when that condition becomes true.

However, unlike the intrinsic API (where the object monitor has only a single condition for signaling), the Lock interface allows the programmer to create as many condition objects as they like.

This allows a separation of concerns—for example, the lock can have multiple, disjoint groups of methods that can use separate conditions.

A condition object (which implements the interface Condition) is created by calling the newCondition() method on a lock object (one that implements the Lock interface). As well as condition objects, the API provides a number of latches and barriers as concurrency primitives that may be useful in some circumstances.

10
New cards

CountDownLatch

CountDownLatch is a synchronization utility in java.util.concurrent that lets one or more threads wait until a set of operations being performed in other threads completes. It hasn't changed in Java 25 — it's been stable since Java 5.

How it works

You create it with a fixed count:

CountDownLatch latch = new CountDownLatch(3);

That count can never be reset or increased — it only counts down, and once it hits zero, it stays there (it's a one-shot tool, not reusable).

Two core methods matter:

  • countDown() — decrements the count by one. Called by the threads doing the work.

  • await() — blocks the calling thread until the count reaches zero. Called by the thread(s) waiting for the work to finish. There's also await(timeout, unit) if you don't want to wait forever.

A simple example

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        int workerCount = 3;
        CountDownLatch latch = new CountDownLatch(workerCount);
        ExecutorService pool = Executors.newFixedThreadPool(workerCount);

        for (int i = 0; i < workerCount; i++) {
            int id = i;
            pool.submit(() -> {
                try {
                    System.out.println("Worker " + id + " doing work...");
                    Thread.sleep(500); // simulate work
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    latch.countDown(); // signal completion
                }
            });
        }

        latch.await(); // main thread blocks here until count == 0
        System.out.println("All workers finished. Proceeding.");
        pool.shutdown();
    }
}

The main thread doesn't proceed until all three workers have called countDown().

What it's typically used for

  1. Wait for parallel tasks to finish — the classic case: fan out work to multiple threads, then block until all of them report done, like the example above.

  2. Starting gate / simultaneous kickoff — the reverse pattern: initialize a latch with count 1, have several threads call await() on it, and have a controller thread call countDown() once to release them all at the same time (useful for load-testing or benchmarking "start together" scenarios).

  3. Waiting for service/resource initialization — e.g., don't let requests start being processed until N background resources have finished loading.

Important limitations

  • One-shot — once the count hits zero, you can't reuse it. If you need repeated synchronization points, you want CyclicBarrier instead.

  • No task ordering guarantees — it just tracks a count, not which thread did what.

  • If a thread crashes/throws before calling countDown(), the latch may never reach zero, and any thread in await() could block forever — that's why countDown() is almost always in a finally block, and why timed await() is often safer for production code.


<p>CountDownLatch is a synchronization utility in <code>java.util.concurrent</code> that lets one or more threads wait until a set of operations being performed in other threads completes. It hasn't changed in Java 25 — it's been stable since Java 5.</p><p>How it works</p><p>You create it with a fixed count:</p><pre><code class="language-java">CountDownLatch latch = new CountDownLatch(3);
</code></pre><p>That count can never be reset or increased — it only counts down, and once it hits zero, it stays there (it's a one-shot tool, not reusable).</p><p>Two core methods matter:</p><ul><li><p><code>countDown()</code> — decrements the count by one. Called by the threads doing the work.</p></li><li><p><code>await()</code> — blocks the calling thread until the count reaches zero. Called by the thread(s) waiting for the work to finish. There's also <code>await(timeout, unit)</code> if you don't want to wait forever.</p></li></ul><p>A simple example</p><pre><code class="language-java">import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        int workerCount = 3;
        CountDownLatch latch = new CountDownLatch(workerCount);
        ExecutorService pool = Executors.newFixedThreadPool(workerCount);

        for (int i = 0; i &lt; workerCount; i++) {
            int id = i;
            pool.submit(() -&gt; {
                try {
                    System.out.println("Worker " + id + " doing work...");
                    Thread.sleep(500); // simulate work
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    latch.countDown(); // signal completion
                }
            });
        }

        latch.await(); // main thread blocks here until count == 0
        System.out.println("All workers finished. Proceeding.");
        pool.shutdown();
    }
}
</code></pre><p>The main thread doesn't proceed until all three workers have called <code>countDown()</code>.</p><p>What it's typically used for</p><ol><li><p><strong>Wait for parallel tasks to finish</strong> — the classic case: fan out work to multiple threads, then block until all of them report done, like the example above.</p></li><li><p><strong>Starting gate / simultaneous kickoff</strong> — the reverse pattern: initialize a latch with count 1, have several threads call <code>await()</code> on it, and have a controller thread call <code>countDown()</code> once to release them all at the same time (useful for load-testing or benchmarking "start together" scenarios).</p></li><li><p><strong>Waiting for service/resource initialization</strong> — e.g., don't let requests start being processed until N background resources have finished loading.</p></li></ol><p>Important limitations</p><ul><li><p><strong>One-shot</strong> — once the count hits zero, you can't reuse it. If you need repeated synchronization points, you want <code>CyclicBarrier</code> instead.</p></li><li><p><strong>No task ordering guarantees</strong> — it just tracks a count, not which thread did what.</p></li><li><p>If a thread crashes/throws before calling <code>countDown()</code>, the latch may never reach zero, and any thread in <code>await()</code> could block forever — that's why <code>countDown()</code> is almost always in a <code>finally</code> block, and why timed <code>await()</code> is often safer for production code.</p></li></ul><p></p>
11
New cards

Is the number of counts in the CountDownLatch arbitrary?

Good question — it's not arbitrary at all, but it's also not computed by the JVM or anything automatic. You choose the number, and it must equal the exact number of countDown() calls you expect to happen before the waiting thread(s) should proceed.

What the number represents

The count is however many "events" or "signals" need to occur before await() unblocks. It's up to you to make it match your actual workload. Common patterns:

1. One count per worker thread/task

int workerCount = 3;
CountDownLatch latch = new CountDownLatch(workerCount); // 3, because 3 workers

If you're processing a list of 50 files across 50 tasks, the count is 50. If you spin up a thread pool with 10 tasks, it's 10.

2. One count per distinct step/resource, regardless of thread count

CountDownLatch latch = new CountDownLatch(3); // e.g. DB connected, cache warmed, config loaded

Here it's not about thread count — it's about 3 specific initialization steps completing, maybe even from different parts of the code.

3. Count of exactly 1, for the "starting gate" pattern

CountDownLatch startSignal = new CountDownLatch(1);

Here the number 1 just means "a single go signal" — many threads await() on it, but only one countDown() call releases them all.

The critical constraint

The number must match reality exactly, or you get bugs:

  • If you set the count too high (e.g., new CountDownLatch(5) but only 4 things ever call countDown()), await() blocks forever — a silent deadlock.

  • If you set it too low (e.g., new CountDownLatch(2) but 3 things call countDown()), the latch reaches zero early and releases waiters before all the work is actually done. Extra countDown() calls beyond zero are just ignored — no error, no exception, so this can fail silently.

So in practice, the count is almost always derived directly from something concrete in your code: list.size(), a thread pool's task count, or a fixed number of known initialization steps you're writing by hand. It's a number you calculate or know in advance, not one you guess.

12
New cards

CountDownLatch in moden java of VT

Java 25 context

Nothing about CountDownLatch itself changed in Java 25. If you're doing this kind of coordination in modern Java, though, it's worth knowing that virtual threads (finalized in Java 21) make the "spin up thousands of threads and wait on a latch" pattern much cheaper than it used to be with platform threads — and Java 25 also has structured concurrency (finalized as a stable feature in this release via JEP 505) as a higher-level alternative for many "fan out, then join" scenarios, where you don't manage the countdown manually — the API tracks completion of a group of subtasks for you and handles cancellation/error propagation automatically. CountDownLatch is still perfectly valid and often simpler for straightforward wait-for-N-things-to-finish or release-N-threads-at-once cases.


When do you still want CountDownLatch (even with virtual threads)

Case 3: Threads doing the waiting aren't the ones managing the tasks: If your waiting logic is decoupled from your task-submission logic (e.g., a completely separate part of the codebase needs to block until some background initialization finishes, and there's no natural "join point" like a try-with-resources), a latch is still the simplest tool. Virtual threads don't remove the need for that.

Case 4: The "release everyone at once" starting-gate pattern:
CountDownLatch(1) used to synchronize many threads to start at the same instant has no equivalent in plain executor APIs — this is a latch-specific use case regardless of thread type.

Case 5: You need partial/rolling progress tracking, not just "all done": If something needs to observe the count dropping (e.g., a progress bar), a latch (or a custom counter) fits naturally; invokeAll/close() only give you an all-or-nothing block. When to reach for structured concurrency instead (Java 25 preferred default)

Case 6: Fan-out/fan-in where you also want cancellation & error propagation

try (var scope = StructuredTaskScope.open()) {
    Subtask<String> t1 = scope.fork(() -> fetchUser());
    Subtask<String> t2 = scope.fork(() -> fetchOrders());
    scope.join(); // waits for both, propagates exceptions, handles cancellation
    ...
}

If one subtask fails, structured concurrency can automatically cancel the others — CountDownLatch and even plain virtual threads give you no such behavior; you'd have to build it manually.

13
New cards

What is concurrentHashMap

The ConcurrentHashMap class provides a concurrent version of the standard HashMap. In general, maps are a very useful (and common) data structure for building concurrent applications. This is due, at least in part, to the shape of the underlying data structure. Let’s take a closer look at the basic HashMap to understand why.

14
New cards

Understanding a simplified HashMap

the classic Java HashMap uses a function (the hash function) to determine which bucket it will store the key-value pair in. This is where the “hash” part of the class’s name comes from.

The key-value pair is actually stored in a linked list (known as the hash chain) that starts from the bucket corresponding to the index obtained by hashing the key.

<p>the classic Java HashMap uses a function (the hash function) to determine which bucket it will store the key-value pair in. This is where the “hash” part of the class’s name comes from.</p><p>The key-value pair is actually stored in a linked list (known as the hash chain) that starts from the bucket corresponding to the index obtained by hashing the key.</p>
15
New cards

The underlying emchanisms of get() in a HashMap

First, the get() method deals with the irritating null case. Following that, we use the key object’s hash code to construct an index into the array table. An unwritten assumption says that the size of table is a power of two, so the operation of indexFor() is basically a modulo operation, which ensures that the return value is a valid index into table.

Now that we have an index into table, we use it to select the relevant hash chain for our lookup operation. We start at the head and walk down the hash chain. At each step we evaluate whether we’ve found our key object,

16
New cards

What problem does NOT resizing the hash table cause, and what complexity is lost?

As elements grow, more keys collide into the same bucket. Lookup degrades toward O(N) instead of the expected O(1)/O(log N). The key advantage of hashing — fast retrieval — is lost entirely without dynamic resizing.

17
New cards

What are the two serious runtime limitations of Dictionary that make it unfit for production?

1. No table resizing — performance degrades toward O(N) as elements accumulate.

2. No defence against pathological hashCode() implementations — a badly-behaved key collapses all lookups to O(N).

Note: Java's own HashMap has neither of these problems — it auto-resizes on load factor threshold and since Java 8 converts overloaded buckets to red-black trees (O(log N) worst case).

18
New cards

Core problem

  1. Why is the plain Dictionary class not thread-safe? Give a concrete example.

  2. What are the two naive approaches to making Dictionary thread-safe, and what is the verdict on both?


Two threads can interleave operations on the same key and both report success when only one actually took effect. Example: Thread A deletes a key while Thread B updates the value for that same key — depending on execution order, both return success but only one operation actually happened. There is no mutual exclusion protecting the read-modify-write sequence.

Full synchronization — make every method synchronized, either directly or via a wrapper. 2. Immutability — wrap Dictionary so all mutating methods throw UnsupportedOperationException. Both fail: synchronization has prohibitive performance overhead, and both share the same deeper flaw — the underlying mutable object still exists and can be accessed directly, bypassing any protection.

<p>Two threads can interleave operations on the same key and both report success when only one actually took effect. Example: Thread A deletes a key while Thread B updates the value for that same key — depending on execution order, both return success but only one operation actually happened. There is no mutual exclusion protecting the read-modify-write sequence.</p><p>Full synchronization — make every method synchronized, either directly or via a wrapper. 2. Immutability — wrap Dictionary so all mutating methods throw <code>UnsupportedOperationException</code>. Both fail: synchronization has prohibitive performance overhead, and both share the same deeper flaw — the underlying mutable object still exists and can be accessed directly, bypassing any protection.</p>
19
New cards
<p><strong><u>Approach 1: Synchronization</u></strong></p><ol><li><p>What are the two concrete ways to add full synchronization to Dictionary, and what is wrong with each?</p></li><li><p>What is the shared-reference flaw in <code>SynchronizedDictionary</code>, and why does it make the approach fundamentally broken?</p></li><li><p>What JDK method mirrors <code>SynchronizedDictionary</code>, and what is the book's verdict on it?</p></li></ol><p></p>

Approach 1: Synchronization

  1. What are the two concrete ways to add full synchronization to Dictionary, and what is wrong with each?

  2. What is the shared-reference flaw in SynchronizedDictionary, and why does it make the approach fundamentally broken?

  3. What JDK method mirrors SynchronizedDictionary, and what is the book's verdict on it?


knowt flashcard image


<img src="https://assets.knowt.com/user-attachments/a0292739-780e-40d6-851e-cfb2dc236d69.png" data-width="100%" data-align="center" alt="knowt flashcard image"><p></p>
20
New cards
<p><strong><u>Approach 2: Immutability</u></strong></p><ol><li><p>How does <code>ImmutableDictionary</code> attempt thread safety, and what mechanism does it rely on?</p></li><li><p>Why does the book call throwing <code>UnsupportedOperationException</code> on mutating methods "terrible from a language design point of view"?</p></li><li><p>What well-known JDK API is forced into the same <code>UnsupportedOperationException</code> compromise as <code>ImmutableDictionary</code>?</p></li></ol><p></p>

Approach 2: Immutability

  1. How does ImmutableDictionary attempt thread safety, and what mechanism does it rely on?

  2. Why does the book call throwing UnsupportedOperationException on mutating methods "terrible from a language design point of view"?

  3. What well-known JDK API is forced into the same UnsupportedOperationException compromise as ImmutableDictionary?


knowt flashcard image


21
New cards

Card 9 — Both approaches & Setup for the solution

  1. What single fundamental flaw do both SynchronizedDictionary and ImmutableDictionary share?

  2. What key insight do both failed approaches reveal about what a correct concurrent map actually needs?


Card 9 — Both approaches

Back: In both cases the underlying mutable Dictionary delegate still exists as a separately reachable object. Any caller who holds a reference to that original object can read and mutate it directly, completely bypassing the wrapper. The wrapper cannot seal off the inner object — it can only intercept calls that go through itself. Neither approach actually achieves its stated goal in a real codebase.


Card 10 — Setup for the solution

Back: The wrappers fail because concurrency control is bolted on top of a mutable object they do not own exclusively. A correct solution needs the concurrency control built into the data structure itself from the ground up — not layered on via a wrapper. This is exactly what ConcurrentHashMap provides: fine-grained internal locking per bucket and atomic operations baked into the implementation, with no separately accessible unsynchronized object underneath.

<p><strong>Card 9 — Both approaches</strong><br><br>Back: In both cases the underlying mutable <code>Dictionary</code> delegate still exists as a separately reachable object. Any caller who holds a reference to that original object can read and mutate it directly, completely bypassing the wrapper. The wrapper cannot seal off the inner object — it can only intercept calls that go through itself. Neither approach actually achieves its stated goal in a real codebase.</p><div data-type="horizontalRule"><hr></div><p class="font-claude-response-body break-words whitespace-normal"><strong>Card 10 — Setup for the solution</strong><br><br>Back: The wrappers fail because concurrency control is bolted on top of a mutable object they do not own exclusively. A correct solution needs the concurrency control built into the data structure itself from the ground up — not layered on via a wrapper. This is exactly what <code>ConcurrentHashMap</code> provides: fine-grained internal locking per bucket and atomic operations baked into the implementation, with no separately accessible unsynchronized object underneath.</p>
22
New cards

Interfaces can't express "optional" vs "mandatory" methods in Java

Map (and List, Collection, etc.) are defined as single, monolithic interfaces that bundle together both read operations (get, size, containsKey) and mutation operations (put, remove, clear, putAll) into one contract. There's no way in the interface itself to say "these methods are required, but these other ones are optional depending on the implementation."

That means if you want to build an immutable map — one that genuinely cannot be modified after construction — you're still forced to write a class that implements put(), remove(), clear(), and every other mutating method, because the compiler requires every method of the interface to have a method body. There is no Map variant, no sub-interface, no annotation, that lets you say "I only support the read half of this contract."

The "back door" and why it's unsatisfactory (Solutions)

Since Java can't structurally express "this method isn't supported," the workaround the JDK designers landed on decades ago was: implement the method anyway, but make its body just throw UnsupportedOperationException immediately. This technically satisfies the compiler (the interface contract says "you must provide a method with this signature," and throwing an exception counts as "providing" it), but it violates the actual meaning of an interface contract — which is supposed to be a promise that calling any method will do what it says, not potentially blow up at runtime.

<p><code>Map</code> (and <code>List</code>, <code>Collection</code>, etc.) are defined as single, monolithic interfaces that bundle together both read operations (<code>get</code>, <code>size</code>, <code>containsKey</code>) and mutation operations (<code>put</code>, <code>remove</code>, <code>clear</code>, <code>putAll</code>) into one contract. There's no way in the interface itself to say "these methods are required, but these other ones are optional depending on the implementation."</p><p class="font-claude-response-body break-words whitespace-normal">That means if you want to build an <strong>immutable</strong> map — one that genuinely cannot be modified after construction — you're still forced to write a class that implements <code>put()</code>, <code>remove()</code>, <code>clear()</code>, and every other mutating method, because the compiler requires every method of the interface to have a method body. There is no <code>Map</code> variant, no sub-interface, no annotation, that lets you say "I only support the read half of this contract."</p><p class="font-claude-response-body break-words whitespace-normal"><strong>The "back door" and why it's unsatisfactory (Solutions)</strong></p><p class="font-claude-response-body break-words whitespace-normal">Since Java can't structurally express "this method isn't supported," the workaround the JDK designers landed on decades ago was: implement the method anyway, but make its body just throw <code>UnsupportedOperationException</code> immediately. This technically satisfies the compiler (the interface contract says "you must provide a method with this signature," and throwing an exception counts as "providing" it), but it violates the <em>actual</em> meaning of an interface contract — which is supposed to be a promise that calling any method will do what it says, not potentially blow up at runtime.</p>
23
New cards

Using ConcurrentHashMap - Example of using HashMap with multiple threads

Having shown a simple map implementation and discussed approaches that we could use to make it concurrent, it’s time to meet the ConcurrentHashMap. In some ways, this is the easy part: it is an extremely easy-to-use class and in most cases is a drop-in replacement for HashMap.

The key point about the ConcurrentHashMap is that it is safe for multiple threads to update it at once. To see why we need this, let’s see what happens when we have two threads adding entries to a HashMap simultaneously (exception handling elided):

<p>Having shown a simple map implementation and discussed approaches that we could use to make it concurrent, it’s time to meet the ConcurrentHashMap. In some ways, this is the easy part: it is an extremely easy-to-use class and in most cases is a drop-in replacement for HashMap.</p><p>The key point about the ConcurrentHashMap is that it is safe for multiple threads to update it at once. To see why we need this, let’s see what happens when we have two threads adding entries to a HashMap simultaneously (exception handling elided):</p>
24
New cards

Using ConcurrentHashMap - How does ConcurrentHashMap avoid locking the whole structure when making a change?

Since Java 8, ConcurrentHashMap no longer uses fixed segments. Instead, it stores entries in an array of buckets (bins), just like HashMap internally. For an insert into an empty bucket, it uses a lock-free CAS (compare-and-swap) operation — no lock at all.

Only when there's a collision (the bucket already has a node) does it fall back to a synchronized block on that individual bucket's head node. This means the lock, when needed, is scoped to a single bucket — not a whole segment of buckets as in pre-Java 8 versions.

Reads are essentially always lock-free, using volatile access to guarantee visibility.

If two threads need to write to the same bucket, they still exclude each other, but this fine-grained approach gives much better throughput than synchronizing the entire map.

As the map grows and the table resizes to more buckets, lock contention naturally decreases further since collisions become less likely.

<p>Since Java 8, <code>ConcurrentHashMap</code> no longer uses fixed segments. Instead,<strong> it stores entries in an array of buckets (bins), just like </strong><code>HashMap</code><strong> internally. For an insert into an empty bucket, it uses a lock-free CAS (compare-and-swap) operation — no lock at all.</strong></p><p><strong>Only when there's a collision (the bucket already has a node) does it fall back to a </strong><code>synchronized</code><strong> block on that individual bucket's head node.</strong> This means the lock, when needed, is scoped to a single bucket — not a whole segment of buckets as in pre-Java 8 versions.</p><p>Reads are essentially always lock-free, using volatile access to guarantee visibility. </p><p>If two threads need to write to the <em>same</em> bucket, they still exclude each other, but this fine-grained approach gives much better throughput than synchronizing the entire map. </p><p>As the map grows and the table resizes to more buckets, lock contention naturally decreases further since collisions become less likely.</p>
25
New cards

When to use a ConcurrencutHashMap

Using ConcurrentHashMap can be almost too simple. In many cases, if you have a multithreaded program and need to share data, then just use a Map, and have the implementation be a ConcurrentHashMap. In fact, if there is ever a chance that a Map might need to be modified by more than one thread, then you should always use the concurrent implementation.

It does use considerably more resources than a plain HashMap and will have worse throughput due to the synchronization of some operations. However, those inconveniences are nothing when compared to the possibility of a race condition leading to Lost Update or an infinite loop.

Finally, we should also note that ConcurrentHashMap actually implements the ConcurrentMap interface, which extends Map.

26
New cards
  1. Why don't Map's two concurrency patterns (full sync, immutability) work well for List?

  2. and how does CopyOnWriteArrayList solve the problem differently?


The list's linear structure can't be partitioned like a hash map's buckets — even a linked list sees contention under append-heavy workloads.

CopyOnWriteArrayList instead uses copy-on-write semantics: every mutating operation creates an entirely new backing array rather than modifying the existing one.

This means iterators are guaranteed never to throw ConcurrentModificationException and never see additions, removals, or changes made after the iterator was created — they read a frozen snapshot.

Caveat: only the list structure is protected; if elements are themselves mutable objects, they can still be mutated directly by another thread.

<p>The list's linear structure can't be partitioned like a hash map's buckets — even a linked list sees contention under append-heavy workloads.</p><p>CopyOnWriteArrayList instead uses copy-on-write semantics: every mutating operation creates an entirely new backing array rather than modifying the existing one. </p><p>This means iterators are guaranteed never to throw ConcurrentModificationException and never see additions, removals, or changes made after the iterator was created — they read a frozen snapshot. </p><p>Caveat: only the list structure is protected; if elements are themselves mutable objects, they can still be mutated directly by another thread.</p>
27
New cards

The cost of using CopyOnWriteArrayList & When to use it

CopyOnWriteArrayList is usually too expensive for general use because every write operation (add, remove, set) copies the entire backing array. However, it is an excellent choice when reads vastly outnumber writes, and you want thread-safe iteration without explicit synchronisation.

Typical use cases:

  • Event/listener registries (List<EventListener>)

  • Subscriber lists

  • Configuration data that changes rarely

  • Feature flags or cached reference data read by many threads


28
New cards

Example of how CopyOnWriteArrayList idea is implemented

knowt flashcard image


In general, the use of the CopyOnWriteArrayList class does require a bit more thought than using ConcurrentHashMap, which is basically a drop-in concurrent replacement for HashMap because of performance issues—the copy-on-write property means that if the list is altered, the entire array must be copied. If changes to the list are common, compared to read accesses, this approach won’t necessarily yield high performance.

<img src="https://assets.knowt.com/user-attachments/f6554211-9f63-4b58-9fec-94895ef606da.png" data-width="100%" data-align="center" alt="knowt flashcard image"><p></p><p>In general, the use of the CopyOnWriteArrayList class does require a bit more thought than using ConcurrentHashMap, which is basically a drop-in concurrent replacement for HashMap because of performance issues—the copy-on-write property means that if the list is altered, the entire array must be copied. If changes to the list are common, compared to read accesses, this approach won’t necessarily yield high performance.</p>
29
New cards

Difference between CopyOnWriteArrayList and synchronizedList()

In general, the CopyOnWriteArrayList makes different trade-offs than synchronizedList(). The latter synchronizes on all operations, so reads from different threads can block each other, which is not true for a COW data structure.

On the other hand, CopyOnWriteArrayList copies the backing array on every mutation, whereas the synchronized version does so only when the backing array is full (the same behavior as ArrayList).

However, as we’ll say repeatedly in chapter 7, reasoning about code from first principles is extremely difficult—the only way to reliably get well-performing code is to test, retest, and measure the results.

30
New cards

What is BlockingQueue

A BlockingQueue is an interface in java.util.concurrent designed specifically for producer-consumer patterns in multithreaded code. It's a queue that automatically handles thread coordination for you — instead of manually writing wait()/notify() logic, the queue itself blocks a thread when it can't proceed.

The core idea

  • If a consumer thread tries to take an item from an empty queue → it blocks (waits) until another thread puts something in.

  • If a producer thread tries to put an item into a full, bounded queue → it blocks until a consumer removes something and frees up space.

One very common use case, and the one we’ll focus on, is the use of a queue to transfer work units between threads. This pattern is often ideally suited for the simplest concurrent extension of Queue—the BlockingQueue.

A number of patterns in multithreaded Java programming rely heavily on the thread-safe implementations of Queue, so it’s important that you fully understand it.

31
New cards

What are the two additional special properties of BlockingQueue?

The BlockingQueue is a queue that has the following two additional special

properties:

  • When trying to put() to the queue, it will cause the putting thread to wait for space to become available if the queue is full.

  • When trying to take() from the queue, it will cause the taking thread to block if the queue is empty.

These two properties are very useful because if one thread (or pool of threads) is out-stripping the ability of the other to keep up, the faster thread is forced to wait, thus regulating the overall system. This is illustrated in figure 6.5.

<p>The BlockingQueue is a queue that has the following two additional special</p><p>properties:</p><ul><li><p>When trying to put() to the queue, it will cause the putting thread to wait for space to become available if the queue is full.</p></li><li><p>When trying to take() from the queue, it will cause the taking thread to block if the queue is empty.</p></li></ul><p>These two properties are very useful because if one thread (or pool of threads) is out-stripping the ability of the other to keep up, the faster thread is forced to wait, thus regulating the overall system. This is illustrated in figure 6.5.</p>
32
New cards

What are the implementations of the BlockingQueue?

Java ships with two basic implementations of the BlockingQueue interface: the LinkedBlockingQueue and the ArrayBlockingQueue. They offer slightly different properties;

For example, the array implementation is very efficient when an exact bound is known for the size of the queue, whereas the linked implementation may be slightly faster under some circumstances.

33
New cards

BlockingQueue’s implementation of back-preassure

It comes down to implied capacity semantics. LinkedBlockingQueue can be given a size limit, but is usually created without one — defaulting to Integer.MAX_VALUE, which is effectively infinite. A real application would never recover from a backlog of over two billion items, so in practice put() never actually blocks — producer threads can write at an unlimited rate.

ArrayBlockingQueue, by contrast, has a genuinely fixed size (the size of its backing array). If producers outpace consumers, the queue eventually fills up, and further put() calls block — forcing producer threads to slow down to match the rate consumers can keep up with.

Key takeaway: ArrayBlockingQueue provides real backpressure by design; LinkedBlockingQueue only provides backpressure if you explicitly bound it — left unbounded, it offers none.

34
New cards
<p>Let’s see the BlockingQueue in action in an example: Altering the account example to use queues and threads. The aim of the example will be to get rid of the need to lock both account objects. The basic architecture of the application is shown in figure 6.6.</p>

Let’s see the BlockingQueue in action in an example: Altering the account example to use queues and threads. The aim of the example will be to get rid of the need to lock both account objects. The basic architecture of the application is shown in figure 6.6.

Listing 6.4 The AccountManager class

public class AccountManager {
    private ConcurrentHashMap<Integer, Account> accounts =
        new ConcurrentHashMap<>();
    private volatile boolean shutdown = false;
    private BlockingQueue<TransferTask> pending =
        new LinkedBlockingQueue<>();
    private BlockingQueue<TransferTask> forDeposit =
        new LinkedBlockingQueue<>();
    private BlockingQueue<TransferTask> failed =
        new LinkedBlockingQueue<>();
    private Thread withdrawals;
    private Thread deposits;

The blocking queues contain TransferTask objects, which are simple data carriers that denote the transfer to be made, as shown next:

public class TransferTask {
    private final Account sender;
    private final Account receiver;
    private final int amount;

    public TransferTask(Account sender, Account receiver, int amount) {
        this.sender = sender;
        this.receiver = receiver;
        this.amount = amount;
    }

    public Account sender() {
        return sender;
    }

    public int amount() {
        return amount;
    }

    public Account receiver() {
        return receiver;
    }
    // Other methods elided
}

There is no additional semantics for the transfer—the class is just a dumb data carrier type.

NOTE The TransferTask type is very simple and, in Java 17, could be written as a record type (which we met in chapter 3).

The AccountManager class provides functionality for accounts to be created and for transfer tasks to be submitted, as illustrated here:

public Account createAccount(int balance) {
    var out = new Account(balance);
    accounts.put(out.getAccountId(), out);
    return out;
}

public void submit(TransferTask transfer) {
    if (shutdown) {
        return false;
    }
    return pending.add(transfer);
}

The real work of the AccountManager is handled by the two threads that manage the transfer tasks between the queues. Let's look at the withdraw operation first:

public void init() {
    Runnable withdraw = () -> {
        boolean interrupted = false;
        while (!interrupted || !pending.isEmpty()) {
            try {
                var task = pending.take();
                var sender = task.sender();
                if (sender.withdraw(task.amount())) {
                    forDeposit.add(task);
                } else {
                    failed.add(task);
                }
            } catch (InterruptedException e) {
                interrupted = true;
            }
        }
        deposits.interrupt();
    };

The deposit operation is defined similarly, and then we initialize the account manager with the tasks:

    Runnable deposit = () -> {
        boolean interrupted = false;
        while (!interrupted || !forDeposit.isEmpty()) {
            try {
                var task = forDeposit.take();
                var receiver = task.receiver();
                receiver.deposit(task.amount());
            } catch (InterruptedException e) {
                interrupted = true;
            }
        }
    };

    init(withdraw, deposit);
}

The package-private overload of the init() method is used to start the background threads. It exists as a separate method to allow for easier testing, as follows:

void init(Runnable withdraw, Runnable deposit) {
    withdrawals = new Thread(withdraw);
    deposits = new Thread(deposit);
    withdrawals.start();
    deposits.start();
}

We need some code to drive this:

var manager = new AccountManager();
manager.init();
var acc1 = manager.createAccount(1000);
var acc2 = manager.createAccount(20_000);
var transfer = new TransferTask(acc1, acc2, 100);
manager.submit(transfer);          // Submits the transfer from acc1 to acc2
Thread.sleep(5000);                // Sleeps to allow time for the transfer to execute
System.out.println(acc1);
System.out.println(acc2);
manager.shutdown();
manager.await();

This produces output like this:

Account{accountId=1, balance=900.0,
lock=java.util.concurrent.locks.ReentrantLock@58372a00[Unlocked]}
Account{accountId=2, balance=20100.0,
lock=java.util.concurrent.locks.ReentrantLock@4dd8dc3[Unlocked]}

However, the code as written does not execute cleanly, despite the calls to shutdown() and await() because of the blocking nature of the calls used. Let's look at figure 6.7 to see why.

<p><strong>Listing 6.4</strong> <strong>The AccountManager class</strong></p><pre><code class="language-java">public class AccountManager {
    private ConcurrentHashMap&lt;Integer, Account&gt; accounts =
        new ConcurrentHashMap&lt;&gt;();
    private volatile boolean shutdown = false;
    private BlockingQueue&lt;TransferTask&gt; pending =
        new LinkedBlockingQueue&lt;&gt;();
    private BlockingQueue&lt;TransferTask&gt; forDeposit =
        new LinkedBlockingQueue&lt;&gt;();
    private BlockingQueue&lt;TransferTask&gt; failed =
        new LinkedBlockingQueue&lt;&gt;();
    private Thread withdrawals;
    private Thread deposits;
</code></pre><p>The blocking queues contain <code>TransferTask</code> objects, which are simple data carriers that denote the transfer to be made, as shown next:</p><pre><code class="language-java">public class TransferTask {
    private final Account sender;
    private final Account receiver;
    private final int amount;

    public TransferTask(Account sender, Account receiver, int amount) {
        this.sender = sender;
        this.receiver = receiver;
        this.amount = amount;
    }

    public Account sender() {
        return sender;
    }

    public int amount() {
        return amount;
    }

    public Account receiver() {
        return receiver;
    }
    // Other methods elided
}
</code></pre><p>There is no additional semantics for the transfer—the class is just a dumb data carrier type.</p><figure data-type="blockquoteFigure"><div><blockquote><p><strong>NOTE</strong> The <code>TransferTask</code> type is very simple and, in Java 17, could be written as a record type (which we met in chapter 3).</p></blockquote><figcaption></figcaption></div></figure><p>The <code>AccountManager</code> class provides functionality for accounts to be created and for transfer tasks to be submitted, as illustrated here:</p><pre><code class="language-java">public Account createAccount(int balance) {
    var out = new Account(balance);
    accounts.put(out.getAccountId(), out);
    return out;
}

public void submit(TransferTask transfer) {
    if (shutdown) {
        return false;
    }
    return pending.add(transfer);
}
</code></pre><p>The real work of the <code>AccountManager</code> is handled by the two threads that manage the transfer tasks between the queues. Let's look at the withdraw operation first:</p><pre><code class="language-java">public void init() {
    Runnable withdraw = () -&gt; {
        boolean interrupted = false;
        while (!interrupted || !pending.isEmpty()) {
            try {
                var task = pending.take();
                var sender = task.sender();
                if (sender.withdraw(task.amount())) {
                    forDeposit.add(task);
                } else {
                    failed.add(task);
                }
            } catch (InterruptedException e) {
                interrupted = true;
            }
        }
        deposits.interrupt();
    };
</code></pre><p>The deposit operation is defined similarly, and then we initialize the account manager with the tasks:</p><pre><code class="language-java">    Runnable deposit = () -&gt; {
        boolean interrupted = false;
        while (!interrupted || !forDeposit.isEmpty()) {
            try {
                var task = forDeposit.take();
                var receiver = task.receiver();
                receiver.deposit(task.amount());
            } catch (InterruptedException e) {
                interrupted = true;
            }
        }
    };

    init(withdraw, deposit);
}
</code></pre><p>The package-private overload of the <code>init()</code> method is used to start the background threads. It exists as a separate method to allow for easier testing, as follows:</p><pre><code class="language-java">void init(Runnable withdraw, Runnable deposit) {
    withdrawals = new Thread(withdraw);
    deposits = new Thread(deposit);
    withdrawals.start();
    deposits.start();
}
</code></pre><p>We need some code to drive this:</p><pre><code class="language-java">var manager = new AccountManager();
manager.init();
var acc1 = manager.createAccount(1000);
var acc2 = manager.createAccount(20_000);
var transfer = new TransferTask(acc1, acc2, 100);
manager.submit(transfer);          // Submits the transfer from acc1 to acc2
Thread.sleep(5000);                // Sleeps to allow time for the transfer to execute
System.out.println(acc1);
System.out.println(acc2);
manager.shutdown();
manager.await();
</code></pre><p>This produces output like this:</p><pre><code>Account{accountId=1, balance=900.0,
lock=java.util.concurrent.locks.ReentrantLock@58372a00[Unlocked]}
Account{accountId=2, balance=20100.0,
lock=java.util.concurrent.locks.ReentrantLock@4dd8dc3[Unlocked]}
</code></pre><p>However, the code as written does not execute cleanly, despite the calls to <code>shutdown()</code> and <code>await()</code> because of the blocking nature of the calls used. Let's look at figure 6.7 to see why.</p>
35
New cards

Interacting with BlockingQueue APIs

BlockingQueue inherits from Queue and offers three distinct strategy families for handling bounds (full or empty queue):

  1. Blocking (put / take): Suspends the execution thread until space or an item becomes available.

  2. Special Value / Non-Blocking (offer / poll): Fails fast by returning a specific value (false for offer, null for poll).

    • Timeouts: offer(e, time, unit) and poll(time, unit) allow waiting up to a threshold before returning false/null. This prevents threads from blocking indefinitely, allowing clean worker thread shutdown loops.

  3. Exception Throwing (add / remove): Throws unchecked runtime exceptions (IllegalStateException / NoSuchElementException). Avoid these completely—using exceptions for normal flow control is an anti-pattern that incurs unnecessary stack-trace overhead.


36
New cards

What are the three behavior strategies BlockingQueue methods use when a queue is full or empty?

  • Block: Wait until the queue accepts or yields an item.

  • Special Value: Return a failure code (false / null) immediately or after a timeout.

  • Throw Exception: Throw a runtime exception immediately.


37
New cards

How do the blocking, non-blocking, and exception-throwing methods map across BlockingQueue operations?

  • Insert: put(e) (block), offer(e) (special value), add(e) (exception)

  • Remove: take() (block), poll() (special value), remove() (exception)

  • Examine: element() (exception), peek() (special value)


38
New cards

Why use timed poll() or offer() over standard take() or put() in multi-threaded worker loops?


Unbounded take() or put() calls can cause threads to block indefinitely when no work arrives. Adding a timeout (e.g., poll(5, TimeUnit.SECONDS)) wakes the thread periodically. This allows it to re-check shutdown flags or health state and exit gracefully in bounded time.

39
New cards

Why should you avoid add() and remove() on a BlockingQueue?

  • Empty or full queues are normal operational states, not unexpected system failures.

  • Throwing exceptions for control flow violates standard design practices.

  • Constructing stack traces and unwinding calls creates unnecessary performance overhead.


40
New cards
<p>Avoiding generic interfaces of Queue (check img) - Why?</p>

Avoiding generic interfaces of Queue (check img) - Why?

The reason for doing this is that this level of indirection provides a place to add additional metadata without compromising the conceptual integrity of the contained type (MyAwesomeClass, in this example). In figure 6.8, we can see how the external metadata wrapper works.

This is surprisingly useful. Use cases where additional metadata is helpful are abundant. Here are a few examples:

  • Testing (such as showing the change history for an object)

  • Performance indicators (such as time of arrival or quality of service)

  • Runtime system information (such as how this instance of MyAwesomeClass has been routed)

It can be much harder to add in this indirection after the fact. If you later discover that more metadata is needed in certain circumstances, it can be a major refactoring job to add in what would have been a simple change in the WorkUnit class.

41
New cards

What’s a Future in java.util.concurrent

The interface Future is a simple representation of an asynchronous task. It is a type that holds the result from a task that may not have finished yet but may at some point in the future. The primary methods on a Future follow:

  • get(): gets the result, if the result isn’t yet available, will block until it is.

  • isDone(): allows the caller to determine whether the computation has finished. It is non-blocking.

  • cancel(): allows the computation to be canceled before completion.

there’s a version of get() that takes a timeout, which won’t block forever.

<p>The interface <em>Future</em> is a simple representation of an asynchronous task. It is a type that holds the result from a task that may not have finished yet but may at some point in the future. The primary methods on a <em>Future</em> follow:</p><ul><li><p>get(): gets the result, if the result isn’t yet available, will block until it is.</p></li><li><p>isDone(): allows the caller to determine whether the computation has finished. It is non-blocking.</p></li><li><p>cancel(): allows the computation to be canceled before completion.</p></li></ul><p>there’s a version of get() that takes a timeout, which won’t block forever.</p>
42
New cards

Future with non-blocking I/O example

As a second example, let’s consider non-blocking I/O. Figure 6.9 shows the Future in action to allow us to use a background thread for I/O.

<p>As a second example, let’s consider non-blocking I/O. Figure 6.9 shows the Future in action to allow us to use a background thread for I/O.</p>
43
New cards

CompletableFuture

  • The Java Future type is defined as an interface, rather than a concrete class. Any API that wants to use a Future-based style has to supply a concrete implementation of Future.

  • The class is called CompletableFuture—it is a concrete type that implements the Future interface and provides additional functionality and is intended as a simple building block for building asynchronous applications.

  • The central idea is that we can create instances of the CompletableFuture<T> type (it is generic in the type of the value that will be returned), and the object that is created represents the Future in an uncompleted (or “unfulfilled”) state.

  • Later, any thread that has a reference to the completable Future can call complete() on it and provide a value—this completes (or “fulfills”) the future. The completed value is immediately visible to all threads that are blocked on a get() call. After completion, any further calls to complete() are ignored.

  • The CompletableFuture cannot cause different threads to see different values. The Future is either uncompleted or completed, and if it is completed, the value it holds is the value provided by the first thread to call complete().

  • This is obviously not immutability—the state of the CompletableFuture does change over time. However, it changes only once—from uncompleted to completed. There is no possibility of an inconsistent state being seen by different threads.


<ul><li><p>The Java Future type is defined as an interface, rather than a concrete class. Any API that wants to use a Future-based style has to supply a concrete implementation of <em>Future</em>.</p></li><li><p>The class is called CompletableFuture—it is a concrete type that implements the <em>Future</em> interface and provides additional functionality and is intended as a simple building block for building asynchronous applications.</p></li><li><p>The central idea is that we can create instances of the CompletableFuture&lt;T&gt; type (it is generic in the type of the value that will be returned), and the object that is created represents the Future in an uncompleted (or “unfulfilled”) state.</p></li><li><p>Later, any thread that has a reference to the completable Future can call complete() on it and provide a value—this completes (or “fulfills”) the future. The completed value is immediately visible to all threads that are blocked on a get() call. After completion, any further calls to complete() are ignored.</p></li><li><p>The CompletableFuture cannot cause different threads to see different values. The Future is either uncompleted or completed, and if it is completed, the value it holds is the value provided by the first thread to call complete().</p></li><li><p>This is obviously not immutability—the state of the CompletableFuture does change over time. However, it changes only once—from uncompleted to completed. There is no possibility of an inconsistent state being seen by different threads.</p></li></ul><p></p>
44
New cards

One way to think about CompletableFuture is by analogy with client/server

One way to think about CompletableFuture is by analogy with client/server systems. The Future interface provides only a query method— isDone() and a blocking get().

This is playing the role of the client. An instance of CompletableFuture plays the role of the server side—it provides full control over the execution and completion of the code that is fulfilling the future and providing the value.

45
New cards

Modeling tasks

two different ways of modeling tasks: the Callable interface and the FutureTask class. We could also consider Runnable, but it is not always that useful, because the run() method does not return a value, and, therefore, it can perform work only via side effects.

One other aspect of the task modeling is important but may not be obvious— the notion that if we assume that our thread capacity is finite, tasks must definitely complete in bounded time.

Infinite loops can hog pool threads, leading to resource exhaustion and system deadlock unless all tasks are guaranteed to terminate in finite time.

46
New cards

Callable interface

The Callable interface represents a very common abstraction. It represents a piece of code that can be called and returns a result. Despite being a straightforward idea, this is actually a subtle and powerful concept that can lead to some extremely useful patterns.

One typical use of a Callable is the lambda expression (or an anonymous implementation).

The last line of this snippet sets s to be the value of out.toString():

var out = getSampleObject();
Callable<String> cb = () -> out.toString();
String s = cb.call();Callable<String> cb = () -> out.toString();

Think of a Callable as being a deferred invocation of the single method, call(), which the lambda provides.

47
New cards

FutureTask Class - What is a futureTask?

FutureTask acts as a bridge between executing asynchronous work and retrieving its result. It does this by implementing two core Java interfaces at the same time: Runnable and Future.

  • As a Runnable (For the Executor):

    It can be submitted directly to a thread pool or passed into an ExecutorService. The executor doesn't care about the result directly—it just grabs the task and invokes its run() method on a background thread to execute the work.

  • As a Future (For the Client Code / Main Thread):

    Your application code holds onto the FutureTask reference to monitor or retrieve the outcome. You call methods like:

    • get(): Blocks until the task completes and returns the result.

    • cancel(): Attempts to stop the task if it hasn't finished.

    • isDone() / isCancelled(): Queries the current status of the task.


48
New cards

FutureTask class - What are the two convenience constructors for FutureTask are provided?

Two convenience constructors for FutureTask are provided: one that takes a Callable and one that takes a Runnable (which uses Executors.callable() to convert the Runnable to a Callable).

This suggests a flexible approach to tasks, allowing a job to be written as a Callable then wrapped into a FutureTask that can then be scheduled (and cancelled, if necessary) on an executor, due to the Runnable nature of FutureTask.

<p>Two convenience constructors for FutureTask are provided: one that takes a Callable and one that takes a Runnable <mark data-color="green" style="background-color: green; color: inherit;">(which uses Executors.callable() to convert the Runnable to a Callable).</mark></p><p>This suggests a flexible approach to tasks, allowing <mark data-color="green" style="background-color: green; color: inherit;">a job to be written as a Callable then wrapped into a FutureTask that can then be scheduled (and cancelled, if necessary) on an executor,</mark> due to the Runnable nature of FutureTask.</p>
49
New cards

What is Executor and Executors?

A couple of standard interfaces are used to describe the threadpools present in the JDK. The first is Executor, which is very simple and defined like this: (img)

However, the Executor is not widely used—far more common is the ExecutorService interface that extends Executor and adds submit() as well as several life cycle methods, such as shutdown().

To help the developer instantiate and work with some standard threadpools, the JDK provides the Executors class, which is a collection of static helper methods (mostly factories). Four of the most commonly used methods follow:

  • newSingleThreadExecutor()

  • newFixedThreadPool(int nThreads)

  • newCachedThreadPool()

  • newScheduledThreadPool(int corePoolSize)


<p>A couple of standard interfaces are used to describe the threadpools present in the JDK. The first is Executor, which is very simple and defined like this: (img)</p><p>However, the Executor is not widely used—<mark data-color="green" style="background-color: green; color: inherit;">far more common is the ExecutorService interface that extends Executor and adds submit() as well as several life cycle methods, such as shutdown().</mark></p><p>To help the developer instantiate and work with some standard threadpools, the JDK provides the Executors class, which is a collection of static helper methods (mostly factories). Four of the most commonly used methods follow:</p><ul><li><p>newSingleThreadExecutor()</p></li><li><p>newFixedThreadPool(int nThreads)</p></li><li><p>newCachedThreadPool()</p></li><li><p>newScheduledThreadPool(int corePoolSize)</p></li></ul><p></p>
50
New cards

What does a Single-threaded executor do?

  • The simplest of the executors is the single-threaded executor This is essentially an encapsulated combination of a single thread and a task queue (which is a blocking queue).

  • Client code places an executable task onto the queue via submit(). That job submission is nonblocking (unless the job queue is full)

  • The single execution thread then takes the tasks one at a time and runs each to completion before taking the next task. Any tasks that are submitted while the execution thread is busy are queued until the thread is available.

Note: This version of the executor is often useful for testing because it can be made more deterministic that other forms.

<ul><li><p>The simplest of the executors is the single-threaded executor This is essentially an encapsulated combination of a single thread and a task queue (which is a blocking queue).</p></li><li><p>Client code places an executable task onto the queue via submit(). That job submission is nonblocking (unless the job queue is full)</p></li><li><p>The single execution thread then takes the tasks one at a time and runs each to completion before taking the next task. Any tasks that are submitted while the execution thread is busy are queued until the thread is available.</p></li></ul><p><strong><em>Note: </em></strong>This version of the executor is often useful for testing because it can be made more deterministic that other forms.</p>
51
New cards

What does a Fixed-thread pool do?

  • The fixed-thread pool, obtained via one of the variants of Executors.newFixedThreadPool(), is essentially the multiple-thread generalization of the single-threaded executor.

  • At creation time, the user supplies an explicit thread count, and the pool is created with that many threads

  • As with the single-threaded, if all threads are in use, new tasks are stored in a blocking queue until a thread becomes free.


Multiple-threaded pool is particularly useful if task flow is stable and known and if all submitted jobs are roughly the same size, in terms of computation duration.

if the executor threads in it die, they are not replaced. If the possibility exists of the submitted jobs throwing a runtime exception, this can lead to the threadpool starving. But CachedThreadPool solve this issue

52
New cards

What does Cached thread pool do?

  • If the incoming work is more uneven or bursty, a pool with a fixed number of threads is likely to be suboptimal.

  • The CachedThreadPool is an unbounded pool, that will reuse threads if they are available but otherwise will create new threads as required to handle incoming tasks. var pool = Executors.newCachedThreadPool()

  • Threads are kept in the idle cache for 60 seconds, and if they are still present at the end of that period, they will be removed from the cache and destroyed. It is, of course, still very important that the tasks do actually terminate


53
New cards

What does ScheduledThreadPoolExecutor do?

ScheduledThreadPoolExecutor, sometimes referred to as an STPE, as shown here: ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);


The scheduled service extends the usual executor service and adds a small amount of new capabilities: schedule(), which runs a one-off task after a specified delay, and two methods for scheduling periodic (i.e., repeating tasks)—scheduleAtFixedRate() and scheduleWithFixedDelay().

  • The behavior of these two methods is slightly different. scheduleAtFixedRate() will activate a new copy of the task on a fixed timetable (and it will do so whether or not previous copies have completed)

  • whereas scheduleWithFixedDelay() will activate a new copy of the task only after the previous instance has completed and the specified delay has elapsed

Note that the return type, which we’ve explicitly called out here, is ScheduledExecutorService. This is different from the other factory methods, which return ExecutorService.