{"id":1420,"date":"2022-03-01T14:17:59","date_gmt":"2022-03-01T14:17:59","guid":{"rendered":"http:\/\/www.igfasouza.com\/blog\/?p=1420"},"modified":"2022-08-17T10:56:31","modified_gmt":"2022-08-17T09:56:31","slug":"project-loom","status":"publish","type":"post","link":"http:\/\/www.igfasouza.com\/blog\/project-loom\/","title":{"rendered":"Project Loom"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/03\/loom_banner1.png\" alt=\"\" width=\"1600\" height=\"300\" class=\"alignnone size-full wp-image-1421\" srcset=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/03\/loom_banner1.png 1600w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/03\/loom_banner1-300x56.png 300w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/03\/loom_banner1-1024x192.png 1024w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/03\/loom_banner1-768x144.png 768w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/03\/loom_banner1-1536x288.png 1536w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/p>\n<p><b>Story Horse?<\/b><\/p>\n<p>Currently, there is no coroutines implementation in Java, and Project Loom is a proposal implementation. Is not yet clear when this should be added to Java, but there is a big expectation for Java 19.<\/p>\n<p>Check out my blog about <a href=\"http:\/\/www.igfasouza.com\/blog\/coroutines\/\" rel=\"noopener\" target=\"_blank\">Coroutines<\/a> for a better understanding.<\/p>\n<p>Project Loom is an attempt by the OpenJDK community to introduce a lightweight concurrency construct to Java. The prototypes for Loom so far have introduced a change in the JVM as well as the Java library.<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/2J2tJm_iwk0\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p>In the recent prototypes in OpenJDK, a new class named Fiber is introduced to the library alongside the Thread class. Project Loom is a proposal to add Fibers and continuations as a native JVM construct. Fibers are lightweight threads, which can be created in large quantities, without worrying about exhausting system resources. Fibers are going to change how we write concurrent programs in Java.<\/p>\n<p>Fibers is similar to Thread, the user implementation should also remain similar, however, there are two main differences;<\/p>\n<ul>\n<li>\nFiber would wrap any task in an internal user-mode continuation. This would allow the task to suspend and resume in Java runtime instead of the kernel\n<\/li>\n<li>\nA pluggable user-mode scheduler (ForkJoinPool, for example) would be used\n<\/li>\n<\/ul>\n<p>A continuation (or co-routine) is a sequence of instructions that can yield and be resumed by the caller at a later stage. Every continuation has an entry point and a yield point. The yield point is where it was suspended. Whenever the caller resumes the continuation, the control returns to the last yield point.<br \/>\nIt&#8217;s important to realize that this suspend\/resume now occurs in the language runtime instead of the OS. Therefore, it prevents the expensive context switch between kernel threads.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/02\/loom01.jpeg\" alt=\"\" width=\"211\" height=\"238\" class=\"alignnone size-full wp-image-1414\" \/><br \/>\n<small>Picture 1: https:\/\/cr.openjdk.java.net\/~rpressler\/loom\/Loom-Proposal.html<\/small><\/p>\n<p>Currently, Java developers are faced with a choice between writing code that&#8217;s harmonious with the design of the platform but is not scalable, or writing code that makes efficient use of existing hardware with asynchronous programming and fighting the platform.  Project Loom is supposed to break that dilemma through virtual threads.<\/p>\n<p>Virtual threads are cheap enough to have a single thread per task, and eliminate many of the common issues with writing concurrent code in Java.  There aren&#8217;t any new APIs you have to learn, but you do need to unlearn many habits such as using thread pools to deal with resource contention.<\/p>\n<p>Debugging is still challenging and the project Loom team is working with the various IDE vendors on how to present large numbers of threads.<\/p>\n<p>Loom has support for a pluggable scheduler to allow developers to experiment with different scheduling algorithms; the default scheduler uses fork\/join which is based on a work-stealing algorithm.  In the future (post the first GA release), the team may look at adding explicit tail-call optimisation.<\/p>\n<p>In OpenJDK, Java threads are just thin wrappers around OS threads and OS threads are a very precious resource; a modern OS can&#8217;t support more than a few thousand active threads at a time.<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/EO9oMiL1fFo\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/02\/fibers.png\" alt=\"\" width=\"1578\" height=\"1441\" class=\"alignnone size-full wp-image-1415\" srcset=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/02\/fibers.png 1578w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/02\/fibers-300x274.png 300w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/02\/fibers-1024x935.png 1024w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/02\/fibers-768x701.png 768w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2022\/02\/fibers-1536x1403.png 1536w\" sizes=\"auto, (max-width: 1578px) 100vw, 1578px\" \/><br \/>\n<small>Picture 2: Coroutines with Fibers<\/small><\/p>\n<p>You can create several Fibers that will be translated in only fill threads and this will be transparent for the user. In this way, you can have better use of the CPU.<\/p>\n<p>Project Loom allows us to write highly scalable code with one lightweight thread per task, this simplifies development, as you do not need to use reactive programming to write scalable code. Another benefit is that lots of legacy code can use this optimization without much change in the codebase. The improvements that Project Loom brings are exciting and allow Java programmers to write scale applications without reactive programming.<\/p>\n<p>These are the two main JEP\u2019s related to Project Loom<\/p>\n<ul>\n<li>JEP 425: Virtual Threads<\/li>\n<li>JEP 428: Structured Concurrency<\/li>\n<\/ul>\n<p>Stay tuned, because here I can explore more about Coroutines and Project Loom with Apache Kafka and Coroutines in the Raspberry Pi world.<\/p>\n<h2>Links<\/h2>\n<p><a href=\"https:\/\/jdk.java.net\/loom\/\" rel=\"noopener\" target=\"_blank\">https:\/\/jdk.java.net\/loom\/<\/a><\/p>\n<p><a href=\"https:\/\/openjdk.java.net\/projects\/loom\/\" rel=\"noopener\" target=\"_blank\">https:\/\/openjdk.java.net\/projects\/loom\/<\/a><\/p>\n<p><a href=\"https:\/\/blogs.oracle.com\/javamagazine\/post\/going-inside-javas-project-loom-and-virtual-threads\" rel=\"noopener\" target=\"_blank\">https:\/\/blogs.oracle.com\/javamagazine\/post\/going-inside-javas-project-loom-and-virtual-threads<\/a><\/p>\n<p><a href=\"https:\/\/nipafx.dev\/inside-java-newscast-17\/\" rel=\"noopener\" target=\"_blank\">https:\/\/nipafx.dev\/inside-java-newscast-17\/<\/a><\/p>\n<p><a href=\"https:\/\/developerlife.com\/2019\/12\/02\/project-loom-experiment\/\" rel=\"noopener\" target=\"_blank\">https:\/\/developerlife.com\/2019\/12\/02\/project-loom-experiment\/<\/a><\/p>\n<p><a href=\"https:\/\/www.javaadvent.com\/2020\/12\/project-loom-and-structured-concurrency.html\" rel=\"noopener\" target=\"_blank\">https:\/\/www.javaadvent.com\/2020\/12\/project-loom-and-structured-concurrency.html<\/a><\/p>\n<p><a href=\"https:\/\/openjdk.org\/jeps\/425\" rel=\"noopener\" target=\"_blank\">https:\/\/openjdk.org\/jeps\/425<\/a><\/p>\n<p><a href=\"https:\/\/openjdk.org\/jeps\/428\" rel=\"noopener\" target=\"_blank\">https:\/\/openjdk.org\/jeps\/428<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Story Horse? Currently, there is no coroutines implementation in Java, and Project Loom is a proposal implementation. Is not yet clear when this should be added to Java, but there is a big expectation for Java 19. Check out my&hellip; <a href=\"http:\/\/www.igfasouza.com\/blog\/project-loom\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1414,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[17],"class_list":["post-1420","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java"],"_links":{"self":[{"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/posts\/1420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/comments?post=1420"}],"version-history":[{"count":6,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/posts\/1420\/revisions"}],"predecessor-version":[{"id":1436,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/posts\/1420\/revisions\/1436"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/media\/1414"}],"wp:attachment":[{"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/media?parent=1420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/categories?post=1420"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/tags?post=1420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}