<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2026-07-26T09:27:34+00:00</updated><id>/feed.xml</id><title type="html">My Backend Blog</title><subtitle>Notes on distributed systems, JVM internals, and building things that scale.</subtitle><author><name>Your Name</name></author><entry><title type="html">Introduction to Mutual Exclusion in Java</title><link href="/2026/07/14/mastering-mutual-exclusion-in-java/" rel="alternate" type="text/html" title="Introduction to Mutual Exclusion in Java" /><published>2026-07-14T00:00:00+00:00</published><updated>2026-07-14T00:00:00+00:00</updated><id>/2026/07/14/mastering-mutual-exclusion-in-java</id><content type="html" xml:base="/2026/07/14/mastering-mutual-exclusion-in-java/"><![CDATA[<p>Every multithreaded program is eventually going to hit the problem of two concurrent threads trying to read, write 
or modify an object stored in a shared memory.</p>

<p>Java offers many tools to deal with this problem, one better than the other, each coming with its own set of pros
and cons.</p>

<p>This article is going to introduce the problem, give an overview of the available solutions (always with examples!)
and introduce you, my dear reader, to the world of concurrent programming in Java.</p>

<h2 id="understanding-the-problem">Understanding the problem</h2>

<p>Let’s analyze the code below - a simple counter with two methods <code class="language-plaintext highlighter-rouge">increment()</code> and <code class="language-plaintext highlighter-rouge">get()</code>. When there is only one thread
, everything runs in the order it appears in the code. Each instruction is executed only after the previous one has finished,
the developer has full control over the order of execution.</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Counter</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>

    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">increment</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">count</span><span class="o">++;</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kt">int</span> <span class="nf">get</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">count</span><span class="o">;</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>

<p><img src="/assets/images/2-threads-update-same-count.png" alt="Alt text describing the image" /></p>]]></content><author><name>Your Name</name></author><category term="java" /><category term="concurrency" /><category term="concurrency" /><category term="synchronized" /><category term="locks" /><summary type="html"><![CDATA[A guided tour of every mutual exclusion mechanism in Java - what problem each one solves and when to reach for it. First post in a deep-dive series.]]></summary></entry></feed>