<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Deepnikode on Medium]]></title>
        <description><![CDATA[Stories by Deepnikode on Medium]]></description>
        <link>https://medium.com/@deepnikode?source=rss-2ef931b5d07f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*e6SDlzynxQyjFNtY</url>
            <title>Stories by Deepnikode on Medium</title>
            <link>https://medium.com/@deepnikode?source=rss-2ef931b5d07f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 13:53:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@deepnikode/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[☕ Java Multithreading for Dummies Like Me…]]></title>
            <link>https://medium.com/@deepnikode/java-multithreading-for-dummies-like-me-b6fd9988c879?source=rss-2ef931b5d07f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6fd9988c879</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[completablefuture]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Deepnikode]]></dc:creator>
            <pubDate>Sat, 19 Jul 2025 05:38:04 GMT</pubDate>
            <atom:updated>2025-07-19T05:38:04.729Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been coding in Java for the past few years, and to be honest — <strong>multithreading initially felt like my brain trying to solve a Rubik’s cube. Bhot hard bhai!</strong> 😵‍💫</p><p>But then I slowly figured out a modern way to handle async code that doesn’t fry your brain: <strong>CompletableFuture</strong>.</p><p>So here’s my no-nonsense, no-theory-heavy take on how Java multithreading works, and how to use CompletableFuture without becoming a monk.</p><h3>🚀 The Basic Idea of Multithreading</h3><p>Let’s say your app is a kitchen, Each <strong>thread</strong> is like a <strong>chef</strong>. If you’ve got just one thread (chef), you have to wait for him to:</p><ul><li>boil water</li><li>cut veggies</li><li>bake cake</li></ul><p>All <strong>one after another</strong>.</p><p>With <strong>multiple threads</strong>, each chef can take care of one task <strong>in parallel</strong>. Now your app (kitchen) is faster and smoother.</p><p>But… managing multiple chefs is a nightmare. So Java gave us tools like Future and later CompletableFuture to make it simpler.</p><h3>🕰️ Future - A Lazy Promise</h3><p>Future is like ordering food from Zomato and waiting.</p><p>You ask for biryani (submit() a task), and get a Future.</p><p>But you have to keep checking:<br> “Is it done yet? Hua kya?” (by calling future.isDone())</p><p>And to actually eat, you call future.get()—which <strong>blocks your thread like a clingy friend</strong>.</p><p>🔴 <strong>Problem</strong>: You still wait, and it’s not truly async.</p><h3>⚡ CompletableFuture - The Cool Cousin</h3><p>Fadu entry of<strong> </strong><strong>CompletableFuture</strong>, which says:</p><p><strong><em>“Hey, I’ll do the thing in the background, and when I’m done, I’ll call you. You chill and relaxxx”</em></strong></p><p>📚 <strong>Important theory</strong>:<br> It’s an enhancement to the Future concept, introduced in <strong>Java 8</strong>.<br> It allows you to:</p><ul><li>create more complex asynchronous workflows</li><li>chain multiple actions together</li><li>handle exceptions more gracefully</li></ul><p>It gives us a clean and flexible way to write <strong>non-blocking</strong>, <strong>asynchronous</strong> code in Java.</p><p>Let’s break down the <strong>most useful methods</strong> with <em>lame but fun</em> examples.</p><h3>✅ supplyAsync() – Do Work and Expect Something in Return</h3><p>This is like assigning a task to your junior:<br><em> </em><strong><em>“Hey, make a pizza and let me know when it’s ready!”</em></strong><em> 🍕</em></p><blockquote>CompletableFuture&lt;String&gt; future = CompletableFuture.supplyAsync(() -&gt; {<br> return “Pizza is ready!”;<br>});</blockquote><p>💡<strong> </strong>Use when you want to run a task in the background, <strong>and get result back</strong>.</p><h3>🏃 runAsync() – Fire and Forget (No Return)</h3><p>You tell your friend:<br> <strong>“Bro, just switch on a Mc Stan song, I don’t need a report back. I’ll just vibe…”</strong> 🎧</p><blockquote>CompletableFuture&lt;Void&gt; future = CompletableFuture.runAsync(() -&gt; {<br> System.out.println(“🎶 Basti Ka Hasti is playing in the background…”);<br>});</blockquote><p>💡 Use when you want to run something async, <strong>and don’t care about the result</strong>.</p><h3>🔗 thenApply() – Transform the Result (Same Thread)</h3><p>You’re reading a file from disk (in a background thread), and once you’re done, you want to <strong>convert the content to uppercase</strong>:</p><blockquote>CompletableFuture&lt;String&gt; future = CompletableFuture<br> .supplyAsync(() -&gt; {<br> System.out.println(“Reading file: “ + Thread.currentThread().getName());<br> return “hello world”;<br> })<br> .thenApply(content -&gt; {<br> System.out.println(“Uppercasing: “ + Thread.currentThread().getName());<br> return content.toUpperCase();<br> });</blockquote><p>📌 <strong>Note</strong>: thenApply() doesn’t switch threads.<br> If your file reading finishes on Thread-1, the transformation also runs on Thread-1.</p><p>✅ <strong>Use for</strong>:</p><ul><li><strong>Lightweight</strong> operations like simple mapping or formatting.</li><li>When you want to <strong>modify the result</strong> without switching threads.</li></ul><h3>🔄 thenApplyAsync() – <strong>Transform the Result in a New Thread</strong></h3><p>Let’s say you fetched a username from a database. Now you want to <strong>capitalize it</strong> — but on a <strong>different thread</strong>, so you don’t block the original one.</p><blockquote>CompletableFuture&lt;String&gt; future = CompletableFuture<br> .supplyAsync(() -&gt; {<br> System.out.println(“Fetching username: “ + Thread.currentThread().getName());<br> return “john_doe”;<br> })<br> .thenApplyAsync(username -&gt; {<br> System.out.println(“Capitalizing: “ + Thread.currentThread().getName());<br> return username.toUpperCase();<br> });</blockquote><p>📌 <strong>Note</strong>: thenApplyAsync() runs the transformation in a <strong>new thread</strong>, usually from the <strong>ForkJoinPool.commonPool</strong>.</p><p>✅ <strong>Use for</strong>:</p><ul><li><strong>Heavy</strong> transformations (e.g., parsing, DB calls).</li><li>When you want to <strong>offload work</strong> to a new thread.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6fd9988c879" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>