{"id":57773,"date":"2025-01-16T08:25:00","date_gmt":"2025-01-16T13:25:00","guid":{"rendered":"https:\/\/codesamplez.com\/?p=57773"},"modified":"2025-09-09T12:46:10","modified_gmt":"2025-09-09T16:46:10","slug":"java-concurrency-for-beginners","status":"publish","type":"post","link":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners","title":{"rendered":"Java Concurrency Basics: Threads, Executors, and Thread Pools"},"content":{"rendered":"\n<p>Ever noticed how a web server handles many users at once without breaking a sweat? That\u2019s thanks to <strong>concurrency<\/strong>. In Java, concurrency means multiple threads working in parallel so programs remain fast and responsive. In this guide, we will explore <strong>Java concurrency basics<\/strong> in a comprehensive manner so that you can understand and apply them in your real-world project efficiently and effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Concurrency in Java?<\/h2>\n\n\n\n<p>Concurrency in Java is the ability to run multiple threads simultaneously within a program, like having multiple chefs in a kitchen handling different orders at the same time. Each thread operates independently, yet they all share the same kitchen resources \u2013 your CPU and memory.<\/p>\n\n\n\n<p>Here&#8217;s the beautiful part: even with just one CPU core, Java creates the illusion of parallel execution by rapidly switching between threads. It&#8217;s like a master chef who can flip pancakes, stir soup, and plate desserts in what seems like the same instant.<\/p>\n\n\n\n<p>But here&#8217;s what most tutorials won&#8217;t tell you upfront \u2013 concurrency isn&#8217;t just about speed. It&#8217;s about building applications that stay responsive under pressure, utilize modern multi-core processors effectively, and handle multiple tasks without breaking a sweat.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Concurrency vs Parallelism: The Critical Difference<\/h2>\n\n\n\n<p>This confusion trips up even experienced developers, so let me clear it up once and for all.<\/p>\n\n\n\n<p><strong>Concurrency<\/strong> is about dealing with multiple tasks at once \u2013 managing them, coordinating them, making progress on all of them. Think of it as juggling: you&#8217;re not throwing all balls simultaneously, but you&#8217;re managing multiple balls in the air.<\/p>\n\n\n\n<p><strong>Parallelism<\/strong> is about actually doing multiple things at the exact same time. This requires multiple CPU cores \u2013 like having multiple jugglers each handling their own set of balls.<\/p>\n\n\n\n<p>In Java, you write concurrent code, and the JVM decides whether to run it in parallel based on available hardware. Smart, right?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-java-concurrency-basics-threads\">Java Concurrency Basics: Threads<\/h2>\n\n\n\n<p>Let me share something that took me years to truly understand: threads are not just about performance \u2013 they&#8217;re about designing better programs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating Threads: The Right Way<\/h3>\n\n\n\n<p>There are multiple ways to create threads in Java, but I&#8217;ll show you the approaches that actually matter in professional development:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ Method 1: Implementing Runnable (Recommended)\npublic class TaskRunner implements Runnable {\n    private final String taskName;\n    \n    public TaskRunner(String taskName) {\n        this.taskName = taskName;\n    }\n    \n    @Override\n    public void run() {\n        System.out.println(\"Executing task: \" + taskName + \n                          \" on thread: \" + Thread.currentThread().getName());\n        \/\/ Your actual task logic goes here\n    }\n}\n\n\/\/ Creating and starting the thread\nThread worker = new Thread(new TaskRunner(\"Database Backup\"));\nworker.start();\n\n\/\/ Method 2: Using Lambda Expressions (Modern Java)\nThread modernWorker = new Thread(() -> {\n    System.out.println(\"Modern thread execution on: \" + \n                      Thread.currentThread().getName());\n    \/\/ Task logic here\n});\nmodernWorker.start();<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/ Method 1: Implementing Runnable (Recommended)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">class<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">TaskRunner<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">implements<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB; font-weight: bold\">Runnable<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">private<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">final<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">taskName<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">TaskRunner<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">taskName<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">this<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">taskName<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> taskName<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">@<\/span><span style=\"color: #D08770\">Override<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">run<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">out<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Executing task: <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> taskName <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                          <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\"> on thread: <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">currentThread<\/span><span style=\"color: #ECEFF4\">().<\/span><span style=\"color: #88C0D0\">getName<\/span><span style=\"color: #ECEFF4\">())<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">        <\/span><span style=\"color: #616E88\">\/\/ Your actual task logic goes here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Creating and starting the thread<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">Thread<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">worker<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">Thread<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">TaskRunner<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Database Backup<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">worker<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">start<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Method 2: Using Lambda Expressions (Modern Java)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">Thread<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">modernWorker<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">Thread<\/span><span style=\"color: #ECEFF4\">(()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">out<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Modern thread execution on: <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                      <\/span><span style=\"color: #D8DEE9\">Thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">currentThread<\/span><span style=\"color: #ECEFF4\">().<\/span><span style=\"color: #88C0D0\">getName<\/span><span style=\"color: #ECEFF4\">())<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ Task logic here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">})<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">modernWorker<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">start<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<p><strong>Why I recommend Runnable over extending Thread<\/strong>?<\/p>\n\n\n\n<p>Implementing Runnable gives you flexibility. Your class can still extend another class if needed, and you&#8217;re following the composition-over-inheritance principle that makes code maintainable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Thread Lifecycle and States<\/h3>\n\n\n\n<p>Understanding thread states isn&#8217;t academic fluff \u2013 it&#8217;s practical knowledge that will save you debugging hours:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>NEW<\/strong>: Thread created but not started<\/li>\n\n\n\n<li><strong>RUNNABLE<\/strong>: Thread is executing or ready to execute<\/li>\n\n\n\n<li><strong>BLOCKED<\/strong>: Thread is blocked, waiting for a monitor lock<\/li>\n\n\n\n<li><strong>WAITING<\/strong>: Thread is waiting indefinitely for another thread<\/li>\n\n\n\n<li><strong>TIMED_WAITING<\/strong>: Thread is waiting for a specified period<\/li>\n\n\n\n<li><strong>TERMINATED<\/strong>: Thread has completed execution<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>Thread thread = new Thread(() -> {\n    try {\n        Thread.sleep(1000); \/\/ TIMED_WAITING state\n    } catch (InterruptedException e) {\n        Thread.currentThread().interrupt();\n    }\n});\n\nSystem.out.println(\"Before start: \" + thread.getState()); \/\/ NEW\nthread.start();\nSystem.out.println(\"After start: \" + thread.getState()); \/\/ RUNNABLE<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #8FBCBB\">Thread<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">thread<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">Thread<\/span><span style=\"color: #ECEFF4\">(()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">try<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">Thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sleep<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1000<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ TIMED_WAITING state<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">catch<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #8FBCBB\">InterruptedException<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">e<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">Thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">currentThread<\/span><span style=\"color: #ECEFF4\">().<\/span><span style=\"color: #88C0D0\">interrupt<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">})<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">out<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Before start: <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">getState<\/span><span style=\"color: #ECEFF4\">())<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ NEW<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">start<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">out<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">After start: <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">getState<\/span><span style=\"color: #ECEFF4\">())<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ RUNNABLE<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Using Executors and Thread Pools: The Professional Approach<\/h2>\n\n\n\n<p>Here&#8217;s where most developers level up their concurrency game. Raw thread creation is like manually managing memory in C \u2013 possible, but why would you when better tools exist?<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"800\" src=\"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/threadpool-task-queue-1200x800.webp\" alt=\"Java ThreadPool And Task Queue\" class=\"wp-image-59223\" srcset=\"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/threadpool-task-queue-1200x800.webp 1200w, https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/threadpool-task-queue-300x200.webp 300w, https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/threadpool-task-queue-768x512.webp 768w, https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/threadpool-task-queue.webp 1536w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use Thread Pools?<\/h3>\n\n\n\n<p>Creating threads is expensive. Each thread consumes memory (typically 1MB for the stack), and constant thread creation\/destruction kills performance. Thread pools solve this by reusing threads for multiple tasks.<\/p>\n\n\n\n<p>Think of thread pools as hiring a permanent kitchen staff instead of hiring and firing chefs for every order. Much more efficient, right?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Simple ThreadPool Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import java.util.concurrent.*;\nimport java.util.List;\nimport java.util.ArrayList;\n\npublic class ThreadPoolDemo {\n    public static void main(String[] args) {\n        \/\/ Create a fixed-size thread pool\n        ExecutorService executor = Executors.newFixedThreadPool(4);\n        \n        \/\/ Submit multiple tasks\n        List&lt;Future&lt;String>> futures = new ArrayList&lt;>();\n        \n        for (int i = 1; i &lt;= 10; i++) {\n            final int taskId = i;\n            Future&lt;String> future = executor.submit(() -> {\n                \/\/ Simulate some work\n                Thread.sleep(1000);\n                return \"Task \" + taskId + \" completed by \" + \n                       Thread.currentThread().getName();\n            });\n            futures.add(future);\n        }\n        \n        \/\/ Collect results\n        for (Future&lt;String> future : futures) {\n            try {\n                System.out.println(future.get());\n            } catch (InterruptedException | ExecutionException e) {\n                e.printStackTrace();\n            }\n        }\n        \n        \/\/ Always shut down the executor\n        executor.shutdown();\n    }\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">java<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #8FBCBB\">util<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #8FBCBB\">concurrent<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #81A1C1\">*;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">java<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #8FBCBB\">util<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #8FBCBB\">List<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">java<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #8FBCBB\">util<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #8FBCBB\">ArrayList<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">class<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">ThreadPoolDemo<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">static<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">main<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #ECEFF4\">[]<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">args<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">        <\/span><span style=\"color: #616E88\">\/\/ Create a fixed-size thread pool<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #8FBCBB\">ExecutorService<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">executor<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Executors<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">newFixedThreadPool<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">        <\/span><span style=\"color: #616E88\">\/\/ Submit multiple tasks<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #8FBCBB\">List<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">Future<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #ECEFF4\">&gt;&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">futures<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">ArrayList<\/span><span style=\"color: #ECEFF4\">&lt;&gt;()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">i<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> i <\/span><span style=\"color: #81A1C1\">&lt;=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #81A1C1\">;<\/span><span style=\"color: #D8DEE9FF\"> i<\/span><span style=\"color: #81A1C1\">++<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">final<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">taskId<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> i<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #8FBCBB\">Future<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #ECEFF4\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">future<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">executor<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">submit<\/span><span style=\"color: #ECEFF4\">(()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">                <\/span><span style=\"color: #616E88\">\/\/ Simulate some work<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                <\/span><span style=\"color: #D8DEE9\">Thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sleep<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1000<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Task <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> taskId <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\"> completed by <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                       <\/span><span style=\"color: #D8DEE9\">Thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">currentThread<\/span><span style=\"color: #ECEFF4\">().<\/span><span style=\"color: #88C0D0\">getName<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">})<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #D8DEE9\">futures<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">add<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">future<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">        <\/span><span style=\"color: #616E88\">\/\/ Collect results<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #8FBCBB\">Future<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #ECEFF4\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">future<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">:<\/span><span style=\"color: #D8DEE9FF\"> futures<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">try<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                <\/span><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">out<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">future<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">get<\/span><span style=\"color: #ECEFF4\">())<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">catch<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #8FBCBB\">InterruptedException<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">|<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">ExecutionException<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">e<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                <\/span><span style=\"color: #D8DEE9\">e<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">printStackTrace<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">        <\/span><span style=\"color: #616E88\">\/\/ Always shut down the executor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">executor<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">shutdown<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Types of Thread Pools: Choose Your Weapon<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ Fixed Thread Pool - Best for CPU-bound tasks\nExecutorService fixedPool = Executors.newFixedThreadPool(4);\n\n\/\/ Cached Thread Pool - Great for many short-lived tasks\nExecutorService cachedPool = Executors.newCachedThreadPool();\n\n\/\/ Single Thread Executor - When order matters\nExecutorService singleThread = Executors.newSingleThreadExecutor();\n\n\/\/ Scheduled Thread Pool - For recurring tasks\nScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);\nscheduler.scheduleAtFixedRate(() -> {\n    System.out.println(\"Periodic task executed\");\n}, 0, 5, TimeUnit.SECONDS);<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/ Fixed Thread Pool - Best for CPU-bound tasks<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">ExecutorService<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">fixedPool<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Executors<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">newFixedThreadPool<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Cached Thread Pool - Great for many short-lived tasks<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">ExecutorService<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">cachedPool<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Executors<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">newCachedThreadPool<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Single Thread Executor - When order matters<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">ExecutorService<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">singleThread<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Executors<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">newSingleThreadExecutor<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Scheduled Thread Pool - For recurring tasks<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">ScheduledExecutorService<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">scheduler<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Executors<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">newScheduledThreadPool<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">scheduler<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">scheduleAtFixedRate<\/span><span style=\"color: #ECEFF4\">(()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">out<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Periodic task executed<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">},<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">TimeUnit<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">SECONDS<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Common Concurrency Issues: The Gotchas That Bite<\/h2>\n\n\n\n<p>Your knowledge on Java concurrency basics aren&#8217;t complete unless you are aware of the common issues and gotchas. Let me share some common mistakes so you can avoid them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Race Conditions &amp; Synchronization<\/h3>\n\n\n\n<p>Race conditions happen when multiple threads access shared data simultaneously, and the outcome depends on timing. It&#8217;s like two people trying to edit the same document simultaneously \u2013 chaos ensues.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>public class CounterExample {\n    private int count = 0;\n    \n    \/\/ WRONG: Not thread-safe\n    public void incrementUnsafe() {\n        count++; \/\/ This is actually three operations: read, increment, write\n    }\n    \n    \/\/ RIGHT: Thread-safe with synchronization\n    public synchronized void incrementSafe() {\n        count++;\n    }\n    \n    \/\/ BETTER: Using atomic classes for simple operations\n    private AtomicInteger atomicCount = new AtomicInteger(0);\n    \n    public void incrementAtomic() {\n        atomicCount.incrementAndGet();\n    }\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">class<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">CounterExample<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">private<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">count<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ WRONG: Not thread-safe<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">incrementUnsafe<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        count<\/span><span style=\"color: #81A1C1\">++;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ This is actually three operations: read, increment, write<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ RIGHT: Thread-safe with synchronization<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">incrementSafe<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        count<\/span><span style=\"color: #81A1C1\">++;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ BETTER: Using atomic classes for simple operations<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">private<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">AtomicInteger<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">atomicCount<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">AtomicInteger<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">incrementAtomic<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">atomicCount<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">incrementAndGet<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Deadlock: The Threading Nightmare<\/h3>\n\n\n\n<p>Deadlocks occur when two or more threads wait for each other indefinitely. Here&#8217;s how to prevent them:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>public class DeadlockPrevention {\n    private static final Object lock1 = new Object();\n    private static final Object lock2 = new Object();\n    \n    \/\/ WRONG: Can cause deadlock\n    public void method1() {\n        synchronized (lock1) {\n            synchronized (lock2) {\n                \/\/ Work here\n            }\n        }\n    }\n    \n    public void method2() {\n        synchronized (lock2) { \/\/ Different order!\n            synchronized (lock1) {\n                \/\/ Work here\n            }\n        }\n    }\n    \n    \/\/ RIGHT: Always acquire locks in the same order\n    public void safeMethod1() {\n        synchronized (lock1) {\n            synchronized (lock2) {\n                \/\/ Work here\n            }\n        }\n    }\n    \n    public void safeMethod2() {\n        synchronized (lock1) { \/\/ Same order as safeMethod1\n            synchronized (lock2) {\n                \/\/ Work here\n            }\n        }\n    }\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">class<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">DeadlockPrevention<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">private<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">static<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">final<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">Object<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">lock1<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">Object<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">private<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">static<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">final<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">Object<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">lock2<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">Object<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ WRONG: Can cause deadlock<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">method1<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lock1<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lock2<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">                <\/span><span style=\"color: #616E88\">\/\/ Work here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">method2<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lock2<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ Different order!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lock1<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">                <\/span><span style=\"color: #616E88\">\/\/ Work here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">    <\/span><span style=\"color: #616E88\">\/\/ RIGHT: Always acquire locks in the same order<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">safeMethod1<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lock1<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lock2<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">                <\/span><span style=\"color: #616E88\">\/\/ Work here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">safeMethod2<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lock1<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\">\/\/ Same order as safeMethod1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">synchronized<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lock2<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">                <\/span><span style=\"color: #616E88\">\/\/ Work here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-best-practices-for-java-concurrency-basics\">Best Practices For Java Concurrency Basics<\/h2>\n\n\n\n<p>Here are some good practices that could save you countless debugging sessions:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Use High-Level Concurrency Utilities<\/h3>\n\n\n\n<p>Instead of managing synchronization manually, leverage Java&#8217;s concurrent collections and utilities:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ Instead of synchronized HashMap\nMap&lt;String, Integer> safeMap = new ConcurrentHashMap&lt;>();\n\n\/\/ Instead of synchronized ArrayList\nList&lt;String> safeList = new CopyOnWriteArrayList&lt;>();\n\n\/\/ For producer-consumer scenarios\nBlockingQueue&lt;Task> taskQueue = new LinkedBlockingQueue&lt;>();<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/ Instead of synchronized HashMap<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">Map<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">Integer<\/span><span style=\"color: #ECEFF4\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">safeMap<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">ConcurrentHashMap<\/span><span style=\"color: #ECEFF4\">&lt;&gt;()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Instead of synchronized ArrayList<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">List<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #ECEFF4\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">safeList<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">CopyOnWriteArrayList<\/span><span style=\"color: #ECEFF4\">&lt;&gt;()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ For producer-consumer scenarios<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">BlockingQueue<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">Task<\/span><span style=\"color: #ECEFF4\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">taskQueue<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">LinkedBlockingQueue<\/span><span style=\"color: #ECEFF4\">&lt;&gt;()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Properly Size Your Thread Pools<\/h3>\n\n\n\n<p><strong>For CPU-bound tasks:<\/strong> Number of cores + 1 <strong>For I\/O-bound tasks:<\/strong> Number of cores \u00d7 (1 + wait time \/ service time)<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ CPU-bound tasks\nint cores = Runtime.getRuntime().availableProcessors();\nExecutorService cpuPool = Executors.newFixedThreadPool(cores + 1);\n\n\/\/ I\/O-bound tasks (rough estimate)\nExecutorService ioPool = Executors.newFixedThreadPool(cores * 2);<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/ CPU-bound tasks<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">cores<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Runtime<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">getRuntime<\/span><span style=\"color: #ECEFF4\">().<\/span><span style=\"color: #88C0D0\">availableProcessors<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">ExecutorService<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">cpuPool<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Executors<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">newFixedThreadPool<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">cores <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ I\/O-bound tasks (rough estimate)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #8FBCBB\">ExecutorService<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">ioPool<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Executors<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">newFixedThreadPool<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">cores <\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Always Handle InterruptedException Properly<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>public void interruptibleTask() {\n    try {\n        \/\/ Some long-running operation\n        Thread.sleep(10000);\n    } catch (InterruptedException e) {\n        \/\/ Restore interrupted status\n        Thread.currentThread().interrupt();\n        \/\/ Handle the interruption gracefully\n        System.out.println(\"Task was interrupted\");\n    }\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">public<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">void<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">interruptibleTask<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">try<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">        <\/span><span style=\"color: #616E88\">\/\/ Some long-running operation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">Thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sleep<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">10000<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">catch<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #8FBCBB\">InterruptedException<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">e<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">        <\/span><span style=\"color: #616E88\">\/\/ Restore interrupted status<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">Thread<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">currentThread<\/span><span style=\"color: #ECEFF4\">().<\/span><span style=\"color: #88C0D0\">interrupt<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">        <\/span><span style=\"color: #616E88\">\/\/ Handle the interruption gracefully<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">out<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Task was interrupted<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. Use CompletableFuture for Complex Async Operations<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>CompletableFuture&lt;String> future = CompletableFuture\n    .supplyAsync(() -> fetchUserData())\n    .thenApplyAsync(userData -> processUserData(userData))\n    .thenApplyAsync(processedData -> generateReport(processedData))\n    .exceptionally(throwable -> {\n        System.err.println(\"Error in async chain: \" + throwable.getMessage());\n        return \"Default report\";\n    });\n\n\/\/ Non-blocking way to get result\nfuture.thenAccept(report -> System.out.println(\"Report: \" + report));<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #8FBCBB\">CompletableFuture<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #ECEFF4\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">future<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">CompletableFuture<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">supplyAsync<\/span><span style=\"color: #ECEFF4\">(()<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">fetchUserData<\/span><span style=\"color: #ECEFF4\">())<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">thenApplyAsync<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">userData <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">processUserData<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">userData<\/span><span style=\"color: #ECEFF4\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">thenApplyAsync<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">processedData <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">generateReport<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">processedData<\/span><span style=\"color: #ECEFF4\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">exceptionally<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">throwable <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">err<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Error in async chain: <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">throwable<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">getMessage<\/span><span style=\"color: #ECEFF4\">())<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Default report<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">})<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Non-blocking way to get result<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">future<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">thenAccept<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">report <\/span><span style=\"color: #8FBCBB\">-&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">System<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">out<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">println<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Report: <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> report<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #81A1C1\">;<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#2e3440ff;color:#c8d0e0;font-size:12px;line-height:1;position:relative\">Java<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs-quick-answers-on-java-concurrency-basics\">FAQs: Quick Answers on Java Concurrency Basics<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1757433343757\"><strong class=\"schema-faq-question\">How do you create a thread in Java?<\/strong> <p class=\"schema-faq-answer\">You can create threads by implementing the Runnable interface (recommended) or extending the Thread class. The modern approach uses ExecutorService with thread pools for better resource management.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1757433350672\"><strong class=\"schema-faq-question\">Concurrency vs Multithreading \u2013 are they the same?<\/strong> <p class=\"schema-faq-answer\">Multithreading is one way to achieve concurrency in Java. Concurrency is the broader concept of managing multiple tasks, while multithreading specifically uses multiple threads to achieve this.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1757433359665\"><strong class=\"schema-faq-question\">How to avoid race conditions in Java?<\/strong> <p class=\"schema-faq-answer\">Use synchronization mechanisms like synchronized blocks, atomic classes (AtomicInteger, AtomicReference), or concurrent collections (ConcurrentHashMap) to ensure thread-safe access to shared resources.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1757433374495\"><strong class=\"schema-faq-question\">When should I use a Thread Pool?<\/strong> <p class=\"schema-faq-answer\">Use thread pools whenever you have multiple tasks to execute, especially short-lived or recurring tasks. Thread pools reuse threads, reducing the overhead of thread creation and destruction.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1757433385401\"><strong class=\"schema-faq-question\">What&#8217;s the difference between execute() and submit() in ExecutorService?<\/strong> <p class=\"schema-faq-answer\">execute() runs tasks without returning results and can&#8217;t handle checked exceptions. submit() returns a Future object that allows you to get results and handle exceptions properly.<\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-advanced-topics-what-s-next\">Advanced Topics: What&#8217;s Next?<\/h2>\n\n\n\n<p>As you master these basics, consider exploring:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Virtual Threads (Project Loom)<\/strong> &#8211; Available in Java 19+, these lightweight threads can revolutionize how we handle concurrency<\/li>\n\n\n\n<li><strong>Reactive Programming<\/strong> with libraries like RxJava or Project Reactor<\/li>\n\n\n\n<li><strong>Fork\/Join Framework<\/strong> for divide-and-conquer algorithms<\/li>\n\n\n\n<li><strong>Lock-free programming<\/strong> with atomic operations<\/li>\n<\/ul>\n\n\n\n<p>Pro Tip\ud83d\udca1: Learn about <a href=\"https:\/\/codesamplez.com\/programming\/forkjoinpool-vs-threadpoolexecutor\">differences between ForkJoinPool and ThreadPoolExecutor<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Real-World Performance Tips<\/h2>\n\n\n\n<p>From my experience building high-traffic applications:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Monitor thread pool metrics<\/strong> in production \u2013 active threads, queue size, and task completion rates tell you everything<\/li>\n\n\n\n<li><strong>Use thread dumps<\/strong> when debugging concurrency issues \u2013 they reveal exactly what threads are doing<\/li>\n\n\n\n<li><strong>Avoid creating threads in hot paths<\/strong> \u2013 always use pre-initialized thread pools<\/li>\n\n\n\n<li><strong>Consider virtual threads<\/strong> for I\/O-heavy applications in modern Java versions<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping It Up<\/h2>\n\n\n\n<p>Java concurrency basics aren&#8217;t just about making code faster \u2013 they&#8217;re about building robust, scalable applications that can handle real-world demands. Start with simple thread pools, understand the fundamentals, and gradually explore advanced topics.<\/p>\n\n\n\n<p>The key is to practice with small examples before tackling complex scenarios. Every expert was once a beginner who made mistakes, learned from them, and kept pushing forward.<\/p>\n\n\n\n<p>Remember: concurrency is a tool, not a goal. Use it when it solves real problems, not just because you can. Your future self (and your users) will thank you for building applications that are both powerful and reliable.<\/p>\n\n\n\n<p>Ready to dive deeper? Check out these essential resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/concurrency\/\">Oracle&#8217;s Official Java Concurrency Tutorial<\/a><\/li>\n\n\n\n<li>&#8220;Java Concurrency in Practice&#8221; by Brian Goetz (the definitive guide)<\/li>\n\n\n\n<li>OpenJDK&#8217;s documentation on Virtual Threads<\/li>\n<\/ul>\n\n\n\n<p>Keep experimenting, keep learning, and most importantly \u2013 don&#8217;t be afraid to make mistakes. They&#8217;re the best teachers in the concurrency world!s like a cook in your hive of operations, handling one task while other threads do theirs. Multiple cooks doing different dishes using the same kitchen resources is precisely like how multiple threads in your Java application will use the same CPU and memory.<\/p>\n\n\n\n<p><span style=\"margin: 0px; padding: 0px;\">However, it doesn&#8217;t necessarily mean they are running&nbsp;<em>exactly<\/em>&nbsp;at the same instant (unless you have multiple CPU cores)<\/span>. Instead, it&#8217;s like the chef rapidly switching between tasks, giving the illusion of parallel execution.<\/p>\n\n\n\n<p>But here\u2019s the catch: too many threads buzzing at once? That\u2019s how you get stung. (Deadlocks, race conditions\u2026 trust me, you don\u2019t want that mess.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Java concurrency with this complete beginner&#8217;s guide covering threads, executors, and thread pools. Learn to build lightning-fast, responsive applications that handle real-world demands. From basic thread creation to advanced synchronization techniques, discover the secrets of professional concurrent programming with practical examples and expert insights.<\/p>\n","protected":false},"author":1,"featured_media":59222,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_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}},"categories":[1],"tags":[3319,6,3272],"class_list":{"0":"post-57773","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-programming","8":"tag-concurrency","9":"tag-java","10":"tag-thread","11":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Java Concurrency Basics: Threads, Executors, and Thread Pools - CodeSamplez.com<\/title>\n<meta name=\"description\" content=\"Learn Java concurrency basics step-by-step \u2013 from threads to thread pools \u2013 with examples. Write multi-threaded Java programs confidently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Concurrency Basics: Threads, Executors, and Thread Pools\" \/>\n<meta property=\"og:description\" content=\"Learn Java concurrency basics step-by-step \u2013 from threads to thread pools \u2013 with examples. Write multi-threaded Java programs confidently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners\" \/>\n<meta property=\"og:site_name\" content=\"CodeSamplez.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codesamplez\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/ranacseruet\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-16T13:25:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-09T16:46:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/java-concurrency-basics.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1536\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Rana Ahsan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ranacseruet\" \/>\n<meta name=\"twitter:site\" content=\"@codesamplez\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rana Ahsan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners\"},\"author\":{\"name\":\"Rana Ahsan\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#\\\/schema\\\/person\\\/a82c3c07205f4bb73d6b3b0906bc328b\"},\"headline\":\"Java Concurrency Basics: Threads, Executors, and Thread Pools\",\"datePublished\":\"2025-01-16T13:25:00+00:00\",\"dateModified\":\"2025-09-09T16:46:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners\"},\"wordCount\":1306,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/java-concurrency-basics.webp\",\"keywords\":[\"concurrency\",\"java\",\"thread\"],\"articleSection\":[\"Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners\",\"name\":\"Java Concurrency Basics: Threads, Executors, and Thread Pools - CodeSamplez.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/java-concurrency-basics.webp\",\"datePublished\":\"2025-01-16T13:25:00+00:00\",\"dateModified\":\"2025-09-09T16:46:10+00:00\",\"description\":\"Learn Java concurrency basics step-by-step \u2013 from threads to thread pools \u2013 with examples. Write multi-threaded Java programs confidently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433343757\"},{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433350672\"},{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433359665\"},{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433374495\"},{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433385401\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#primaryimage\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/java-concurrency-basics.webp\",\"contentUrl\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/java-concurrency-basics.webp\",\"width\":1536,\"height\":1024,\"caption\":\"Java Concurrency Basics\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codesamplez.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Concurrency Basics: Threads, Executors, and Thread Pools\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#website\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/\",\"name\":\"CODESAMPLEZ.COM\",\"description\":\"Programming And Development Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codesamplez.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#organization\",\"name\":\"codesamplez.com\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/cropped-favicon.webp\",\"contentUrl\":\"https:\\\/\\\/codesamplez.com\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/cropped-favicon.webp\",\"width\":512,\"height\":512,\"caption\":\"codesamplez.com\"},\"image\":{\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codesamplez\",\"https:\\\/\\\/x.com\\\/codesamplez\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/#\\\/schema\\\/person\\\/a82c3c07205f4bb73d6b3b0906bc328b\",\"name\":\"Rana Ahsan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g\",\"caption\":\"Rana Ahsan\"},\"description\":\"Rana Ahsan is a seasoned software engineer and technology leader specialized in distributed systems and software architecture. With a Master\u2019s in Software Engineering from Concordia University, his experience spans leading scalable architecture at Coursera and TopHat, contributing to open-source projects. This blog, CodeSamplez.com, showcases his passion for sharing practical insights on programming and distributed systems concepts and help educate others. Github | X | LinkedIn\",\"sameAs\":[\"https:\\\/\\\/github.com\\\/ranacseruet\",\"https:\\\/\\\/www.facebook.com\\\/ranacseruet\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/ranacseruet\\\/\",\"https:\\\/\\\/x.com\\\/ranacseruet\"],\"url\":\"https:\\\/\\\/codesamplez.com\\\/author\\\/admin\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433343757\",\"position\":1,\"url\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433343757\",\"name\":\"How do you create a thread in Java?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"You can create threads by implementing the Runnable interface (recommended) or extending the Thread class. The modern approach uses ExecutorService with thread pools for better resource management.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433350672\",\"position\":2,\"url\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433350672\",\"name\":\"Concurrency vs Multithreading \u2013 are they the same?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Multithreading is one way to achieve concurrency in Java. Concurrency is the broader concept of managing multiple tasks, while multithreading specifically uses multiple threads to achieve this.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433359665\",\"position\":3,\"url\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433359665\",\"name\":\"How to avoid race conditions in Java?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use synchronization mechanisms like synchronized blocks, atomic classes (AtomicInteger, AtomicReference), or concurrent collections (ConcurrentHashMap) to ensure thread-safe access to shared resources.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433374495\",\"position\":4,\"url\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433374495\",\"name\":\"When should I use a Thread Pool?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use thread pools whenever you have multiple tasks to execute, especially short-lived or recurring tasks. Thread pools reuse threads, reducing the overhead of thread creation and destruction.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433385401\",\"position\":5,\"url\":\"https:\\\/\\\/codesamplez.com\\\/programming\\\/java-concurrency-for-beginners#faq-question-1757433385401\",\"name\":\"What's the difference between execute() and submit() in ExecutorService?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"execute() runs tasks without returning results and can't handle checked exceptions. submit() returns a Future object that allows you to get results and handle exceptions properly.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java Concurrency Basics: Threads, Executors, and Thread Pools - CodeSamplez.com","description":"Learn Java concurrency basics step-by-step \u2013 from threads to thread pools \u2013 with examples. Write multi-threaded Java programs confidently.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners","og_locale":"en_US","og_type":"article","og_title":"Java Concurrency Basics: Threads, Executors, and Thread Pools","og_description":"Learn Java concurrency basics step-by-step \u2013 from threads to thread pools \u2013 with examples. Write multi-threaded Java programs confidently.","og_url":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners","og_site_name":"CodeSamplez.com","article_publisher":"https:\/\/www.facebook.com\/codesamplez","article_author":"https:\/\/www.facebook.com\/ranacseruet","article_published_time":"2025-01-16T13:25:00+00:00","article_modified_time":"2025-09-09T16:46:10+00:00","og_image":[{"width":1536,"height":1024,"url":"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/java-concurrency-basics.webp","type":"image\/webp"}],"author":"Rana Ahsan","twitter_card":"summary_large_image","twitter_creator":"@ranacseruet","twitter_site":"@codesamplez","twitter_misc":{"Written by":"Rana Ahsan","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#article","isPartOf":{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners"},"author":{"name":"Rana Ahsan","@id":"https:\/\/codesamplez.com\/#\/schema\/person\/a82c3c07205f4bb73d6b3b0906bc328b"},"headline":"Java Concurrency Basics: Threads, Executors, and Thread Pools","datePublished":"2025-01-16T13:25:00+00:00","dateModified":"2025-09-09T16:46:10+00:00","mainEntityOfPage":{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners"},"wordCount":1306,"commentCount":0,"publisher":{"@id":"https:\/\/codesamplez.com\/#organization"},"image":{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#primaryimage"},"thumbnailUrl":"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/java-concurrency-basics.webp","keywords":["concurrency","java","thread"],"articleSection":["Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners","url":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners","name":"Java Concurrency Basics: Threads, Executors, and Thread Pools - CodeSamplez.com","isPartOf":{"@id":"https:\/\/codesamplez.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#primaryimage"},"image":{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#primaryimage"},"thumbnailUrl":"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/java-concurrency-basics.webp","datePublished":"2025-01-16T13:25:00+00:00","dateModified":"2025-09-09T16:46:10+00:00","description":"Learn Java concurrency basics step-by-step \u2013 from threads to thread pools \u2013 with examples. Write multi-threaded Java programs confidently.","breadcrumb":{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#breadcrumb"},"mainEntity":[{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433343757"},{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433350672"},{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433359665"},{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433374495"},{"@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433385401"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#primaryimage","url":"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/java-concurrency-basics.webp","contentUrl":"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/java-concurrency-basics.webp","width":1536,"height":1024,"caption":"Java Concurrency Basics"},{"@type":"BreadcrumbList","@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codesamplez.com\/"},{"@type":"ListItem","position":2,"name":"Java Concurrency Basics: Threads, Executors, and Thread Pools"}]},{"@type":"WebSite","@id":"https:\/\/codesamplez.com\/#website","url":"https:\/\/codesamplez.com\/","name":"CODESAMPLEZ.COM","description":"Programming And Development Resources","publisher":{"@id":"https:\/\/codesamplez.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codesamplez.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codesamplez.com\/#organization","name":"codesamplez.com","url":"https:\/\/codesamplez.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codesamplez.com\/#\/schema\/logo\/image\/","url":"https:\/\/codesamplez.com\/wp-content\/uploads\/2024\/10\/cropped-favicon.webp","contentUrl":"https:\/\/codesamplez.com\/wp-content\/uploads\/2024\/10\/cropped-favicon.webp","width":512,"height":512,"caption":"codesamplez.com"},"image":{"@id":"https:\/\/codesamplez.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codesamplez","https:\/\/x.com\/codesamplez"]},{"@type":"Person","@id":"https:\/\/codesamplez.com\/#\/schema\/person\/a82c3c07205f4bb73d6b3b0906bc328b","name":"Rana Ahsan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c7a4f88bcf4a55cd1483386318ebecf27359154275a0b355b0ea186676f9f7f?s=96&d=mm&r=g","caption":"Rana Ahsan"},"description":"Rana Ahsan is a seasoned software engineer and technology leader specialized in distributed systems and software architecture. With a Master\u2019s in Software Engineering from Concordia University, his experience spans leading scalable architecture at Coursera and TopHat, contributing to open-source projects. This blog, CodeSamplez.com, showcases his passion for sharing practical insights on programming and distributed systems concepts and help educate others. Github | X | LinkedIn","sameAs":["https:\/\/github.com\/ranacseruet","https:\/\/www.facebook.com\/ranacseruet","https:\/\/www.linkedin.com\/in\/ranacseruet\/","https:\/\/x.com\/ranacseruet"],"url":"https:\/\/codesamplez.com\/author\/admin"},{"@type":"Question","@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433343757","position":1,"url":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433343757","name":"How do you create a thread in Java?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"You can create threads by implementing the Runnable interface (recommended) or extending the Thread class. The modern approach uses ExecutorService with thread pools for better resource management.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433350672","position":2,"url":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433350672","name":"Concurrency vs Multithreading \u2013 are they the same?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Multithreading is one way to achieve concurrency in Java. Concurrency is the broader concept of managing multiple tasks, while multithreading specifically uses multiple threads to achieve this.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433359665","position":3,"url":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433359665","name":"How to avoid race conditions in Java?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Use synchronization mechanisms like synchronized blocks, atomic classes (AtomicInteger, AtomicReference), or concurrent collections (ConcurrentHashMap) to ensure thread-safe access to shared resources.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433374495","position":4,"url":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433374495","name":"When should I use a Thread Pool?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Use thread pools whenever you have multiple tasks to execute, especially short-lived or recurring tasks. Thread pools reuse threads, reducing the overhead of thread creation and destruction.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433385401","position":5,"url":"https:\/\/codesamplez.com\/programming\/java-concurrency-for-beginners#faq-question-1757433385401","name":"What's the difference between execute() and submit() in ExecutorService?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"execute() runs tasks without returning results and can't handle checked exceptions. submit() returns a Future object that allows you to get results and handle exceptions properly.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/codesamplez.com\/wp-content\/uploads\/2025\/01\/java-concurrency-basics.webp","jetpack_shortlink":"https:\/\/wp.me\/p1hHlI-f1P","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":56927,"url":"https:\/\/codesamplez.com\/programming\/forkjoinpool-vs-threadpoolexecutor","url_meta":{"origin":57773,"position":0},"title":"Java ForkJoinPool vs ThreadPoolExecutor","author":"Rana Ahsan","date":"August 6, 2024","format":false,"excerpt":"ForkJoinPool and ThreadPoolExecutor are essential tools in Java concurrency. ForkJoinPool excels at handling recursive, CPU-intensive tasks using a divide-and-conquer approach, while ThreadPoolExecutor is versatile for managing general-purpose, mixed workloads, including IO-bound tasks. This article explores their differences, usage scenarios, and how to maximize their performance.","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"ForkJoinPool vs ThreadPoolExecutor","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2024\/08\/ForkJoinPool-vs-ThreadPoolExecutor.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2024\/08\/ForkJoinPool-vs-ThreadPoolExecutor.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2024\/08\/ForkJoinPool-vs-ThreadPoolExecutor.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2024\/08\/ForkJoinPool-vs-ThreadPoolExecutor.webp?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":59299,"url":"https:\/\/codesamplez.com\/programming\/python-runtime-environment","url_meta":{"origin":57773,"position":1},"title":"Python Runtime Environment: Understanding Code Execution Flow","author":"Rana Ahsan","date":"November 24, 2025","format":false,"excerpt":"Ever wondered what happens when you run Python code? The Python runtime environment\u2014comprising the interpreter, virtual machine, and system resources\u2014executes your code through bytecode compilation and stack-based execution. Understanding these internals helps you debug faster, optimize smarter, and deploy with confidence. Master Python's runtime today.","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"python runtime environment","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/11\/python-runtime-environment.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/11\/python-runtime-environment.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/11\/python-runtime-environment.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/11\/python-runtime-environment.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/11\/python-runtime-environment.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/11\/python-runtime-environment.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":57931,"url":"https:\/\/codesamplez.com\/programming\/python-asyncio-tutorial","url_meta":{"origin":57773,"position":2},"title":"Python AsyncIO Tutorial: A Comprehensive Guide To Async Python","author":"Rana Ahsan","date":"February 10, 2025","format":false,"excerpt":"Alright, listen up! Tired of slow Python code? Python asyncio is your superhero! Imagine a restaurant where everyone waits ages for food \u2013 yikes! Asyncio is like a super-efficient waiter, juggling tasks and making your programs lightning fast, especially with web stuff. Trust me, this is Python power-up you need\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"Python AsyncIO Tutorial","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/python-asyncio-tutorial.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/python-asyncio-tutorial.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/python-asyncio-tutorial.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/python-asyncio-tutorial.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/python-asyncio-tutorial.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/02\/python-asyncio-tutorial.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":57829,"url":"https:\/\/codesamplez.com\/programming\/python-advanced-concepts","url_meta":{"origin":57773,"position":3},"title":"Advanced Python Concepts: Guide To Explore Beyond Basics","author":"Rana Ahsan","date":"January 29, 2025","format":false,"excerpt":"Unlock the power of Python with this deep dive into advanced concepts like decorators, generators, async\/await, and metaclasses. Perfect for intermediate to advanced developers, this guide offers practical examples, insights, and actionable tips to level up your Python skills. Ready to transform your code? Let\u2019s get started! \ud83d\udc0d\u2728","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"python advanced concepts","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/01\/python-advanced-concepts.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/01\/python-advanced-concepts.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/01\/python-advanced-concepts.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/01\/python-advanced-concepts.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/01\/python-advanced-concepts.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/01\/python-advanced-concepts.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":58169,"url":"https:\/\/codesamplez.com\/programming\/python-gil","url_meta":{"origin":57773,"position":4},"title":"Python GIL (Global Interpreter Lock) Explained \u2013 A Complete Guide","author":"Rana Ahsan","date":"March 13, 2025","format":false,"excerpt":"Struggling with Python's Global Interpreter Lock? You're not alone! When I first discovered the GIL, it completely derailed my multi-threading plans. This guide explains what the GIL is, why it exists, and proven strategies to work around its limitations in your Python applications.","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"Python GIL (Global Interpreter Lock) Tutorial","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/03\/python-gil-tutorial.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/03\/python-gil-tutorial.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/03\/python-gil-tutorial.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/03\/python-gil-tutorial.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/03\/python-gil-tutorial.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2025\/03\/python-gil-tutorial.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":22969,"url":"https:\/\/codesamplez.com\/programming\/multithreaded-programming-c-sharp","url_meta":{"origin":57773,"position":5},"title":"Multithreaded Programming In C#: A Beginners Guide","author":"Rana Ahsan","date":"February 18, 2013","format":false,"excerpt":"In this guide, we explore the essentials of multithreaded programming in C#. Multithreading allows you to run tasks in parallel, improving the performance and responsiveness of your application. By leveraging the Thread and ThreadPool classes, developers can easily implement multithreading for tasks like database queries or UI operations.","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/codesamplez.com\/category\/programming"},"img":{"alt_text":"Multithreaded Programming C#","src":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/multi-threaded-programming-c-sharp.webp?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/multi-threaded-programming-c-sharp.webp?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/multi-threaded-programming-c-sharp.webp?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/multi-threaded-programming-c-sharp.webp?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/multi-threaded-programming-c-sharp.webp?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/codesamplez.com\/wp-content\/uploads\/2013\/02\/multi-threaded-programming-c-sharp.webp?resize=1400%2C800&ssl=1 4x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/posts\/57773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/comments?post=57773"}],"version-history":[{"count":11,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/posts\/57773\/revisions"}],"predecessor-version":[{"id":59225,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/posts\/57773\/revisions\/59225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/media\/59222"}],"wp:attachment":[{"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/media?parent=57773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/categories?post=57773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codesamplez.com\/wp-json\/wp\/v2\/tags?post=57773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}