<?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 Arunkumar Srambikkal on Medium]]></title>
        <description><![CDATA[Stories by Arunkumar Srambikkal on Medium]]></description>
        <link>https://medium.com/@computinglife?source=rss-76ef797539c3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*i6gS5Hiap2XzMP4L.png</url>
            <title>Stories by Arunkumar Srambikkal on Medium</title>
            <link>https://medium.com/@computinglife?source=rss-76ef797539c3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 04:26:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@computinglife/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[Leaky Abstractions — Fibers in Java & some learning on AI code capabilities]]></title>
            <link>https://medium.com/@computinglife/leaky-abstractions-fibers-in-java-some-learning-on-ai-code-capabilities-32090cd603c6?source=rss-76ef797539c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/32090cd603c6</guid>
            <category><![CDATA[java-virtual-threads]]></category>
            <category><![CDATA[fiber]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[messaging]]></category>
            <category><![CDATA[ai-code-review]]></category>
            <dc:creator><![CDATA[Arunkumar Srambikkal]]></dc:creator>
            <pubDate>Wed, 18 Jun 2025 09:50:59 GMT</pubDate>
            <atom:updated>2025-06-23T14:21:50.306Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaky Abstractions — Fibers in Java &amp; some learning on AI code capabilities</h3><p>Fibers have always been up there in concurrency related discussions, even from Windows NT time period. I think they got into my psyche after reading that SQL Server supported it as a performance option in scenarios where context switching kills the DB performance. Since i read about this a long time back, my untrained mind has always retained it as a cool capability to be used when threads are not enough.</p><p>Due to way its presented, it is easy to think of Fibers as a performance panacea &amp; typically discussions around processes, threads and Fibers go along these lines -&gt;</p><blockquote>Processes are independent units of execution with their own memory spaces, while threads are lightweight units of execution that share the memory space of their parent process. Threads are generally faster and more efficient for tasks that require frequent communication and data sharing</blockquote><blockquote>Fibers are lightweight threads designed to further enhance concurrency. Unlike traditional threads, which are managed by the operating system and have a significant context-switching overhead, fibers are managed by the JVM and are designed to have much lower overhead.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jkplJyCg6IVrnIEr_g5tFg.png" /></figure><p>Obviously, whats not to like about a concurrency feature which is lighter than thread ? It seems to be a natural evolution from processes to threads to Fibers in search of better concurrency in parallel systems.</p><p>This therefore became a strong design contender for me, when i decided to create a M*N message passing harness, to compare the performance of Java to compare it with Golang / Rust.</p><p><em>Context </em>: The M*N test is popular test to compare the message passing capability of a platform.<a href="https://en.wikipedia.org/wiki/Joe_Armstrong_(programmer)"> Joe Armstrong</a>’s book on Erlang: Software For a Concurrent World, has this exercise to write a benchmark that creates N processes in a ring and sends a message around the ring M times so that a total of N*M messages get sent</p><p>Obviously when the number of processes (Erlang speak for threads / actors) gets big 1 or 2 million in size, traditional threads cannot be used at all. Fibers therefore seemed the best option to test the new tool which has forever been in the making aka Project Loom aka Virtual Threads.</p><h3>The Initial Design</h3><p>In keeping with these definitions above, I imagined the new Virtual User threads aka Fibers in Java as a long lived task like a physical thread but just much more lighter, enough to run almost a million of them. I imagined them equivalent to creating actors in Akka. I was hoping the system would switch between these tasks, scheduling the correct one as and when they are ready, without the program having to do much of thread management</p><p>So this was the first quick design i came up with just so that i could Virtual Threads :).</p><ol><li>Create a virtual thread for each process</li><li>Have all of them wait (object.wait) for being notified about a new message</li><li>The task which gets the message passed it to the next task</li><li>This is done by waking it up from its sleep using a notify call (object.notify).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*dheAzXsta2Ii5CAfFRO1wg.png" /></figure><h3>The problems I faced</h3><p>When i ran this, it worked perfectly fine — up to a count of 256 Virtual Threads. When i went above 256, the message passing failed and it looked like tasks just hung randomly.</p><p>The number 256 was obviously a give away that some limits where being hit.</p><h3>The Findings</h3><p>After some debugging, what i found is that Virtual threads are run by the default <a href="https://www.baeldung.com/java-fork-join">ForkJoinPool </a>in Java using a limited underlying thread pool it has — 256 of them by default. So if the Virtual Thread task blocks, it blocks the Carrier Thread (the platform thread which hosts the Virtual Thread) and eventually ends up blocking the entire Virtual Thread functionality and no new tasks could run.</p><p>Naturally i assumed this abstraction was deeply flawed. I mean, what is the point of a task that cannot block. We could then all go back to async callbacks like in Javascript.</p><p>But Oracle’s documentation about Virtual thread says this —</p><blockquote>“However, when code running in a virtual thread calls a blocking I/O operation, the Java runtime suspends the virtual thread until it can be resumed.”</blockquote><p>I’m not sure why this did not apply to my code, which was having threads waiting on object.wait requests but maybe it applies only to IO calls.Therefore i decided to investigate further.</p><p>The Virtual Threads functionality did not report any call stack from the jstack command in spite of passing the required flags (Djdk.trackAllThreads=true) and it did not report any Java Flight Recorder events either. Good old google search and some more readings finally gave clues to the problem.</p><h3>The Fix</h3><p>It turns out that a Virtual Thread that performs a blocking operation while in a synchronized block, it will block the underlying platform thread (aka Carrier Thread) which is hosting the Virtual Thread, from being used for any other task / callable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_BHaGuFyQtV1mPm_B1t71A.png" /></figure><p>This scenario quickly ends up eating up the native thread pool which is backing the ForkJoinPool used to run the Virtual Thread functionality.</p><blockquote>You can get around this by increasing the native parallelism available in the JVM using this flag -Djdk.virtualThreadScheduler.parallelism=1024</blockquote><p>So I fixed the issue by removing synchronized blocks and using re-entrant locks and condition API from the java concurrency library instead of the base wait/notify API which requires the use of synchronized blocks. Its funny why that should work but it does.</p><p>Once i knew what was happening i was able to quickly find <a href="https://blog.ydb.tech/how-we-switched-to-java-21-virtual-threads-and-got-deadlock-in-tpc-c-for-postgresql-cca2fe08d70b">many others </a>who had hit the same issue.</p><h4>Optimization Attempts</h4><p>The message passing does not truly require concurrent execution since its always a single task which is taking a message and then sending it to another. So i sought to optimize this by simply looping on an array of Runnables (with state) and creating a Future every time i needed it to run (to simulate a task).</p><p>Surprisingly this turned out to be 50% slower than the Virtual Threads based design. However this design used much less memory (180 MB vs 1Gig).</p><h3>The Learning</h3><blockquote>As always its best to <strong>completely </strong>read about a new functionality before starting to type it out and spending half a day to debug it and complaining about it in a blog on Medium. But apparently this behavior is common in our Industry</blockquote><p>Things to keep in mind when using Virtual Threads -</p><ol><li>Virtual Threads scale much better than using regular threads &amp; possibly Futures too.</li><li>Do not use synchronize calls within Virtual Threads.</li><li>Do not use <a href="https://www.geeksforgeeks.org/java/native-keyword-java/">native methods</a> or <a href="https://www.geeksforgeeks.org/java/foreign-function-and-memory-api-in-java/">foreign functions</a> within Virtual Threads. (learned from others who got into similar situations of pinning)</li><li>The total number of Virtual Threads that exist at time cannot be too high as it will consume an insignificant amount of memory, though its minuscule compare to a platform thread. It roughly requires 5 KB for a single thread.</li></ol><p>These restrictions does severely hamper what you can do with this feature as most non-trivial EXISTING functionality is sure to perform either of the two. Maybe newer code can be written to avoid either of these but not calling native methods will be hard.</p><blockquote>However it does seem faster than using Futures when there are too many of them and it is definitely better than trying to create a ton of platform threads for sure which would have been impossible.</blockquote><h3>The Leaky Abstraction</h3><p>Why do i call this a Leaky Abstraction ? Read<a href="https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/"> this excellent article</a> from <a href="https://en.wikipedia.org/wiki/Joel_Spolsky">Joel Spolsky</a> to know more. The summary is that concepts should not have any underlying gotchas aka leaks. These leaks are what makes it hard to use new abstractions.</p><h3>The extra — How well did AI perform for this problem?</h3><p>Obviously, with all the AI Agent hype going around, i was eager to see what the vaunted capabilities of AI can achieve while i was in the process of debugging this issue. When i submitted my original code which would stop working at 256 tasks, to Grok and CoPilot, these systems were giving me good tips on how to make my code better. But these were tactical in nature and restricted to local patterns about possible issues in initialization or synchronizations (none of which was a real problem, but was good practice to enhance readability). The suggestions were all along the lines of code improvements for maintainability / readability and possible issues with my test harness. Some were also wild guesses about possible issues.</p><p>However these AI systems did not have the correct mental model of what was going on underneath.</p><p>For now therefore my impression about the demise of programming at the hands of AI is that its not going to pan to the extend people expect with its current capabilities.</p><p>Of-course i can imagine how it can reduce the physical task of typing in a lot of code. That is a welcome help of-course and it could help reduce a lot of manual labor especially when writing a lot of new code. But large code bases are going to be a different problem altogether and that is where human cognitive abilities will continue to outshine AI</p><h4>To be updated later</h4><blockquote>ps : In case you are wondering, my starting test harness with each task waking up and going back to sleep for sending a message, running 200000 tasks sending a message across them 30 times, takes roughly 60–70 secs to complete on a 12th gen Intel Core-i7 laptop with 14 cores.</blockquote><blockquote>The same tests takes 1.7 seconds with Golang.</blockquote><p>Can we better the design / how does it compare with go / rust and erlang in this test.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32090cd603c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>