{"id":3380,"date":"2025-12-24T10:01:11","date_gmt":"2025-12-24T15:01:11","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3380"},"modified":"2025-12-24T10:01:11","modified_gmt":"2025-12-24T15:01:11","slug":"synchronous-to-asynchronous","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java\/synchronous-to-asynchronous\/","title":{"rendered":"Synchronous to Asynchronous"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Converting a synchronous method to an asynchronous one in Java involves modifying the code to allow other tasks to execute while it is waiting for input\/output operations to complete. Here&#8217;s an example of how to convert a synchronous method to an asynchronous one in Java:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say we have a synchronous method that performs a CPU-intensive operation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public int calculate(int x, int y) {\n    int result = 0;\n    for (int i = 0; i &lt; x; i++) {\n        for (int j = 0; j &lt; y; j++) {\n            result += i * j;\n        }\n    }\n    return result;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To convert this to an asynchronous method, we can make use of the <code>CompletableFuture<\/code> class in Java. First, we need to change the return type from <code>int<\/code> to <code>CompletableFuture&lt;Integer&gt;<\/code>. We also need to mark the method with the <code>Async<\/code> annotation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.concurrent.CompletableFuture;\nimport java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\n\nimport org.springframework.scheduling.annotation.Async;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class CalculationService {\n\n    private ExecutorService executor = Executors.newFixedThreadPool(10);\n\n    @Async\n    public CompletableFuture&lt;Integer&gt; calculateAsync(int x, int y) {\n        CompletableFuture&lt;Integer&gt; completableFuture = new CompletableFuture&lt;&gt;();\n        executor.submit(() -&gt; {\n            int result = 0;\n            for (int i = 0; i &lt; x; i++) {\n                for (int j = 0; j &lt; y; j++) {\n                    result += i * j;\n                }\n            }\n            completableFuture.complete(result);\n        });\n        return completableFuture;\n    }\n}\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we have used the <code>@Async<\/code> annotation provided by Spring Framework to mark the method as asynchronous. We have also created a <code>CompletableFuture<\/code> object to hold the result of the calculation. The calculation is performed inside a separate thread using an <code>ExecutorService<\/code>, and the result is set in the <code>CompletableFuture<\/code> object when it becomes available.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By returning a <code>CompletableFuture<\/code> object, other tasks can continue executing while the calculation is being performed in the background. The caller can then use the <code>get()<\/code> method on the <code>CompletableFuture<\/code> object to wait for the result of the calculation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converting a synchronous method to an asynchronous one in Java involves modifying the code to allow other tasks to execute while it is waiting for input\/output operations to complete. Here&#8217;s an example of how to convert a synchronous method to an asynchronous one in Java: Let&#8217;s say we have a synchronous method that performs a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3382,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[280],"tags":[],"series":[],"class_list":["post-3380","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/multitasking-ga6749a2b2_640.jpg?fit=640%2C394&ssl=1","jetpack-related-posts":[{"id":3576,"url":"https:\/\/www.mymiller.name\/wordpress\/springboot\/asynchronous-vs-synchronous-logging-in-spring-boot-a-deep-dive-with-examples-and-lombok-configuration\/","url_meta":{"origin":3380,"position":0},"title":"Asynchronous vs. Synchronous Logging in Spring Boot: A Deep Dive with Examples and Lombok Configuration","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Logging is essential for application monitoring and debugging, and Spring Boot offers both asynchronous and synchronous logging options. Let\u2019s explore these approaches, demonstrate their usage with examples, and streamline configuration using Lombok. Synchronous Logging: How it Works: Logging events are directly written to the destination (e.g., file, console) by the\u2026","rel":"","context":"In &quot;Springboot&quot;","block_context":{"text":"Springboot","link":"https:\/\/www.mymiller.name\/wordpress\/category\/springboot\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_vap919vap919vap9.jpg?fit=1200%2C1200&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_vap919vap919vap9.jpg?fit=1200%2C1200&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_vap919vap919vap9.jpg?fit=1200%2C1200&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_vap919vap919vap9.jpg?fit=1200%2C1200&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_vap919vap919vap9.jpg?fit=1200%2C1200&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3740,"url":"https:\/\/www.mymiller.name\/wordpress\/springboot\/threading-in-spring-a-comprehensive-guide\/","url_meta":{"origin":3380,"position":1},"title":"Threading in Spring: A Comprehensive Guide","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Threading is a crucial aspect of building modern, high-performance applications. It allows you to execute multiple tasks concurrently, improving responsiveness and utilizing system resources effectively. Spring Framework provides robust support for managing and using threads, simplifying development and ensuring efficiency. This article explores thread usage in Spring, delves into different\u2026","rel":"","context":"In &quot;Springboot&quot;","block_context":{"text":"Springboot","link":"https:\/\/www.mymiller.name\/wordpress\/category\/springboot\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8248619_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8248619_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8248619_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8248619_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ai-generated-8248619_1280-jpg.avif 3x"},"classes":[]},{"id":3919,"url":"https:\/\/www.mymiller.name\/wordpress\/spring\/unleashing-scalability-spring-boot-and-java-virtual-threads\/","url_meta":{"origin":3380,"position":2},"title":"Unleashing Scalability: Spring Boot and Java Virtual Threads","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Java has long been a powerhouse for enterprise applications, and Spring Boot has made developing them an absolute dream. But even with Spring Boot's magic, a persistent bottleneck has challenged developers: the overhead of traditional thread-per-request models when dealing with blocking I\/O operations. Think database calls, external API integrations, or\u2026","rel":"","context":"In &quot;Spring&quot;","block_context":{"text":"Spring","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/fiber-4814456_1280.avif 3x"},"classes":[]},{"id":3951,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/scaling-streams-mastering-virtual-threads-in-spring-boot-4-and-java-25\/","url_meta":{"origin":3380,"position":3},"title":"Scaling Streams: Mastering Virtual Threads in Spring Boot 4 and Java 25","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"As a software architect, I\u2019ve seen the industry shift from heavy platform threads to reactive streams, and finally to the \"best of both worlds\": Virtual Threads. With the recent release of Spring Boot 4.0 and Java 25 (LTS), Project Loom's innovations have officially become the bedrock of high-concurrency enterprise Java.\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 3x"},"classes":[]},{"id":2391,"url":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/java-development-tips\/","url_meta":{"origin":3380,"position":4},"title":"Java Development Tips","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Java-based servers or applications often have to deal with large amounts of data.\u00a0 Whether the data is from a database, or from a local file, processing this data in an efficient manner is a priority for maintainability. In this article, we will discuss the types of Java Data Objects and\u2026","rel":"","context":"In &quot;Java Tips&quot;","block_context":{"text":"Java Tips","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_tips\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/archive-1850170_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/archive-1850170_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/archive-1850170_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1936,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/advancedstring-java-lang-string-steroids\/","url_meta":{"origin":3380,"position":5},"title":"AdvancedString &#8211; java.lang.String on steroids!","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Every need an additional method on the String Class? \u00a0Well I have and it would have made life much easier. \u00a0Unfortunately you can't subclass String as it is Final. \u00a0So what are you to do? \u00a0Well you wrap the String class. \u00a0I have created AdvancedString, which contains additional functionality and\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"AdvancedString","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3380"}],"version-history":[{"count":2,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3380\/revisions"}],"predecessor-version":[{"id":3383,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3380\/revisions\/3383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3382"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3380"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}