[{"data":1,"prerenderedAt":1337},["ShallowReactive",2],{"tag-multithreading":3},[4],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"date":11,"layout":12,"subtitle":13,"image":14,"optimized_image":14,"category":15,"tags":16,"author":20,"paginate":7,"body":21,"_type":1331,"_id":1332,"_source":1333,"_file":1334,"_stem":1335,"_extension":1336},"/posts/mastering-concurrency","posts",false,"","Mastering Concurrency in Java: Threads, Executors, and Virtual Threads","Exploring Java’s concurrency model with detailed insights into Threads, ExecutorService, and Virtual Threads from Project Loom.","2024-03-15T11:00:00.000Z","post","A Deep Dive into Java’s Multithreading and Parallel Computing","/assets/img/uploads/java-concurrency.png","code",[15,17,18,19],"java","concurrency","multithreading","jaimedearcos",{"type":22,"children":23,"toc":1314},"root",[24,36,43,48,104,117,123,151,433,440,473,478,484,504,510,515,816,822,869,875,893,899,904,910,943,948,1213,1219,1237,1243,1248,1253,1259,1303,1308],{"type":25,"tag":26,"props":27,"children":28},"element","blockquote",{},[29],{"type":25,"tag":30,"props":31,"children":32},"p",{},[33],{"type":34,"value":35},"text","Java's concurrency model is a critical component for building scalable and efficient applications. From classic Thread management to modern ExecutorService and the revolutionary Virtual Threads in Project Loom, mastering Java’s concurrency tools is essential for high-performance development.",{"type":25,"tag":37,"props":38,"children":40},"h2",{"id":39},"understanding-javas-concurrency-model",[41],{"type":34,"value":42},"Understanding Java’s Concurrency Model",{"type":25,"tag":30,"props":44,"children":45},{},[46],{"type":34,"value":47},"Concurrency in Java enables applications to perform multiple tasks simultaneously, improving throughput and responsiveness. Java provides several concurrency primitives:",{"type":25,"tag":49,"props":50,"children":51},"ul",{},[52,64,74,84,94],{"type":25,"tag":53,"props":54,"children":55},"li",{},[56,62],{"type":25,"tag":57,"props":58,"children":59},"strong",{},[60],{"type":34,"value":61},"Threads",{"type":34,"value":63},": The fundamental unit of execution, allowing tasks to run in parallel.",{"type":25,"tag":53,"props":65,"children":66},{},[67,72],{"type":25,"tag":57,"props":68,"children":69},{},[70],{"type":34,"value":71},"Executors",{"type":34,"value":73},": High-level abstraction for managing thread pools and task execution.",{"type":25,"tag":53,"props":75,"children":76},{},[77,82],{"type":25,"tag":57,"props":78,"children":79},{},[80],{"type":34,"value":81},"Virtual Threads",{"type":34,"value":83},": Lightweight, memory-efficient threads introduced in Java 21 for massive scalability.",{"type":25,"tag":53,"props":85,"children":86},{},[87,92],{"type":25,"tag":57,"props":88,"children":89},{},[90],{"type":34,"value":91},"Fork/Join Framework",{"type":34,"value":93},": Designed for divide-and-conquer algorithms using parallel task execution.",{"type":25,"tag":53,"props":95,"children":96},{},[97,102],{"type":25,"tag":57,"props":98,"children":99},{},[100],{"type":34,"value":101},"Reactive Programming",{"type":34,"value":103},": Asynchronous, non-blocking programming using event streams.",{"type":25,"tag":30,"props":105,"children":106},{},[107,109,115],{"type":34,"value":108},"Let's now take a deeper look at three major building blocks of concurrent programming in Java: classic threads, the ",{"type":25,"tag":15,"props":110,"children":112},{"className":111},[],[113],{"type":34,"value":114},"ExecutorService",{"type":34,"value":116},", and the new virtual threads.",{"type":25,"tag":37,"props":118,"children":120},{"id":119},"classic-approach-java-threads",[121],{"type":34,"value":122},"Classic Approach: Java Threads",{"type":25,"tag":30,"props":124,"children":125},{},[126,128,134,136,141,143,149],{"type":34,"value":127},"The most basic unit of concurrency in Java is the ",{"type":25,"tag":15,"props":129,"children":131},{"className":130},[],[132],{"type":34,"value":133},"Thread",{"type":34,"value":135},". You can extend the ",{"type":25,"tag":15,"props":137,"children":139},{"className":138},[],[140],{"type":34,"value":133},{"type":34,"value":142}," class or implement the ",{"type":25,"tag":15,"props":144,"children":146},{"className":145},[],[147],{"type":34,"value":148},"Runnable",{"type":34,"value":150}," interface to define concurrent tasks.",{"type":25,"tag":152,"props":153,"children":156},"pre",{"className":154,"code":155,"language":17,"meta":8,"style":8},"language-java shiki shiki-themes github-dark github-light","class SimpleThreadExample extends Thread {\n    public void run() {\n        System.out.println(\"Thread running: \" + Thread.currentThread().getName());\n    }\n}\n\npublic class ThreadDemo {\n    public static void main(String[] args) {\n        SimpleThreadExample thread = new SimpleThreadExample();\n        thread.start();\n    }\n}\n",[157],{"type":25,"tag":15,"props":158,"children":159},{"__ignoreMap":8},[160,194,218,273,282,291,301,324,371,399,417,425],{"type":25,"tag":161,"props":162,"children":165},"span",{"class":163,"line":164},"line",1,[166,172,178,183,188],{"type":25,"tag":161,"props":167,"children":169},{"style":168},"--shiki-default:#F97583;--shiki-light:#D73A49",[170],{"type":34,"value":171},"class",{"type":25,"tag":161,"props":173,"children":175},{"style":174},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[176],{"type":34,"value":177}," SimpleThreadExample",{"type":25,"tag":161,"props":179,"children":180},{"style":168},[181],{"type":34,"value":182}," extends",{"type":25,"tag":161,"props":184,"children":185},{"style":174},[186],{"type":34,"value":187}," Thread",{"type":25,"tag":161,"props":189,"children":191},{"style":190},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[192],{"type":34,"value":193}," {\n",{"type":25,"tag":161,"props":195,"children":197},{"class":163,"line":196},2,[198,203,208,213],{"type":25,"tag":161,"props":199,"children":200},{"style":168},[201],{"type":34,"value":202},"    public",{"type":25,"tag":161,"props":204,"children":205},{"style":168},[206],{"type":34,"value":207}," void",{"type":25,"tag":161,"props":209,"children":210},{"style":174},[211],{"type":34,"value":212}," run",{"type":25,"tag":161,"props":214,"children":215},{"style":190},[216],{"type":34,"value":217},"() {\n",{"type":25,"tag":161,"props":219,"children":221},{"class":163,"line":220},3,[222,227,232,237,243,248,253,258,263,268],{"type":25,"tag":161,"props":223,"children":224},{"style":190},[225],{"type":34,"value":226},"        System.out.",{"type":25,"tag":161,"props":228,"children":229},{"style":174},[230],{"type":34,"value":231},"println",{"type":25,"tag":161,"props":233,"children":234},{"style":190},[235],{"type":34,"value":236},"(",{"type":25,"tag":161,"props":238,"children":240},{"style":239},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[241],{"type":34,"value":242},"\"Thread running: \"",{"type":25,"tag":161,"props":244,"children":245},{"style":168},[246],{"type":34,"value":247}," +",{"type":25,"tag":161,"props":249,"children":250},{"style":190},[251],{"type":34,"value":252}," Thread.",{"type":25,"tag":161,"props":254,"children":255},{"style":174},[256],{"type":34,"value":257},"currentThread",{"type":25,"tag":161,"props":259,"children":260},{"style":190},[261],{"type":34,"value":262},"().",{"type":25,"tag":161,"props":264,"children":265},{"style":174},[266],{"type":34,"value":267},"getName",{"type":25,"tag":161,"props":269,"children":270},{"style":190},[271],{"type":34,"value":272},"());\n",{"type":25,"tag":161,"props":274,"children":276},{"class":163,"line":275},4,[277],{"type":25,"tag":161,"props":278,"children":279},{"style":190},[280],{"type":34,"value":281},"    }\n",{"type":25,"tag":161,"props":283,"children":285},{"class":163,"line":284},5,[286],{"type":25,"tag":161,"props":287,"children":288},{"style":190},[289],{"type":34,"value":290},"}\n",{"type":25,"tag":161,"props":292,"children":294},{"class":163,"line":293},6,[295],{"type":25,"tag":161,"props":296,"children":298},{"emptyLinePlaceholder":297},true,[299],{"type":34,"value":300},"\n",{"type":25,"tag":161,"props":302,"children":304},{"class":163,"line":303},7,[305,310,315,320],{"type":25,"tag":161,"props":306,"children":307},{"style":168},[308],{"type":34,"value":309},"public",{"type":25,"tag":161,"props":311,"children":312},{"style":168},[313],{"type":34,"value":314}," class",{"type":25,"tag":161,"props":316,"children":317},{"style":174},[318],{"type":34,"value":319}," ThreadDemo",{"type":25,"tag":161,"props":321,"children":322},{"style":190},[323],{"type":34,"value":193},{"type":25,"tag":161,"props":325,"children":327},{"class":163,"line":326},8,[328,332,337,341,346,350,355,360,366],{"type":25,"tag":161,"props":329,"children":330},{"style":168},[331],{"type":34,"value":202},{"type":25,"tag":161,"props":333,"children":334},{"style":168},[335],{"type":34,"value":336}," static",{"type":25,"tag":161,"props":338,"children":339},{"style":168},[340],{"type":34,"value":207},{"type":25,"tag":161,"props":342,"children":343},{"style":174},[344],{"type":34,"value":345}," main",{"type":25,"tag":161,"props":347,"children":348},{"style":190},[349],{"type":34,"value":236},{"type":25,"tag":161,"props":351,"children":352},{"style":168},[353],{"type":34,"value":354},"String",{"type":25,"tag":161,"props":356,"children":357},{"style":190},[358],{"type":34,"value":359},"[] ",{"type":25,"tag":161,"props":361,"children":363},{"style":362},"--shiki-default:#FFAB70;--shiki-light:#E36209",[364],{"type":34,"value":365},"args",{"type":25,"tag":161,"props":367,"children":368},{"style":190},[369],{"type":34,"value":370},") {\n",{"type":25,"tag":161,"props":372,"children":374},{"class":163,"line":373},9,[375,380,385,390,394],{"type":25,"tag":161,"props":376,"children":377},{"style":190},[378],{"type":34,"value":379},"        SimpleThreadExample thread ",{"type":25,"tag":161,"props":381,"children":382},{"style":168},[383],{"type":34,"value":384},"=",{"type":25,"tag":161,"props":386,"children":387},{"style":168},[388],{"type":34,"value":389}," new",{"type":25,"tag":161,"props":391,"children":392},{"style":174},[393],{"type":34,"value":177},{"type":25,"tag":161,"props":395,"children":396},{"style":190},[397],{"type":34,"value":398},"();\n",{"type":25,"tag":161,"props":400,"children":402},{"class":163,"line":401},10,[403,408,413],{"type":25,"tag":161,"props":404,"children":405},{"style":190},[406],{"type":34,"value":407},"        thread.",{"type":25,"tag":161,"props":409,"children":410},{"style":174},[411],{"type":34,"value":412},"start",{"type":25,"tag":161,"props":414,"children":415},{"style":190},[416],{"type":34,"value":398},{"type":25,"tag":161,"props":418,"children":420},{"class":163,"line":419},11,[421],{"type":25,"tag":161,"props":422,"children":423},{"style":190},[424],{"type":34,"value":281},{"type":25,"tag":161,"props":426,"children":428},{"class":163,"line":427},12,[429],{"type":25,"tag":161,"props":430,"children":431},{"style":190},[432],{"type":34,"value":290},{"type":25,"tag":434,"props":435,"children":437},"h3",{"id":436},"limitations-of-raw-threads",[438],{"type":34,"value":439},"Limitations of Raw Threads",{"type":25,"tag":49,"props":441,"children":442},{},[443,453,463],{"type":25,"tag":53,"props":444,"children":445},{},[446,451],{"type":25,"tag":57,"props":447,"children":448},{},[449],{"type":34,"value":450},"Performance Overhead",{"type":34,"value":452},": Creating and destroying threads is expensive.",{"type":25,"tag":53,"props":454,"children":455},{},[456,461],{"type":25,"tag":57,"props":457,"children":458},{},[459],{"type":34,"value":460},"No Pooling",{"type":34,"value":462},": Each thread is a one-off. No built-in reuse.",{"type":25,"tag":53,"props":464,"children":465},{},[466,471],{"type":25,"tag":57,"props":467,"children":468},{},[469],{"type":34,"value":470},"Scalability Issues",{"type":34,"value":472},": System resources limit the number of threads you can create.",{"type":25,"tag":30,"props":474,"children":475},{},[476],{"type":34,"value":477},"Using raw threads for large-scale concurrent applications quickly becomes impractical.",{"type":25,"tag":37,"props":479,"children":481},{"id":480},"modern-concurrency-executorservice-and-thread-pooling",[482],{"type":34,"value":483},"Modern Concurrency: ExecutorService and Thread Pooling",{"type":25,"tag":30,"props":485,"children":486},{},[487,489,494,496,502],{"type":34,"value":488},"To solve the limitations of raw threads, Java introduced the ",{"type":25,"tag":15,"props":490,"children":492},{"className":491},[],[493],{"type":34,"value":114},{"type":34,"value":495}," as part of ",{"type":25,"tag":15,"props":497,"children":499},{"className":498},[],[500],{"type":34,"value":501},"java.util.concurrent",{"type":34,"value":503},". It provides a high-level API to manage and reuse threads through pooling.",{"type":25,"tag":434,"props":505,"children":507},{"id":506},"how-thread-pooling-works",[508],{"type":34,"value":509},"How Thread Pooling Works",{"type":25,"tag":30,"props":511,"children":512},{},[513],{"type":34,"value":514},"Thread pooling allows you to reuse a fixed number of threads to execute multiple tasks. Instead of creating a new thread for each task, the executor assigns tasks to available worker threads in the pool.",{"type":25,"tag":152,"props":516,"children":518},{"className":154,"code":517,"language":17,"meta":8,"style":8},"import java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\n\npublic class ExecutorExample {\n    public static void main(String[] args) {\n        ExecutorService executor = Executors.newFixedThreadPool(3);\n        for (int i = 0; i \u003C 5; i++) {\n            executor.submit(() -> System.out.println(\"Task executed by: \" + Thread.currentThread().getName()));\n        }\n        executor.shutdown();\n    }\n}\n",[519],{"type":25,"tag":15,"props":520,"children":521},{"__ignoreMap":8},[522,535,547,554,574,613,650,711,777,785,802,809],{"type":25,"tag":161,"props":523,"children":524},{"class":163,"line":164},[525,530],{"type":25,"tag":161,"props":526,"children":527},{"style":168},[528],{"type":34,"value":529},"import",{"type":25,"tag":161,"props":531,"children":532},{"style":190},[533],{"type":34,"value":534}," java.util.concurrent.ExecutorService;\n",{"type":25,"tag":161,"props":536,"children":537},{"class":163,"line":196},[538,542],{"type":25,"tag":161,"props":539,"children":540},{"style":168},[541],{"type":34,"value":529},{"type":25,"tag":161,"props":543,"children":544},{"style":190},[545],{"type":34,"value":546}," java.util.concurrent.Executors;\n",{"type":25,"tag":161,"props":548,"children":549},{"class":163,"line":220},[550],{"type":25,"tag":161,"props":551,"children":552},{"emptyLinePlaceholder":297},[553],{"type":34,"value":300},{"type":25,"tag":161,"props":555,"children":556},{"class":163,"line":275},[557,561,565,570],{"type":25,"tag":161,"props":558,"children":559},{"style":168},[560],{"type":34,"value":309},{"type":25,"tag":161,"props":562,"children":563},{"style":168},[564],{"type":34,"value":314},{"type":25,"tag":161,"props":566,"children":567},{"style":174},[568],{"type":34,"value":569}," ExecutorExample",{"type":25,"tag":161,"props":571,"children":572},{"style":190},[573],{"type":34,"value":193},{"type":25,"tag":161,"props":575,"children":576},{"class":163,"line":284},[577,581,585,589,593,597,601,605,609],{"type":25,"tag":161,"props":578,"children":579},{"style":168},[580],{"type":34,"value":202},{"type":25,"tag":161,"props":582,"children":583},{"style":168},[584],{"type":34,"value":336},{"type":25,"tag":161,"props":586,"children":587},{"style":168},[588],{"type":34,"value":207},{"type":25,"tag":161,"props":590,"children":591},{"style":174},[592],{"type":34,"value":345},{"type":25,"tag":161,"props":594,"children":595},{"style":190},[596],{"type":34,"value":236},{"type":25,"tag":161,"props":598,"children":599},{"style":168},[600],{"type":34,"value":354},{"type":25,"tag":161,"props":602,"children":603},{"style":190},[604],{"type":34,"value":359},{"type":25,"tag":161,"props":606,"children":607},{"style":362},[608],{"type":34,"value":365},{"type":25,"tag":161,"props":610,"children":611},{"style":190},[612],{"type":34,"value":370},{"type":25,"tag":161,"props":614,"children":615},{"class":163,"line":293},[616,621,625,630,635,639,645],{"type":25,"tag":161,"props":617,"children":618},{"style":190},[619],{"type":34,"value":620},"        ExecutorService executor ",{"type":25,"tag":161,"props":622,"children":623},{"style":168},[624],{"type":34,"value":384},{"type":25,"tag":161,"props":626,"children":627},{"style":190},[628],{"type":34,"value":629}," Executors.",{"type":25,"tag":161,"props":631,"children":632},{"style":174},[633],{"type":34,"value":634},"newFixedThreadPool",{"type":25,"tag":161,"props":636,"children":637},{"style":190},[638],{"type":34,"value":236},{"type":25,"tag":161,"props":640,"children":642},{"style":641},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[643],{"type":34,"value":644},"3",{"type":25,"tag":161,"props":646,"children":647},{"style":190},[648],{"type":34,"value":649},");\n",{"type":25,"tag":161,"props":651,"children":652},{"class":163,"line":303},[653,658,663,668,673,677,682,687,692,697,702,707],{"type":25,"tag":161,"props":654,"children":655},{"style":168},[656],{"type":34,"value":657},"        for",{"type":25,"tag":161,"props":659,"children":660},{"style":190},[661],{"type":34,"value":662}," (",{"type":25,"tag":161,"props":664,"children":665},{"style":168},[666],{"type":34,"value":667},"int",{"type":25,"tag":161,"props":669,"children":670},{"style":190},[671],{"type":34,"value":672}," i ",{"type":25,"tag":161,"props":674,"children":675},{"style":168},[676],{"type":34,"value":384},{"type":25,"tag":161,"props":678,"children":679},{"style":641},[680],{"type":34,"value":681}," 0",{"type":25,"tag":161,"props":683,"children":684},{"style":190},[685],{"type":34,"value":686},"; i ",{"type":25,"tag":161,"props":688,"children":689},{"style":168},[690],{"type":34,"value":691},"\u003C",{"type":25,"tag":161,"props":693,"children":694},{"style":641},[695],{"type":34,"value":696}," 5",{"type":25,"tag":161,"props":698,"children":699},{"style":190},[700],{"type":34,"value":701},"; i",{"type":25,"tag":161,"props":703,"children":704},{"style":168},[705],{"type":34,"value":706},"++",{"type":25,"tag":161,"props":708,"children":709},{"style":190},[710],{"type":34,"value":370},{"type":25,"tag":161,"props":712,"children":713},{"class":163,"line":326},[714,719,724,729,734,739,743,747,752,756,760,764,768,772],{"type":25,"tag":161,"props":715,"children":716},{"style":190},[717],{"type":34,"value":718},"            executor.",{"type":25,"tag":161,"props":720,"children":721},{"style":174},[722],{"type":34,"value":723},"submit",{"type":25,"tag":161,"props":725,"children":726},{"style":190},[727],{"type":34,"value":728},"(() ",{"type":25,"tag":161,"props":730,"children":731},{"style":168},[732],{"type":34,"value":733},"->",{"type":25,"tag":161,"props":735,"children":736},{"style":190},[737],{"type":34,"value":738}," System.out.",{"type":25,"tag":161,"props":740,"children":741},{"style":174},[742],{"type":34,"value":231},{"type":25,"tag":161,"props":744,"children":745},{"style":190},[746],{"type":34,"value":236},{"type":25,"tag":161,"props":748,"children":749},{"style":239},[750],{"type":34,"value":751},"\"Task executed by: \"",{"type":25,"tag":161,"props":753,"children":754},{"style":168},[755],{"type":34,"value":247},{"type":25,"tag":161,"props":757,"children":758},{"style":190},[759],{"type":34,"value":252},{"type":25,"tag":161,"props":761,"children":762},{"style":174},[763],{"type":34,"value":257},{"type":25,"tag":161,"props":765,"children":766},{"style":190},[767],{"type":34,"value":262},{"type":25,"tag":161,"props":769,"children":770},{"style":174},[771],{"type":34,"value":267},{"type":25,"tag":161,"props":773,"children":774},{"style":190},[775],{"type":34,"value":776},"()));\n",{"type":25,"tag":161,"props":778,"children":779},{"class":163,"line":373},[780],{"type":25,"tag":161,"props":781,"children":782},{"style":190},[783],{"type":34,"value":784},"        }\n",{"type":25,"tag":161,"props":786,"children":787},{"class":163,"line":401},[788,793,798],{"type":25,"tag":161,"props":789,"children":790},{"style":190},[791],{"type":34,"value":792},"        executor.",{"type":25,"tag":161,"props":794,"children":795},{"style":174},[796],{"type":34,"value":797},"shutdown",{"type":25,"tag":161,"props":799,"children":800},{"style":190},[801],{"type":34,"value":398},{"type":25,"tag":161,"props":803,"children":804},{"class":163,"line":419},[805],{"type":25,"tag":161,"props":806,"children":807},{"style":190},[808],{"type":34,"value":281},{"type":25,"tag":161,"props":810,"children":811},{"class":163,"line":427},[812],{"type":25,"tag":161,"props":813,"children":814},{"style":190},[815],{"type":34,"value":290},{"type":25,"tag":434,"props":817,"children":819},{"id":818},"common-executor-types",[820],{"type":34,"value":821},"Common Executor Types:",{"type":25,"tag":49,"props":823,"children":824},{},[825,836,847,858],{"type":25,"tag":53,"props":826,"children":827},{},[828,834],{"type":25,"tag":15,"props":829,"children":831},{"className":830},[],[832],{"type":34,"value":833},"newFixedThreadPool(n)",{"type":34,"value":835},": Fixed number of threads.",{"type":25,"tag":53,"props":837,"children":838},{},[839,845],{"type":25,"tag":15,"props":840,"children":842},{"className":841},[],[843],{"type":34,"value":844},"newCachedThreadPool()",{"type":34,"value":846},": Dynamically creates threads as needed.",{"type":25,"tag":53,"props":848,"children":849},{},[850,856],{"type":25,"tag":15,"props":851,"children":853},{"className":852},[],[854],{"type":34,"value":855},"newSingleThreadExecutor()",{"type":34,"value":857},": One thread for all tasks.",{"type":25,"tag":53,"props":859,"children":860},{},[861,867],{"type":25,"tag":15,"props":862,"children":864},{"className":863},[],[865],{"type":34,"value":866},"newScheduledThreadPool(n)",{"type":34,"value":868},": Supports delayed and periodic tasks.",{"type":25,"tag":434,"props":870,"children":872},{"id":871},"advantages",[873],{"type":34,"value":874},"Advantages:",{"type":25,"tag":49,"props":876,"children":877},{},[878,883,888],{"type":25,"tag":53,"props":879,"children":880},{},[881],{"type":34,"value":882},"Efficient resource use through reuse.",{"type":25,"tag":53,"props":884,"children":885},{},[886],{"type":34,"value":887},"Better scalability and responsiveness.",{"type":25,"tag":53,"props":889,"children":890},{},[891],{"type":34,"value":892},"Built-in management of task queues and error handling.",{"type":25,"tag":37,"props":894,"children":896},{"id":895},"the-future-virtual-threads-and-project-loom",[897],{"type":34,"value":898},"The Future: Virtual Threads and Project Loom",{"type":25,"tag":30,"props":900,"children":901},{},[902],{"type":34,"value":903},"Java 21 introduces Virtual Threads, a lightweight and scalable alternative to platform threads. A virtual thread is managed by the Java runtime rather than the operating system, allowing millions of threads to coexist.",{"type":25,"tag":434,"props":905,"children":907},{"id":906},"key-features",[908],{"type":34,"value":909},"Key Features:",{"type":25,"tag":49,"props":911,"children":912},{},[913,923,933],{"type":25,"tag":53,"props":914,"children":915},{},[916,921],{"type":25,"tag":57,"props":917,"children":918},{},[919],{"type":34,"value":920},"Minimal Memory Footprint",{"type":34,"value":922},": Much lighter than traditional threads.",{"type":25,"tag":53,"props":924,"children":925},{},[926,931],{"type":25,"tag":57,"props":927,"children":928},{},[929],{"type":34,"value":930},"Unblocked I/O",{"type":34,"value":932},": I/O operations don’t block the underlying platform thread.",{"type":25,"tag":53,"props":934,"children":935},{},[936,941],{"type":25,"tag":57,"props":937,"children":938},{},[939],{"type":34,"value":940},"Massive Scalability",{"type":34,"value":942},": Suitable for applications requiring thousands or millions of concurrent tasks (e.g., web servers).",{"type":25,"tag":30,"props":944,"children":945},{},[946],{"type":34,"value":947},"Example:",{"type":25,"tag":152,"props":949,"children":951},{"className":154,"code":950,"language":17,"meta":8,"style":8},"public class VirtualThreadExample {\n    public static void main(String[] args) {\n        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {\n            for (int i = 0; i \u003C 10; i++) {\n                executor.submit(() -> {\n                    System.out.println(\"Running in: \" + Thread.currentThread().getName());\n                });\n            }\n        }\n    }\n}\n",[952],{"type":25,"tag":15,"props":953,"children":954},{"__ignoreMap":8},[955,975,1014,1054,1107,1131,1176,1184,1192,1199,1206],{"type":25,"tag":161,"props":956,"children":957},{"class":163,"line":164},[958,962,966,971],{"type":25,"tag":161,"props":959,"children":960},{"style":168},[961],{"type":34,"value":309},{"type":25,"tag":161,"props":963,"children":964},{"style":168},[965],{"type":34,"value":314},{"type":25,"tag":161,"props":967,"children":968},{"style":174},[969],{"type":34,"value":970}," VirtualThreadExample",{"type":25,"tag":161,"props":972,"children":973},{"style":190},[974],{"type":34,"value":193},{"type":25,"tag":161,"props":976,"children":977},{"class":163,"line":196},[978,982,986,990,994,998,1002,1006,1010],{"type":25,"tag":161,"props":979,"children":980},{"style":168},[981],{"type":34,"value":202},{"type":25,"tag":161,"props":983,"children":984},{"style":168},[985],{"type":34,"value":336},{"type":25,"tag":161,"props":987,"children":988},{"style":168},[989],{"type":34,"value":207},{"type":25,"tag":161,"props":991,"children":992},{"style":174},[993],{"type":34,"value":345},{"type":25,"tag":161,"props":995,"children":996},{"style":190},[997],{"type":34,"value":236},{"type":25,"tag":161,"props":999,"children":1000},{"style":168},[1001],{"type":34,"value":354},{"type":25,"tag":161,"props":1003,"children":1004},{"style":190},[1005],{"type":34,"value":359},{"type":25,"tag":161,"props":1007,"children":1008},{"style":362},[1009],{"type":34,"value":365},{"type":25,"tag":161,"props":1011,"children":1012},{"style":190},[1013],{"type":34,"value":370},{"type":25,"tag":161,"props":1015,"children":1016},{"class":163,"line":220},[1017,1022,1026,1031,1036,1040,1044,1049],{"type":25,"tag":161,"props":1018,"children":1019},{"style":168},[1020],{"type":34,"value":1021},"        try",{"type":25,"tag":161,"props":1023,"children":1024},{"style":190},[1025],{"type":34,"value":662},{"type":25,"tag":161,"props":1027,"children":1028},{"style":168},[1029],{"type":34,"value":1030},"var",{"type":25,"tag":161,"props":1032,"children":1033},{"style":190},[1034],{"type":34,"value":1035}," executor ",{"type":25,"tag":161,"props":1037,"children":1038},{"style":168},[1039],{"type":34,"value":384},{"type":25,"tag":161,"props":1041,"children":1042},{"style":190},[1043],{"type":34,"value":629},{"type":25,"tag":161,"props":1045,"children":1046},{"style":174},[1047],{"type":34,"value":1048},"newVirtualThreadPerTaskExecutor",{"type":25,"tag":161,"props":1050,"children":1051},{"style":190},[1052],{"type":34,"value":1053},"()) {\n",{"type":25,"tag":161,"props":1055,"children":1056},{"class":163,"line":275},[1057,1062,1066,1070,1074,1078,1082,1086,1090,1095,1099,1103],{"type":25,"tag":161,"props":1058,"children":1059},{"style":168},[1060],{"type":34,"value":1061},"            for",{"type":25,"tag":161,"props":1063,"children":1064},{"style":190},[1065],{"type":34,"value":662},{"type":25,"tag":161,"props":1067,"children":1068},{"style":168},[1069],{"type":34,"value":667},{"type":25,"tag":161,"props":1071,"children":1072},{"style":190},[1073],{"type":34,"value":672},{"type":25,"tag":161,"props":1075,"children":1076},{"style":168},[1077],{"type":34,"value":384},{"type":25,"tag":161,"props":1079,"children":1080},{"style":641},[1081],{"type":34,"value":681},{"type":25,"tag":161,"props":1083,"children":1084},{"style":190},[1085],{"type":34,"value":686},{"type":25,"tag":161,"props":1087,"children":1088},{"style":168},[1089],{"type":34,"value":691},{"type":25,"tag":161,"props":1091,"children":1092},{"style":641},[1093],{"type":34,"value":1094}," 10",{"type":25,"tag":161,"props":1096,"children":1097},{"style":190},[1098],{"type":34,"value":701},{"type":25,"tag":161,"props":1100,"children":1101},{"style":168},[1102],{"type":34,"value":706},{"type":25,"tag":161,"props":1104,"children":1105},{"style":190},[1106],{"type":34,"value":370},{"type":25,"tag":161,"props":1108,"children":1109},{"class":163,"line":284},[1110,1115,1119,1123,1127],{"type":25,"tag":161,"props":1111,"children":1112},{"style":190},[1113],{"type":34,"value":1114},"                executor.",{"type":25,"tag":161,"props":1116,"children":1117},{"style":174},[1118],{"type":34,"value":723},{"type":25,"tag":161,"props":1120,"children":1121},{"style":190},[1122],{"type":34,"value":728},{"type":25,"tag":161,"props":1124,"children":1125},{"style":168},[1126],{"type":34,"value":733},{"type":25,"tag":161,"props":1128,"children":1129},{"style":190},[1130],{"type":34,"value":193},{"type":25,"tag":161,"props":1132,"children":1133},{"class":163,"line":293},[1134,1139,1143,1147,1152,1156,1160,1164,1168,1172],{"type":25,"tag":161,"props":1135,"children":1136},{"style":190},[1137],{"type":34,"value":1138},"                    System.out.",{"type":25,"tag":161,"props":1140,"children":1141},{"style":174},[1142],{"type":34,"value":231},{"type":25,"tag":161,"props":1144,"children":1145},{"style":190},[1146],{"type":34,"value":236},{"type":25,"tag":161,"props":1148,"children":1149},{"style":239},[1150],{"type":34,"value":1151},"\"Running in: \"",{"type":25,"tag":161,"props":1153,"children":1154},{"style":168},[1155],{"type":34,"value":247},{"type":25,"tag":161,"props":1157,"children":1158},{"style":190},[1159],{"type":34,"value":252},{"type":25,"tag":161,"props":1161,"children":1162},{"style":174},[1163],{"type":34,"value":257},{"type":25,"tag":161,"props":1165,"children":1166},{"style":190},[1167],{"type":34,"value":262},{"type":25,"tag":161,"props":1169,"children":1170},{"style":174},[1171],{"type":34,"value":267},{"type":25,"tag":161,"props":1173,"children":1174},{"style":190},[1175],{"type":34,"value":272},{"type":25,"tag":161,"props":1177,"children":1178},{"class":163,"line":303},[1179],{"type":25,"tag":161,"props":1180,"children":1181},{"style":190},[1182],{"type":34,"value":1183},"                });\n",{"type":25,"tag":161,"props":1185,"children":1186},{"class":163,"line":326},[1187],{"type":25,"tag":161,"props":1188,"children":1189},{"style":190},[1190],{"type":34,"value":1191},"            }\n",{"type":25,"tag":161,"props":1193,"children":1194},{"class":163,"line":373},[1195],{"type":25,"tag":161,"props":1196,"children":1197},{"style":190},[1198],{"type":34,"value":784},{"type":25,"tag":161,"props":1200,"children":1201},{"class":163,"line":401},[1202],{"type":25,"tag":161,"props":1203,"children":1204},{"style":190},[1205],{"type":34,"value":281},{"type":25,"tag":161,"props":1207,"children":1208},{"class":163,"line":419},[1209],{"type":25,"tag":161,"props":1210,"children":1211},{"style":190},[1212],{"type":34,"value":290},{"type":25,"tag":434,"props":1214,"children":1216},{"id":1215},"when-to-use-virtual-threads",[1217],{"type":34,"value":1218},"When to Use Virtual Threads:",{"type":25,"tag":49,"props":1220,"children":1221},{},[1222,1227,1232],{"type":25,"tag":53,"props":1223,"children":1224},{},[1225],{"type":34,"value":1226},"I/O-bound applications (e.g., database access, HTTP clients).",{"type":25,"tag":53,"props":1228,"children":1229},{},[1230],{"type":34,"value":1231},"High-concurrency workloads (e.g., messaging systems).",{"type":25,"tag":53,"props":1233,"children":1234},{},[1235],{"type":34,"value":1236},"When simplifying thread management is a priority.",{"type":25,"tag":37,"props":1238,"children":1240},{"id":1239},"conclusion",[1241],{"type":34,"value":1242},"Conclusion",{"type":25,"tag":30,"props":1244,"children":1245},{},[1246],{"type":34,"value":1247},"Java offers a flexible and powerful concurrency model. Classic Threads provide a foundational understanding, ExecutorService enables efficient thread reuse and task management, and Virtual Threads open the door to a new era of scalable concurrency with minimal overhead.",{"type":25,"tag":30,"props":1249,"children":1250},{},[1251],{"type":34,"value":1252},"Each model has its place depending on the problem at hand. Mastering them gives you the toolkit needed to build high-performance, concurrent applications.",{"type":25,"tag":37,"props":1254,"children":1256},{"id":1255},"further-reading",[1257],{"type":34,"value":1258},"Further Reading",{"type":25,"tag":49,"props":1260,"children":1261},{},[1262,1274,1284,1293],{"type":25,"tag":53,"props":1263,"children":1264},{},[1265],{"type":25,"tag":1266,"props":1267,"children":1271},"a",{"href":1268,"rel":1269},"https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html",[1270],"nofollow",[1272],{"type":34,"value":1273},"Java Concurrency API",{"type":25,"tag":53,"props":1275,"children":1276},{},[1277],{"type":25,"tag":1266,"props":1278,"children":1281},{"href":1279,"rel":1280},"https://openjdk.org/projects/loom/",[1270],[1282],{"type":34,"value":1283},"Project Loom (Virtual Threads)",{"type":25,"tag":53,"props":1285,"children":1286},{},[1287],{"type":25,"tag":1266,"props":1288,"children":1291},{"href":1289,"rel":1290},"https://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html",[1270],[1292],{"type":34,"value":91},{"type":25,"tag":53,"props":1294,"children":1295},{},[1296],{"type":25,"tag":1266,"props":1297,"children":1300},{"href":1298,"rel":1299},"https://www.oreilly.com/library/view/effective-java-3rd/9780134686097/",[1270],[1301],{"type":34,"value":1302},"Effective Java - Joshua Bloch",{"type":25,"tag":30,"props":1304,"children":1305},{},[1306],{"type":34,"value":1307},"Deep understanding of Java's concurrency tools can significantly improve your system’s performance, scalability, and maintainability.",{"type":25,"tag":1309,"props":1310,"children":1311},"style",{},[1312],{"type":34,"value":1313},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":8,"searchDepth":196,"depth":196,"links":1315},[1316,1317,1320,1325,1329,1330],{"id":39,"depth":196,"text":42},{"id":119,"depth":196,"text":122,"children":1318},[1319],{"id":436,"depth":220,"text":439},{"id":480,"depth":196,"text":483,"children":1321},[1322,1323,1324],{"id":506,"depth":220,"text":509},{"id":818,"depth":220,"text":821},{"id":871,"depth":220,"text":874},{"id":895,"depth":196,"text":898,"children":1326},[1327,1328],{"id":906,"depth":220,"text":909},{"id":1215,"depth":220,"text":1218},{"id":1239,"depth":196,"text":1242},{"id":1255,"depth":196,"text":1258},"markdown","content:posts:mastering-concurrency.md","content","posts/mastering-concurrency.md","posts/mastering-concurrency","md",1790510065083]