<?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 Fatima ELOUAFI on Medium]]></title>
        <description><![CDATA[Stories by Fatima ELOUAFI on Medium]]></description>
        <link>https://medium.com/@elouafifatima759?source=rss-2b0ce7e6fd36------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Fatima ELOUAFI on Medium</title>
            <link>https://medium.com/@elouafifatima759?source=rss-2b0ce7e6fd36------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 17:39:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@elouafifatima759/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[Exploring Java Virtual Threads: The Future of Concurrent Programming.]]></title>
            <link>https://elouafifatima759.medium.com/exploring-java-virtual-threads-the-future-of-concurrent-programming-488cc039c2ad?source=rss-2b0ce7e6fd36------2</link>
            <guid isPermaLink="false">https://medium.com/p/488cc039c2ad</guid>
            <category><![CDATA[concurrent-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[jdk21]]></category>
            <category><![CDATA[threads]]></category>
            <dc:creator><![CDATA[Fatima ELOUAFI]]></dc:creator>
            <pubDate>Sun, 18 Feb 2024 20:37:23 GMT</pubDate>
            <atom:updated>2024-02-18T20:52:25.972Z</atom:updated>
            <content:encoded><![CDATA[<p>Java continues to evolve, introducing enhancements that shape the future of software development. Among the most far-reaching updates was the introduction of <strong>Virtual Threads</strong> new feature of the JDK 21, a milestone achieved through Project Loom. Now, officially integrated into the Java Virtual Machine, virtual threads represent a paradigm shift in C<strong>oncurrent Programming.</strong></p><p>Virtual threads, born out of the ambitious Project Loom initiative (A project of OpenJDK community ) are designed to simplify concurrent programming and to be inexpensive to create and use in terms of operating system resources usage, making it practical to have a large number of them in one application.</p><p>In this article, we will dive into what virtual threads are, the difference between Virtual Threads and Traditional Threads (Platform Threads), why they matter, and Finally, we’ll explore how to implement virtual threads in Java .</p><h3>Platform Threads:</h3><p><strong>A platform thread is a wrapper around an OS kernel thread and each Java platform thread is associated 1-to-1 with an OS kernel thread .<br>Platform thread executes Java code on its associated OS thread and captures it for its entire lifetime.<br>Consequently, the number of available platform threads is limited to the number of OS threads.<br>Platforms threads are suitable for executing all types of tasks However, they are a limited resource due to large runtime stack size maintained by the operating system and limited number of OS threads.</strong></p><h3>Virtual Threads:</h3><p><strong>Virtual threads are conceptual threads that are managed by the Java Virtual Machine rather than by the operating system.<br>Virtual threads are also an instance of<em> java.lang.Thread</em> but unlike the platform thread the virtual thread isn’t tied to a specific OS thread.</strong></p><p><strong>What makes Virtual threads outstanding is when they run a blocking I/O operation code , the Java runtime suspends the virtual thread until it can be resumed so The OS thread associated with the suspended virtual thread(virtual threads still run code on OS threads) becomes free to perform other operations.<br>To sum up: Virtual threads are suitable for running tasks that spend most of the time blocked.</strong></p><h3>How virtual threads work?</h3><p>Virtual threads introduce an abstraction layer between operating-system processes and application-level concurrency. Said differently, virtual threads are used to schedule tasks that the Java virtual machine orchestrates, so the JVM mediates between the operating system and the Application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4fL9UOJO0ZgQ3MUaQ8jZzg.png" /><figcaption>The architecture of virtual threads in Java</figcaption></figure><p><strong>In this architecture, the application instantiates virtual threads and the JVM assigns the compute resources to handle them. Contrast this to conventional threads, which are mapped directly onto operating system processes.</strong></p><h3>Why use Virtual Threads:</h3><ul><li>They are lightweight and consume fewer system resources compared to Platform threads.</li><li>A very large number of virtual threads can be created because they are not limited by the number of OS thread.</li><li>Provide better performance when running concurrent tasks that spend much of their time blocked.</li><li>The JVM can supports millions of virtual threads.</li></ul><h3>Creating and running virtual threads:</h3><ol><li><strong>Creating a Virtual Thread with the Thread Class:</strong></li></ol><p>In this example we create and start a virtual thread that prints a message using the <em>ofVirtual()</em> method of the <em>Thread</em> class.</p><p>It’s important to call the <em>join()</em> method to wait for the virtual thread to terminate so we can see the printed message.</p><pre>Thread thread = Thread.ofVirtual().start(() -&gt; System.out.println(&quot;I am a virtual thread&quot;));<br>thread.join();</pre><p><strong>2. Creating a Virtual Thread with the Thread.Builder Interface:</strong></p><p>The <em>Thread.Builder</em> interface let us create threads with common Thread properties such as the thread’s name.</p><pre>Thread.Builder builder = Thread.ofVirtual().name(&quot;VirtualThread&quot;);<br>Runnable task = () -&gt; {<br>    System.out.println(&quot;I am a virtual thread&quot;);<br>};<br>Thread thread = builder.start(task);<br>System.out.println(&quot;Thread name: &quot; + thread.getName());<br>thread.join();<br><br>/*Output:<br>I am a virtual thread<br>Thread name: VirtualThread<br>*/<br></pre><h3>Conclusion:</h3><p>In conclusion, Java 21’s virtual threads mark a significant leap forward in the evolution of concurrent programming.</p><p>But it’s essential to recognize that virtual threads don’t offer faster execution speeds compared to platform threads their true value lies in enhancing scalability of high-throughput concurrent applications especially those that handle a great number of concurrent tasks that spend much of their time waiting.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=488cc039c2ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>