<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SRIMATHI K R</title>
    <description>The latest articles on DEV Community by SRIMATHI K R (@srimathi_kr_b35d06a65924).</description>
    <link>https://dev.to/srimathi_kr_b35d06a65924</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4007434%2Fd4d76d42-6338-422b-8160-ff1e0b534f50.png</url>
      <title>DEV Community: SRIMATHI K R</title>
      <link>https://dev.to/srimathi_kr_b35d06a65924</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/srimathi_kr_b35d06a65924"/>
    <language>en</language>
    <item>
      <title>Understanding Semaphores in Java – A Beginner's Guide</title>
      <dc:creator>SRIMATHI K R</dc:creator>
      <pubDate>Tue, 30 Jun 2026 07:26:55 +0000</pubDate>
      <link>https://dev.to/srimathi_kr_b35d06a65924/understanding-semaphores-in-java-a-beginners-guide-57ae</link>
      <guid>https://dev.to/srimathi_kr_b35d06a65924/understanding-semaphores-in-java-a-beginners-guide-57ae</guid>
      <description>&lt;p&gt;Imagine you and your friends are waiting to use a washing machine.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there is only &lt;strong&gt;one washing machine&lt;/strong&gt;, only &lt;strong&gt;one person&lt;/strong&gt; can use it at a time.&lt;/li&gt;
&lt;li&gt;If there are &lt;strong&gt;five washing machines&lt;/strong&gt;, then &lt;strong&gt;five people&lt;/strong&gt; can use them simultaneously.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is exactly what a &lt;strong&gt;Semaphore&lt;/strong&gt; does in programming.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Semaphore&lt;/strong&gt; is a synchronization mechanism that limits the number of threads that can access a shared resource simultaneously.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Concepts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;Semaphore&lt;/strong&gt; uses &lt;strong&gt;permits&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;acquire()&lt;/code&gt; takes a permit. If no permits are available, the thread waits.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;release()&lt;/code&gt; returns a permit back to the semaphore.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Semaphore(5)&lt;/code&gt; allows up to &lt;strong&gt;5 concurrent threads&lt;/strong&gt; to access a shared resource.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Producer-Consumer
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Producer-Consumer Problem&lt;/strong&gt; is one of the most common use cases of semaphores.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;Producer&lt;/strong&gt; creates items and places them into a shared buffer.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;Consumer&lt;/strong&gt; removes items from the shared buffer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Semaphores coordinate access so that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The producer &lt;strong&gt;does not overfill&lt;/strong&gt; the buffer.&lt;/li&gt;
&lt;li&gt;The consumer &lt;strong&gt;does not consume&lt;/strong&gt; from an empty buffer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This ensures proper synchronization between producer and consumer threads.&lt;/p&gt;




&lt;h1&gt;
  
  
  Mutex vs Semaphore
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Mutex
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Mutex&lt;/strong&gt; allows &lt;strong&gt;only one thread&lt;/strong&gt; to access the critical section at a time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Characteristics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Only &lt;strong&gt;ONE thread&lt;/strong&gt; can enter the critical section.&lt;/li&gt;
&lt;li&gt;Used for &lt;strong&gt;mutual exclusion&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Prevents race conditions by ensuring exclusive access to shared resources.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Semaphore
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Semaphore&lt;/strong&gt; allows &lt;strong&gt;multiple threads&lt;/strong&gt; to access a shared resource based on the number of permits available.&lt;/p&gt;

&lt;h3&gt;
  
  
  Characteristics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Allows multiple threads simultaneously.&lt;/li&gt;
&lt;li&gt;The number of concurrent threads depends on the number of permits.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;Semaphore(5)&lt;/code&gt; allows up to &lt;strong&gt;5 threads&lt;/strong&gt; to access the resource at the same time.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Java Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.Semaphore&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SemaphoreExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;Semaphore&lt;/span&gt; &lt;span class="n"&gt;semaphore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Semaphore&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;semaphore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;acquire&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Critical section&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread is accessing the shared resource."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;semaphore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;release&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Use a &lt;strong&gt;Mutex&lt;/strong&gt; when &lt;strong&gt;only one thread&lt;/strong&gt; should access a shared resource at a time.&lt;/li&gt;
&lt;li&gt;Use a &lt;strong&gt;Semaphore&lt;/strong&gt; when a &lt;strong&gt;limited number of threads&lt;/strong&gt; can access the resource simultaneously.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common real-world use cases include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database connection pools&lt;/li&gt;
&lt;li&gt;Printer management&lt;/li&gt;
&lt;li&gt;Thread pools&lt;/li&gt;
&lt;li&gt;API rate limiting&lt;/li&gt;
&lt;li&gt;Parking lot simulations&lt;/li&gt;
&lt;li&gt;Producer-Consumer systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Semaphores help efficiently manage concurrent access to shared resources while preventing conflicts and improving application performance.&lt;/p&gt;

</description>
      <category>java</category>
      <category>semaphores</category>
      <category>beginners</category>
      <category>todayilearned</category>
    </item>
  </channel>
</rss>
