1. Mutex Lock
The following code illustrates the crucial role of a mutex in synchronizing access to a shared resource.
In this demonstration, we use two threads to increment the value of a counter. In one scenario, a mutex is employed to securely read and update the counter's value. In contrast, the second scenario omits the mutex, performing the same operations.
We introduce a sleep function to amplify the issue, creating a deliberate delay between each thread's reading and updating actions.
Observe that the final value of the counter is inaccurate when incremented without a mutex. Ideally, a counter incremented 100 times by 2 threads should result in a total of 200.
public class Solution {
private static int counter = 0;
private static final Object lock = new Object();
public static void runExperiment(String experimentName, Runnable task) {
counter = 0;
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"Final counter value " + experimentName + ": " + counter + "\n"
);
}
public static void incrementCounterWithMutex() {
for (int i = 0; i < 100; i++) {
synchronized (lock) {
int temp = counter;
try {
Thread.sleep(1); // Sleep for 1 millisecond
} catch (InterruptedException e) {
e.printStackTrace();
}
counter = temp + 1;
}
}
}
public static void incrementCounterNoMutex() {
for (int i = 0; i < 100; i++) {
int temp = counter;
try {
Thread.sleep(1); // Sleep for 1 millisecond
} catch (InterruptedException e) {
e.printStackTrace();
}
counter = temp + 1;
}
}
public static void main(String[] args) {
runExperiment("With Mutex Experiment", Solution::incrementCounterWithMutex);
runExperiment("No Mutex Experiment", Solution::incrementCounterNoMutex);
}
}
🤖 Don't fully get this? Learn it with Claude
Stuck on 1. Mutex Lock? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **1. Mutex Lock** (Concurrency) and want to truly understand it. Explain 1. Mutex Lock from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **1. Mutex Lock** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **1. Mutex Lock** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **1. Mutex Lock** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.