[{"data":1,"prerenderedAt":3129},["ShallowReactive",2],{"tag-concurrency":3},[4,1874],{"_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":21,"paginate":7,"body":22,"_type":1868,"_id":1869,"_source":1870,"_file":1871,"_stem":1872,"_extension":1873},"/posts/async-java-completable-vs-reactive","posts",false,"","Asynchronous Programming in Java: CompletableFuture vs Reactive Streams","A deep dive into asynchronous programming in Java comparing CompletableFuture and Reactive Streams, with examples and output.","2024-04-15T11:00:00.000Z","post","Choosing the Right Tool for Concurrency in Modern Java","/assets/img/uploads/java-async.png","code",[15,17,18,19,20],"java","asynchronous","reactive","concurrency","jaimedearcos",{"type":23,"children":24,"toc":1854},"root",[25,53,60,70,77,469,477,485,491,778,785,793,799,807,827,835,853,857,863,898,904,1209,1216,1224,1230,1564,1571,1579,1585,1592,1610,1617,1635,1638,1644,1773,1776,1782,1801,1806,1813,1848],{"type":26,"tag":27,"props":28,"children":29},"element","blockquote",{},[30],{"type":26,"tag":31,"props":32,"children":33},"p",{},[34,37,43,45,51],{"type":35,"value":36},"text","Asynchronous programming enables applications to execute non-blocking operations, crucial for performance in modern, concurrent systems. In Java, two powerful tools are commonly used: ",{"type":26,"tag":15,"props":38,"children":40},{"className":39},[],[41],{"type":35,"value":42},"CompletableFuture",{"type":35,"value":44}," and ",{"type":26,"tag":46,"props":47,"children":48},"strong",{},[49],{"type":35,"value":50},"Reactive Streams",{"type":35,"value":52}," (e.g., Project Reactor or RxJava). In this post, we’ll compare both approaches through real code examples and output results.",{"type":26,"tag":54,"props":55,"children":57},"h2",{"id":56},"_1-completablefuture-javas-built-in-promise-api",[58],{"type":35,"value":59},"1. CompletableFuture: Java’s Built-in Promise API",{"type":26,"tag":31,"props":61,"children":62},{},[63,68],{"type":26,"tag":15,"props":64,"children":66},{"className":65},[],[67],{"type":35,"value":42},{"type":35,"value":69}," is a class introduced in Java 8 to write asynchronous, non-blocking code. It's suitable for simple async flows and integrates well with imperative codebases.",{"type":26,"tag":71,"props":72,"children":74},"h3",{"id":73},"example-1-basic-async-execution",[75],{"type":35,"value":76},"Example 1: Basic Async Execution",{"type":26,"tag":78,"props":79,"children":82},"pre",{"code":80,"language":17,"meta":8,"className":81,"style":8},"import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureDemo {\n    public static void main(String[] args) {\n        CompletableFuture\u003CString> future = CompletableFuture.supplyAsync(() -> {\n            sleep(1000);\n            return \"Hello from CompletableFuture!\";\n        });\n\n        future.thenAccept(System.out::println);\n        sleep(1500); // wait for completion\n    }\n\n    static void sleep(long millis) {\n        try { Thread.sleep(millis); } catch (InterruptedException e) { }\n    }\n}\n","language-java shiki shiki-themes github-dark github-light",[83],{"type":26,"tag":15,"props":84,"children":85},{"__ignoreMap":8},[86,104,114,139,189,236,260,280,289,297,326,355,364,372,408,452,460],{"type":26,"tag":87,"props":88,"children":91},"span",{"class":89,"line":90},"line",1,[92,98],{"type":26,"tag":87,"props":93,"children":95},{"style":94},"--shiki-default:#F97583;--shiki-light:#D73A49",[96],{"type":35,"value":97},"import",{"type":26,"tag":87,"props":99,"children":101},{"style":100},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[102],{"type":35,"value":103}," java.util.concurrent.CompletableFuture;\n",{"type":26,"tag":87,"props":105,"children":107},{"class":89,"line":106},2,[108],{"type":26,"tag":87,"props":109,"children":111},{"emptyLinePlaceholder":110},true,[112],{"type":35,"value":113},"\n",{"type":26,"tag":87,"props":115,"children":117},{"class":89,"line":116},3,[118,123,128,134],{"type":26,"tag":87,"props":119,"children":120},{"style":94},[121],{"type":35,"value":122},"public",{"type":26,"tag":87,"props":124,"children":125},{"style":94},[126],{"type":35,"value":127}," class",{"type":26,"tag":87,"props":129,"children":131},{"style":130},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[132],{"type":35,"value":133}," CompletableFutureDemo",{"type":26,"tag":87,"props":135,"children":136},{"style":100},[137],{"type":35,"value":138}," {\n",{"type":26,"tag":87,"props":140,"children":142},{"class":89,"line":141},4,[143,148,153,158,163,168,173,178,184],{"type":26,"tag":87,"props":144,"children":145},{"style":94},[146],{"type":35,"value":147},"    public",{"type":26,"tag":87,"props":149,"children":150},{"style":94},[151],{"type":35,"value":152}," static",{"type":26,"tag":87,"props":154,"children":155},{"style":94},[156],{"type":35,"value":157}," void",{"type":26,"tag":87,"props":159,"children":160},{"style":130},[161],{"type":35,"value":162}," main",{"type":26,"tag":87,"props":164,"children":165},{"style":100},[166],{"type":35,"value":167},"(",{"type":26,"tag":87,"props":169,"children":170},{"style":94},[171],{"type":35,"value":172},"String",{"type":26,"tag":87,"props":174,"children":175},{"style":100},[176],{"type":35,"value":177},"[] ",{"type":26,"tag":87,"props":179,"children":181},{"style":180},"--shiki-default:#FFAB70;--shiki-light:#E36209",[182],{"type":35,"value":183},"args",{"type":26,"tag":87,"props":185,"children":186},{"style":100},[187],{"type":35,"value":188},") {\n",{"type":26,"tag":87,"props":190,"children":192},{"class":89,"line":191},5,[193,198,202,207,212,217,222,227,232],{"type":26,"tag":87,"props":194,"children":195},{"style":100},[196],{"type":35,"value":197},"        CompletableFuture\u003C",{"type":26,"tag":87,"props":199,"children":200},{"style":94},[201],{"type":35,"value":172},{"type":26,"tag":87,"props":203,"children":204},{"style":100},[205],{"type":35,"value":206},"> future ",{"type":26,"tag":87,"props":208,"children":209},{"style":94},[210],{"type":35,"value":211},"=",{"type":26,"tag":87,"props":213,"children":214},{"style":100},[215],{"type":35,"value":216}," CompletableFuture.",{"type":26,"tag":87,"props":218,"children":219},{"style":130},[220],{"type":35,"value":221},"supplyAsync",{"type":26,"tag":87,"props":223,"children":224},{"style":100},[225],{"type":35,"value":226},"(() ",{"type":26,"tag":87,"props":228,"children":229},{"style":94},[230],{"type":35,"value":231},"->",{"type":26,"tag":87,"props":233,"children":234},{"style":100},[235],{"type":35,"value":138},{"type":26,"tag":87,"props":237,"children":239},{"class":89,"line":238},6,[240,245,249,255],{"type":26,"tag":87,"props":241,"children":242},{"style":130},[243],{"type":35,"value":244},"            sleep",{"type":26,"tag":87,"props":246,"children":247},{"style":100},[248],{"type":35,"value":167},{"type":26,"tag":87,"props":250,"children":252},{"style":251},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[253],{"type":35,"value":254},"1000",{"type":26,"tag":87,"props":256,"children":257},{"style":100},[258],{"type":35,"value":259},");\n",{"type":26,"tag":87,"props":261,"children":263},{"class":89,"line":262},7,[264,269,275],{"type":26,"tag":87,"props":265,"children":266},{"style":94},[267],{"type":35,"value":268},"            return",{"type":26,"tag":87,"props":270,"children":272},{"style":271},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[273],{"type":35,"value":274}," \"Hello from CompletableFuture!\"",{"type":26,"tag":87,"props":276,"children":277},{"style":100},[278],{"type":35,"value":279},";\n",{"type":26,"tag":87,"props":281,"children":283},{"class":89,"line":282},8,[284],{"type":26,"tag":87,"props":285,"children":286},{"style":100},[287],{"type":35,"value":288},"        });\n",{"type":26,"tag":87,"props":290,"children":292},{"class":89,"line":291},9,[293],{"type":26,"tag":87,"props":294,"children":295},{"emptyLinePlaceholder":110},[296],{"type":35,"value":113},{"type":26,"tag":87,"props":298,"children":300},{"class":89,"line":299},10,[301,306,311,316,321],{"type":26,"tag":87,"props":302,"children":303},{"style":100},[304],{"type":35,"value":305},"        future.",{"type":26,"tag":87,"props":307,"children":308},{"style":130},[309],{"type":35,"value":310},"thenAccept",{"type":26,"tag":87,"props":312,"children":313},{"style":100},[314],{"type":35,"value":315},"(System.out",{"type":26,"tag":87,"props":317,"children":318},{"style":94},[319],{"type":35,"value":320},"::",{"type":26,"tag":87,"props":322,"children":323},{"style":100},[324],{"type":35,"value":325},"println);\n",{"type":26,"tag":87,"props":327,"children":329},{"class":89,"line":328},11,[330,335,339,344,349],{"type":26,"tag":87,"props":331,"children":332},{"style":130},[333],{"type":35,"value":334},"        sleep",{"type":26,"tag":87,"props":336,"children":337},{"style":100},[338],{"type":35,"value":167},{"type":26,"tag":87,"props":340,"children":341},{"style":251},[342],{"type":35,"value":343},"1500",{"type":26,"tag":87,"props":345,"children":346},{"style":100},[347],{"type":35,"value":348},"); ",{"type":26,"tag":87,"props":350,"children":352},{"style":351},"--shiki-default:#6A737D;--shiki-light:#6A737D",[353],{"type":35,"value":354},"// wait for completion\n",{"type":26,"tag":87,"props":356,"children":358},{"class":89,"line":357},12,[359],{"type":26,"tag":87,"props":360,"children":361},{"style":100},[362],{"type":35,"value":363},"    }\n",{"type":26,"tag":87,"props":365,"children":367},{"class":89,"line":366},13,[368],{"type":26,"tag":87,"props":369,"children":370},{"emptyLinePlaceholder":110},[371],{"type":35,"value":113},{"type":26,"tag":87,"props":373,"children":375},{"class":89,"line":374},14,[376,381,385,390,394,399,404],{"type":26,"tag":87,"props":377,"children":378},{"style":94},[379],{"type":35,"value":380},"    static",{"type":26,"tag":87,"props":382,"children":383},{"style":94},[384],{"type":35,"value":157},{"type":26,"tag":87,"props":386,"children":387},{"style":130},[388],{"type":35,"value":389}," sleep",{"type":26,"tag":87,"props":391,"children":392},{"style":100},[393],{"type":35,"value":167},{"type":26,"tag":87,"props":395,"children":396},{"style":94},[397],{"type":35,"value":398},"long",{"type":26,"tag":87,"props":400,"children":401},{"style":180},[402],{"type":35,"value":403}," millis",{"type":26,"tag":87,"props":405,"children":406},{"style":100},[407],{"type":35,"value":188},{"type":26,"tag":87,"props":409,"children":411},{"class":89,"line":410},15,[412,417,422,427,432,437,442,447],{"type":26,"tag":87,"props":413,"children":414},{"style":94},[415],{"type":35,"value":416},"        try",{"type":26,"tag":87,"props":418,"children":419},{"style":100},[420],{"type":35,"value":421}," { Thread.",{"type":26,"tag":87,"props":423,"children":424},{"style":130},[425],{"type":35,"value":426},"sleep",{"type":26,"tag":87,"props":428,"children":429},{"style":100},[430],{"type":35,"value":431},"(millis); } ",{"type":26,"tag":87,"props":433,"children":434},{"style":94},[435],{"type":35,"value":436},"catch",{"type":26,"tag":87,"props":438,"children":439},{"style":100},[440],{"type":35,"value":441}," (InterruptedException ",{"type":26,"tag":87,"props":443,"children":444},{"style":180},[445],{"type":35,"value":446},"e",{"type":26,"tag":87,"props":448,"children":449},{"style":100},[450],{"type":35,"value":451},") { }\n",{"type":26,"tag":87,"props":453,"children":455},{"class":89,"line":454},16,[456],{"type":26,"tag":87,"props":457,"children":458},{"style":100},[459],{"type":35,"value":363},{"type":26,"tag":87,"props":461,"children":463},{"class":89,"line":462},17,[464],{"type":26,"tag":87,"props":465,"children":466},{"style":100},[467],{"type":35,"value":468},"}\n",{"type":26,"tag":31,"props":470,"children":471},{},[472],{"type":26,"tag":46,"props":473,"children":474},{},[475],{"type":35,"value":476},"Output:",{"type":26,"tag":78,"props":478,"children":480},{"code":479},"Hello from CompletableFuture!\n",[481],{"type":26,"tag":15,"props":482,"children":483},{"__ignoreMap":8},[484],{"type":35,"value":479},{"type":26,"tag":71,"props":486,"children":488},{"id":487},"example-2-chaining-and-combining",[489],{"type":35,"value":490},"Example 2: Chaining and Combining",{"type":26,"tag":78,"props":492,"children":494},{"code":493,"language":17,"meta":8,"className":81,"style":8},"import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureCombineDemo {\n    public static void main(String[] args) {\n        CompletableFuture\u003CString> greeting = CompletableFuture.supplyAsync(() -> \"Hello\");\n        CompletableFuture\u003CString> name = CompletableFuture.supplyAsync(() -> \"World\");\n\n        CompletableFuture\u003CString> result = greeting.thenCombine(name, (g, n) -> g + \", \" + n);\n        System.out.println(result.join());\n    }\n}\n",[495],{"type":26,"tag":15,"props":496,"children":497},{"__ignoreMap":8},[498,509,516,536,575,620,665,672,736,764,771],{"type":26,"tag":87,"props":499,"children":500},{"class":89,"line":90},[501,505],{"type":26,"tag":87,"props":502,"children":503},{"style":94},[504],{"type":35,"value":97},{"type":26,"tag":87,"props":506,"children":507},{"style":100},[508],{"type":35,"value":103},{"type":26,"tag":87,"props":510,"children":511},{"class":89,"line":106},[512],{"type":26,"tag":87,"props":513,"children":514},{"emptyLinePlaceholder":110},[515],{"type":35,"value":113},{"type":26,"tag":87,"props":517,"children":518},{"class":89,"line":116},[519,523,527,532],{"type":26,"tag":87,"props":520,"children":521},{"style":94},[522],{"type":35,"value":122},{"type":26,"tag":87,"props":524,"children":525},{"style":94},[526],{"type":35,"value":127},{"type":26,"tag":87,"props":528,"children":529},{"style":130},[530],{"type":35,"value":531}," CompletableFutureCombineDemo",{"type":26,"tag":87,"props":533,"children":534},{"style":100},[535],{"type":35,"value":138},{"type":26,"tag":87,"props":537,"children":538},{"class":89,"line":141},[539,543,547,551,555,559,563,567,571],{"type":26,"tag":87,"props":540,"children":541},{"style":94},[542],{"type":35,"value":147},{"type":26,"tag":87,"props":544,"children":545},{"style":94},[546],{"type":35,"value":152},{"type":26,"tag":87,"props":548,"children":549},{"style":94},[550],{"type":35,"value":157},{"type":26,"tag":87,"props":552,"children":553},{"style":130},[554],{"type":35,"value":162},{"type":26,"tag":87,"props":556,"children":557},{"style":100},[558],{"type":35,"value":167},{"type":26,"tag":87,"props":560,"children":561},{"style":94},[562],{"type":35,"value":172},{"type":26,"tag":87,"props":564,"children":565},{"style":100},[566],{"type":35,"value":177},{"type":26,"tag":87,"props":568,"children":569},{"style":180},[570],{"type":35,"value":183},{"type":26,"tag":87,"props":572,"children":573},{"style":100},[574],{"type":35,"value":188},{"type":26,"tag":87,"props":576,"children":577},{"class":89,"line":191},[578,582,586,591,595,599,603,607,611,616],{"type":26,"tag":87,"props":579,"children":580},{"style":100},[581],{"type":35,"value":197},{"type":26,"tag":87,"props":583,"children":584},{"style":94},[585],{"type":35,"value":172},{"type":26,"tag":87,"props":587,"children":588},{"style":100},[589],{"type":35,"value":590},"> greeting ",{"type":26,"tag":87,"props":592,"children":593},{"style":94},[594],{"type":35,"value":211},{"type":26,"tag":87,"props":596,"children":597},{"style":100},[598],{"type":35,"value":216},{"type":26,"tag":87,"props":600,"children":601},{"style":130},[602],{"type":35,"value":221},{"type":26,"tag":87,"props":604,"children":605},{"style":100},[606],{"type":35,"value":226},{"type":26,"tag":87,"props":608,"children":609},{"style":94},[610],{"type":35,"value":231},{"type":26,"tag":87,"props":612,"children":613},{"style":271},[614],{"type":35,"value":615}," \"Hello\"",{"type":26,"tag":87,"props":617,"children":618},{"style":100},[619],{"type":35,"value":259},{"type":26,"tag":87,"props":621,"children":622},{"class":89,"line":238},[623,627,631,636,640,644,648,652,656,661],{"type":26,"tag":87,"props":624,"children":625},{"style":100},[626],{"type":35,"value":197},{"type":26,"tag":87,"props":628,"children":629},{"style":94},[630],{"type":35,"value":172},{"type":26,"tag":87,"props":632,"children":633},{"style":100},[634],{"type":35,"value":635},"> name ",{"type":26,"tag":87,"props":637,"children":638},{"style":94},[639],{"type":35,"value":211},{"type":26,"tag":87,"props":641,"children":642},{"style":100},[643],{"type":35,"value":216},{"type":26,"tag":87,"props":645,"children":646},{"style":130},[647],{"type":35,"value":221},{"type":26,"tag":87,"props":649,"children":650},{"style":100},[651],{"type":35,"value":226},{"type":26,"tag":87,"props":653,"children":654},{"style":94},[655],{"type":35,"value":231},{"type":26,"tag":87,"props":657,"children":658},{"style":271},[659],{"type":35,"value":660}," \"World\"",{"type":26,"tag":87,"props":662,"children":663},{"style":100},[664],{"type":35,"value":259},{"type":26,"tag":87,"props":666,"children":667},{"class":89,"line":262},[668],{"type":26,"tag":87,"props":669,"children":670},{"emptyLinePlaceholder":110},[671],{"type":35,"value":113},{"type":26,"tag":87,"props":673,"children":674},{"class":89,"line":282},[675,679,683,688,692,697,702,707,711,716,721,726,731],{"type":26,"tag":87,"props":676,"children":677},{"style":100},[678],{"type":35,"value":197},{"type":26,"tag":87,"props":680,"children":681},{"style":94},[682],{"type":35,"value":172},{"type":26,"tag":87,"props":684,"children":685},{"style":100},[686],{"type":35,"value":687},"> result ",{"type":26,"tag":87,"props":689,"children":690},{"style":94},[691],{"type":35,"value":211},{"type":26,"tag":87,"props":693,"children":694},{"style":100},[695],{"type":35,"value":696}," greeting.",{"type":26,"tag":87,"props":698,"children":699},{"style":130},[700],{"type":35,"value":701},"thenCombine",{"type":26,"tag":87,"props":703,"children":704},{"style":100},[705],{"type":35,"value":706},"(name, (g, n) ",{"type":26,"tag":87,"props":708,"children":709},{"style":94},[710],{"type":35,"value":231},{"type":26,"tag":87,"props":712,"children":713},{"style":100},[714],{"type":35,"value":715}," g ",{"type":26,"tag":87,"props":717,"children":718},{"style":94},[719],{"type":35,"value":720},"+",{"type":26,"tag":87,"props":722,"children":723},{"style":271},[724],{"type":35,"value":725}," \", \"",{"type":26,"tag":87,"props":727,"children":728},{"style":94},[729],{"type":35,"value":730}," +",{"type":26,"tag":87,"props":732,"children":733},{"style":100},[734],{"type":35,"value":735}," n);\n",{"type":26,"tag":87,"props":737,"children":738},{"class":89,"line":291},[739,744,749,754,759],{"type":26,"tag":87,"props":740,"children":741},{"style":100},[742],{"type":35,"value":743},"        System.out.",{"type":26,"tag":87,"props":745,"children":746},{"style":130},[747],{"type":35,"value":748},"println",{"type":26,"tag":87,"props":750,"children":751},{"style":100},[752],{"type":35,"value":753},"(result.",{"type":26,"tag":87,"props":755,"children":756},{"style":130},[757],{"type":35,"value":758},"join",{"type":26,"tag":87,"props":760,"children":761},{"style":100},[762],{"type":35,"value":763},"());\n",{"type":26,"tag":87,"props":765,"children":766},{"class":89,"line":299},[767],{"type":26,"tag":87,"props":768,"children":769},{"style":100},[770],{"type":35,"value":363},{"type":26,"tag":87,"props":772,"children":773},{"class":89,"line":328},[774],{"type":26,"tag":87,"props":775,"children":776},{"style":100},[777],{"type":35,"value":468},{"type":26,"tag":31,"props":779,"children":780},{},[781],{"type":26,"tag":46,"props":782,"children":783},{},[784],{"type":35,"value":476},{"type":26,"tag":78,"props":786,"children":788},{"code":787},"Hello, World\n",[789],{"type":26,"tag":15,"props":790,"children":791},{"__ignoreMap":8},[792],{"type":35,"value":787},{"type":26,"tag":71,"props":794,"children":796},{"id":795},"pros-and-cons-of-completablefuture",[797],{"type":35,"value":798},"Pros and Cons of CompletableFuture",{"type":26,"tag":31,"props":800,"children":801},{},[802],{"type":26,"tag":46,"props":803,"children":804},{},[805],{"type":35,"value":806},"Pros:",{"type":26,"tag":808,"props":809,"children":810},"ul",{},[811,817,822],{"type":26,"tag":812,"props":813,"children":814},"li",{},[815],{"type":35,"value":816},"Native Java API, no dependencies",{"type":26,"tag":812,"props":818,"children":819},{},[820],{"type":35,"value":821},"Familiar to imperative developers",{"type":26,"tag":812,"props":823,"children":824},{},[825],{"type":35,"value":826},"Easy for linear async flows",{"type":26,"tag":31,"props":828,"children":829},{},[830],{"type":26,"tag":46,"props":831,"children":832},{},[833],{"type":35,"value":834},"Cons:",{"type":26,"tag":808,"props":836,"children":837},{},[838,843,848],{"type":26,"tag":812,"props":839,"children":840},{},[841],{"type":35,"value":842},"Limited operators compared to reactive libraries",{"type":26,"tag":812,"props":844,"children":845},{},[846],{"type":35,"value":847},"Complex error handling in larger flows",{"type":26,"tag":812,"props":849,"children":850},{},[851],{"type":35,"value":852},"Not suitable for high-throughput event streams",{"type":26,"tag":854,"props":855,"children":856},"hr",{},[],{"type":26,"tag":54,"props":858,"children":860},{"id":859},"_2-reactive-streams-declarative-and-event-driven",[861],{"type":35,"value":862},"2. Reactive Streams: Declarative and Event-Driven",{"type":26,"tag":31,"props":864,"children":865},{},[866,868,873,875,881,883,889,891,896],{"type":35,"value":867},"Reactive programming models async execution as a stream of data. With libraries like ",{"type":26,"tag":46,"props":869,"children":870},{},[871],{"type":35,"value":872},"Project Reactor",{"type":35,"value":874}," (",{"type":26,"tag":15,"props":876,"children":878},{"className":877},[],[879],{"type":35,"value":880},"Mono",{"type":35,"value":882},", ",{"type":26,"tag":15,"props":884,"children":886},{"className":885},[],[887],{"type":35,"value":888},"Flux",{"type":35,"value":890},") or ",{"type":26,"tag":46,"props":892,"children":893},{},[894],{"type":35,"value":895},"RxJava",{"type":35,"value":897},", you can build pipelines to transform, filter, and combine async data.",{"type":26,"tag":71,"props":899,"children":901},{"id":900},"example-1-mono-single-async-value",[902],{"type":35,"value":903},"Example 1: Mono (Single Async Value)",{"type":26,"tag":78,"props":905,"children":907},{"code":906,"language":17,"meta":8,"className":81,"style":8},"import reactor.core.publisher.Mono;\nimport java.time.Duration;\n\npublic class ReactorMonoExample {\n    public static void main(String[] args) {\n        Mono.just(\"Hello from Mono!\")\n            .delayElement(Duration.ofMillis(500))\n            .subscribe(System.out::println);\n\n        sleep(1000);\n    }\n\n    static void sleep(long millis) {\n        try { Thread.sleep(millis); } catch (InterruptedException e) { }\n    }\n}\n",[908],{"type":26,"tag":15,"props":909,"children":910},{"__ignoreMap":8},[911,923,935,942,962,1001,1028,1065,1089,1096,1115,1122,1129,1160,1195,1202],{"type":26,"tag":87,"props":912,"children":913},{"class":89,"line":90},[914,918],{"type":26,"tag":87,"props":915,"children":916},{"style":94},[917],{"type":35,"value":97},{"type":26,"tag":87,"props":919,"children":920},{"style":100},[921],{"type":35,"value":922}," reactor.core.publisher.Mono;\n",{"type":26,"tag":87,"props":924,"children":925},{"class":89,"line":106},[926,930],{"type":26,"tag":87,"props":927,"children":928},{"style":94},[929],{"type":35,"value":97},{"type":26,"tag":87,"props":931,"children":932},{"style":100},[933],{"type":35,"value":934}," java.time.Duration;\n",{"type":26,"tag":87,"props":936,"children":937},{"class":89,"line":116},[938],{"type":26,"tag":87,"props":939,"children":940},{"emptyLinePlaceholder":110},[941],{"type":35,"value":113},{"type":26,"tag":87,"props":943,"children":944},{"class":89,"line":141},[945,949,953,958],{"type":26,"tag":87,"props":946,"children":947},{"style":94},[948],{"type":35,"value":122},{"type":26,"tag":87,"props":950,"children":951},{"style":94},[952],{"type":35,"value":127},{"type":26,"tag":87,"props":954,"children":955},{"style":130},[956],{"type":35,"value":957}," ReactorMonoExample",{"type":26,"tag":87,"props":959,"children":960},{"style":100},[961],{"type":35,"value":138},{"type":26,"tag":87,"props":963,"children":964},{"class":89,"line":191},[965,969,973,977,981,985,989,993,997],{"type":26,"tag":87,"props":966,"children":967},{"style":94},[968],{"type":35,"value":147},{"type":26,"tag":87,"props":970,"children":971},{"style":94},[972],{"type":35,"value":152},{"type":26,"tag":87,"props":974,"children":975},{"style":94},[976],{"type":35,"value":157},{"type":26,"tag":87,"props":978,"children":979},{"style":130},[980],{"type":35,"value":162},{"type":26,"tag":87,"props":982,"children":983},{"style":100},[984],{"type":35,"value":167},{"type":26,"tag":87,"props":986,"children":987},{"style":94},[988],{"type":35,"value":172},{"type":26,"tag":87,"props":990,"children":991},{"style":100},[992],{"type":35,"value":177},{"type":26,"tag":87,"props":994,"children":995},{"style":180},[996],{"type":35,"value":183},{"type":26,"tag":87,"props":998,"children":999},{"style":100},[1000],{"type":35,"value":188},{"type":26,"tag":87,"props":1002,"children":1003},{"class":89,"line":238},[1004,1009,1014,1018,1023],{"type":26,"tag":87,"props":1005,"children":1006},{"style":100},[1007],{"type":35,"value":1008},"        Mono.",{"type":26,"tag":87,"props":1010,"children":1011},{"style":130},[1012],{"type":35,"value":1013},"just",{"type":26,"tag":87,"props":1015,"children":1016},{"style":100},[1017],{"type":35,"value":167},{"type":26,"tag":87,"props":1019,"children":1020},{"style":271},[1021],{"type":35,"value":1022},"\"Hello from Mono!\"",{"type":26,"tag":87,"props":1024,"children":1025},{"style":100},[1026],{"type":35,"value":1027},")\n",{"type":26,"tag":87,"props":1029,"children":1030},{"class":89,"line":262},[1031,1036,1041,1046,1051,1055,1060],{"type":26,"tag":87,"props":1032,"children":1033},{"style":100},[1034],{"type":35,"value":1035},"            .",{"type":26,"tag":87,"props":1037,"children":1038},{"style":130},[1039],{"type":35,"value":1040},"delayElement",{"type":26,"tag":87,"props":1042,"children":1043},{"style":100},[1044],{"type":35,"value":1045},"(Duration.",{"type":26,"tag":87,"props":1047,"children":1048},{"style":130},[1049],{"type":35,"value":1050},"ofMillis",{"type":26,"tag":87,"props":1052,"children":1053},{"style":100},[1054],{"type":35,"value":167},{"type":26,"tag":87,"props":1056,"children":1057},{"style":251},[1058],{"type":35,"value":1059},"500",{"type":26,"tag":87,"props":1061,"children":1062},{"style":100},[1063],{"type":35,"value":1064},"))\n",{"type":26,"tag":87,"props":1066,"children":1067},{"class":89,"line":282},[1068,1072,1077,1081,1085],{"type":26,"tag":87,"props":1069,"children":1070},{"style":100},[1071],{"type":35,"value":1035},{"type":26,"tag":87,"props":1073,"children":1074},{"style":130},[1075],{"type":35,"value":1076},"subscribe",{"type":26,"tag":87,"props":1078,"children":1079},{"style":100},[1080],{"type":35,"value":315},{"type":26,"tag":87,"props":1082,"children":1083},{"style":94},[1084],{"type":35,"value":320},{"type":26,"tag":87,"props":1086,"children":1087},{"style":100},[1088],{"type":35,"value":325},{"type":26,"tag":87,"props":1090,"children":1091},{"class":89,"line":291},[1092],{"type":26,"tag":87,"props":1093,"children":1094},{"emptyLinePlaceholder":110},[1095],{"type":35,"value":113},{"type":26,"tag":87,"props":1097,"children":1098},{"class":89,"line":299},[1099,1103,1107,1111],{"type":26,"tag":87,"props":1100,"children":1101},{"style":130},[1102],{"type":35,"value":334},{"type":26,"tag":87,"props":1104,"children":1105},{"style":100},[1106],{"type":35,"value":167},{"type":26,"tag":87,"props":1108,"children":1109},{"style":251},[1110],{"type":35,"value":254},{"type":26,"tag":87,"props":1112,"children":1113},{"style":100},[1114],{"type":35,"value":259},{"type":26,"tag":87,"props":1116,"children":1117},{"class":89,"line":328},[1118],{"type":26,"tag":87,"props":1119,"children":1120},{"style":100},[1121],{"type":35,"value":363},{"type":26,"tag":87,"props":1123,"children":1124},{"class":89,"line":357},[1125],{"type":26,"tag":87,"props":1126,"children":1127},{"emptyLinePlaceholder":110},[1128],{"type":35,"value":113},{"type":26,"tag":87,"props":1130,"children":1131},{"class":89,"line":366},[1132,1136,1140,1144,1148,1152,1156],{"type":26,"tag":87,"props":1133,"children":1134},{"style":94},[1135],{"type":35,"value":380},{"type":26,"tag":87,"props":1137,"children":1138},{"style":94},[1139],{"type":35,"value":157},{"type":26,"tag":87,"props":1141,"children":1142},{"style":130},[1143],{"type":35,"value":389},{"type":26,"tag":87,"props":1145,"children":1146},{"style":100},[1147],{"type":35,"value":167},{"type":26,"tag":87,"props":1149,"children":1150},{"style":94},[1151],{"type":35,"value":398},{"type":26,"tag":87,"props":1153,"children":1154},{"style":180},[1155],{"type":35,"value":403},{"type":26,"tag":87,"props":1157,"children":1158},{"style":100},[1159],{"type":35,"value":188},{"type":26,"tag":87,"props":1161,"children":1162},{"class":89,"line":374},[1163,1167,1171,1175,1179,1183,1187,1191],{"type":26,"tag":87,"props":1164,"children":1165},{"style":94},[1166],{"type":35,"value":416},{"type":26,"tag":87,"props":1168,"children":1169},{"style":100},[1170],{"type":35,"value":421},{"type":26,"tag":87,"props":1172,"children":1173},{"style":130},[1174],{"type":35,"value":426},{"type":26,"tag":87,"props":1176,"children":1177},{"style":100},[1178],{"type":35,"value":431},{"type":26,"tag":87,"props":1180,"children":1181},{"style":94},[1182],{"type":35,"value":436},{"type":26,"tag":87,"props":1184,"children":1185},{"style":100},[1186],{"type":35,"value":441},{"type":26,"tag":87,"props":1188,"children":1189},{"style":180},[1190],{"type":35,"value":446},{"type":26,"tag":87,"props":1192,"children":1193},{"style":100},[1194],{"type":35,"value":451},{"type":26,"tag":87,"props":1196,"children":1197},{"class":89,"line":410},[1198],{"type":26,"tag":87,"props":1199,"children":1200},{"style":100},[1201],{"type":35,"value":363},{"type":26,"tag":87,"props":1203,"children":1204},{"class":89,"line":454},[1205],{"type":26,"tag":87,"props":1206,"children":1207},{"style":100},[1208],{"type":35,"value":468},{"type":26,"tag":31,"props":1210,"children":1211},{},[1212],{"type":26,"tag":46,"props":1213,"children":1214},{},[1215],{"type":35,"value":476},{"type":26,"tag":78,"props":1217,"children":1219},{"code":1218},"Hello from Mono!\n",[1220],{"type":26,"tag":15,"props":1221,"children":1222},{"__ignoreMap":8},[1223],{"type":35,"value":1218},{"type":26,"tag":71,"props":1225,"children":1227},{"id":1226},"example-2-flux-multiple-async-events",[1228],{"type":35,"value":1229},"Example 2: Flux (Multiple Async Events)",{"type":26,"tag":78,"props":1231,"children":1233},{"code":1232,"language":17,"meta":8,"className":81,"style":8},"import reactor.core.publisher.Flux;\nimport java.time.Duration;\n\npublic class ReactorFluxExample {\n    public static void main(String[] args) {\n        Flux.interval(Duration.ofMillis(300))\n            .take(5)\n            .map(i -> \"Tick \" + i)\n            .subscribe(System.out::println);\n\n        sleep(2000);\n    }\n\n    static void sleep(long millis) {\n        try { Thread.sleep(millis); } catch (InterruptedException e) { }\n    }\n}\n",[1234],{"type":26,"tag":15,"props":1235,"children":1236},{"__ignoreMap":8},[1237,1249,1260,1267,1287,1326,1360,1385,1420,1443,1450,1470,1477,1484,1515,1550,1557],{"type":26,"tag":87,"props":1238,"children":1239},{"class":89,"line":90},[1240,1244],{"type":26,"tag":87,"props":1241,"children":1242},{"style":94},[1243],{"type":35,"value":97},{"type":26,"tag":87,"props":1245,"children":1246},{"style":100},[1247],{"type":35,"value":1248}," reactor.core.publisher.Flux;\n",{"type":26,"tag":87,"props":1250,"children":1251},{"class":89,"line":106},[1252,1256],{"type":26,"tag":87,"props":1253,"children":1254},{"style":94},[1255],{"type":35,"value":97},{"type":26,"tag":87,"props":1257,"children":1258},{"style":100},[1259],{"type":35,"value":934},{"type":26,"tag":87,"props":1261,"children":1262},{"class":89,"line":116},[1263],{"type":26,"tag":87,"props":1264,"children":1265},{"emptyLinePlaceholder":110},[1266],{"type":35,"value":113},{"type":26,"tag":87,"props":1268,"children":1269},{"class":89,"line":141},[1270,1274,1278,1283],{"type":26,"tag":87,"props":1271,"children":1272},{"style":94},[1273],{"type":35,"value":122},{"type":26,"tag":87,"props":1275,"children":1276},{"style":94},[1277],{"type":35,"value":127},{"type":26,"tag":87,"props":1279,"children":1280},{"style":130},[1281],{"type":35,"value":1282}," ReactorFluxExample",{"type":26,"tag":87,"props":1284,"children":1285},{"style":100},[1286],{"type":35,"value":138},{"type":26,"tag":87,"props":1288,"children":1289},{"class":89,"line":191},[1290,1294,1298,1302,1306,1310,1314,1318,1322],{"type":26,"tag":87,"props":1291,"children":1292},{"style":94},[1293],{"type":35,"value":147},{"type":26,"tag":87,"props":1295,"children":1296},{"style":94},[1297],{"type":35,"value":152},{"type":26,"tag":87,"props":1299,"children":1300},{"style":94},[1301],{"type":35,"value":157},{"type":26,"tag":87,"props":1303,"children":1304},{"style":130},[1305],{"type":35,"value":162},{"type":26,"tag":87,"props":1307,"children":1308},{"style":100},[1309],{"type":35,"value":167},{"type":26,"tag":87,"props":1311,"children":1312},{"style":94},[1313],{"type":35,"value":172},{"type":26,"tag":87,"props":1315,"children":1316},{"style":100},[1317],{"type":35,"value":177},{"type":26,"tag":87,"props":1319,"children":1320},{"style":180},[1321],{"type":35,"value":183},{"type":26,"tag":87,"props":1323,"children":1324},{"style":100},[1325],{"type":35,"value":188},{"type":26,"tag":87,"props":1327,"children":1328},{"class":89,"line":238},[1329,1334,1339,1343,1347,1351,1356],{"type":26,"tag":87,"props":1330,"children":1331},{"style":100},[1332],{"type":35,"value":1333},"        Flux.",{"type":26,"tag":87,"props":1335,"children":1336},{"style":130},[1337],{"type":35,"value":1338},"interval",{"type":26,"tag":87,"props":1340,"children":1341},{"style":100},[1342],{"type":35,"value":1045},{"type":26,"tag":87,"props":1344,"children":1345},{"style":130},[1346],{"type":35,"value":1050},{"type":26,"tag":87,"props":1348,"children":1349},{"style":100},[1350],{"type":35,"value":167},{"type":26,"tag":87,"props":1352,"children":1353},{"style":251},[1354],{"type":35,"value":1355},"300",{"type":26,"tag":87,"props":1357,"children":1358},{"style":100},[1359],{"type":35,"value":1064},{"type":26,"tag":87,"props":1361,"children":1362},{"class":89,"line":262},[1363,1367,1372,1376,1381],{"type":26,"tag":87,"props":1364,"children":1365},{"style":100},[1366],{"type":35,"value":1035},{"type":26,"tag":87,"props":1368,"children":1369},{"style":130},[1370],{"type":35,"value":1371},"take",{"type":26,"tag":87,"props":1373,"children":1374},{"style":100},[1375],{"type":35,"value":167},{"type":26,"tag":87,"props":1377,"children":1378},{"style":251},[1379],{"type":35,"value":1380},"5",{"type":26,"tag":87,"props":1382,"children":1383},{"style":100},[1384],{"type":35,"value":1027},{"type":26,"tag":87,"props":1386,"children":1387},{"class":89,"line":282},[1388,1392,1397,1402,1406,1411,1415],{"type":26,"tag":87,"props":1389,"children":1390},{"style":100},[1391],{"type":35,"value":1035},{"type":26,"tag":87,"props":1393,"children":1394},{"style":130},[1395],{"type":35,"value":1396},"map",{"type":26,"tag":87,"props":1398,"children":1399},{"style":100},[1400],{"type":35,"value":1401},"(i ",{"type":26,"tag":87,"props":1403,"children":1404},{"style":94},[1405],{"type":35,"value":231},{"type":26,"tag":87,"props":1407,"children":1408},{"style":271},[1409],{"type":35,"value":1410}," \"Tick \"",{"type":26,"tag":87,"props":1412,"children":1413},{"style":94},[1414],{"type":35,"value":730},{"type":26,"tag":87,"props":1416,"children":1417},{"style":100},[1418],{"type":35,"value":1419}," i)\n",{"type":26,"tag":87,"props":1421,"children":1422},{"class":89,"line":291},[1423,1427,1431,1435,1439],{"type":26,"tag":87,"props":1424,"children":1425},{"style":100},[1426],{"type":35,"value":1035},{"type":26,"tag":87,"props":1428,"children":1429},{"style":130},[1430],{"type":35,"value":1076},{"type":26,"tag":87,"props":1432,"children":1433},{"style":100},[1434],{"type":35,"value":315},{"type":26,"tag":87,"props":1436,"children":1437},{"style":94},[1438],{"type":35,"value":320},{"type":26,"tag":87,"props":1440,"children":1441},{"style":100},[1442],{"type":35,"value":325},{"type":26,"tag":87,"props":1444,"children":1445},{"class":89,"line":299},[1446],{"type":26,"tag":87,"props":1447,"children":1448},{"emptyLinePlaceholder":110},[1449],{"type":35,"value":113},{"type":26,"tag":87,"props":1451,"children":1452},{"class":89,"line":328},[1453,1457,1461,1466],{"type":26,"tag":87,"props":1454,"children":1455},{"style":130},[1456],{"type":35,"value":334},{"type":26,"tag":87,"props":1458,"children":1459},{"style":100},[1460],{"type":35,"value":167},{"type":26,"tag":87,"props":1462,"children":1463},{"style":251},[1464],{"type":35,"value":1465},"2000",{"type":26,"tag":87,"props":1467,"children":1468},{"style":100},[1469],{"type":35,"value":259},{"type":26,"tag":87,"props":1471,"children":1472},{"class":89,"line":357},[1473],{"type":26,"tag":87,"props":1474,"children":1475},{"style":100},[1476],{"type":35,"value":363},{"type":26,"tag":87,"props":1478,"children":1479},{"class":89,"line":366},[1480],{"type":26,"tag":87,"props":1481,"children":1482},{"emptyLinePlaceholder":110},[1483],{"type":35,"value":113},{"type":26,"tag":87,"props":1485,"children":1486},{"class":89,"line":374},[1487,1491,1495,1499,1503,1507,1511],{"type":26,"tag":87,"props":1488,"children":1489},{"style":94},[1490],{"type":35,"value":380},{"type":26,"tag":87,"props":1492,"children":1493},{"style":94},[1494],{"type":35,"value":157},{"type":26,"tag":87,"props":1496,"children":1497},{"style":130},[1498],{"type":35,"value":389},{"type":26,"tag":87,"props":1500,"children":1501},{"style":100},[1502],{"type":35,"value":167},{"type":26,"tag":87,"props":1504,"children":1505},{"style":94},[1506],{"type":35,"value":398},{"type":26,"tag":87,"props":1508,"children":1509},{"style":180},[1510],{"type":35,"value":403},{"type":26,"tag":87,"props":1512,"children":1513},{"style":100},[1514],{"type":35,"value":188},{"type":26,"tag":87,"props":1516,"children":1517},{"class":89,"line":410},[1518,1522,1526,1530,1534,1538,1542,1546],{"type":26,"tag":87,"props":1519,"children":1520},{"style":94},[1521],{"type":35,"value":416},{"type":26,"tag":87,"props":1523,"children":1524},{"style":100},[1525],{"type":35,"value":421},{"type":26,"tag":87,"props":1527,"children":1528},{"style":130},[1529],{"type":35,"value":426},{"type":26,"tag":87,"props":1531,"children":1532},{"style":100},[1533],{"type":35,"value":431},{"type":26,"tag":87,"props":1535,"children":1536},{"style":94},[1537],{"type":35,"value":436},{"type":26,"tag":87,"props":1539,"children":1540},{"style":100},[1541],{"type":35,"value":441},{"type":26,"tag":87,"props":1543,"children":1544},{"style":180},[1545],{"type":35,"value":446},{"type":26,"tag":87,"props":1547,"children":1548},{"style":100},[1549],{"type":35,"value":451},{"type":26,"tag":87,"props":1551,"children":1552},{"class":89,"line":454},[1553],{"type":26,"tag":87,"props":1554,"children":1555},{"style":100},[1556],{"type":35,"value":363},{"type":26,"tag":87,"props":1558,"children":1559},{"class":89,"line":462},[1560],{"type":26,"tag":87,"props":1561,"children":1562},{"style":100},[1563],{"type":35,"value":468},{"type":26,"tag":31,"props":1565,"children":1566},{},[1567],{"type":26,"tag":46,"props":1568,"children":1569},{},[1570],{"type":35,"value":476},{"type":26,"tag":78,"props":1572,"children":1574},{"code":1573},"Tick 0\nTick 1\nTick 2\nTick 3\nTick 4\n",[1575],{"type":26,"tag":15,"props":1576,"children":1577},{"__ignoreMap":8},[1578],{"type":35,"value":1573},{"type":26,"tag":71,"props":1580,"children":1582},{"id":1581},"pros-and-cons-of-reactive-streams",[1583],{"type":35,"value":1584},"Pros and Cons of Reactive Streams",{"type":26,"tag":31,"props":1586,"children":1587},{},[1588],{"type":26,"tag":46,"props":1589,"children":1590},{},[1591],{"type":35,"value":806},{"type":26,"tag":808,"props":1593,"children":1594},{},[1595,1600,1605],{"type":26,"tag":812,"props":1596,"children":1597},{},[1598],{"type":35,"value":1599},"Rich operator set for complex flows",{"type":26,"tag":812,"props":1601,"children":1602},{},[1603],{"type":35,"value":1604},"Built-in backpressure handling",{"type":26,"tag":812,"props":1606,"children":1607},{},[1608],{"type":35,"value":1609},"Ideal for data streams and event-driven apps",{"type":26,"tag":31,"props":1611,"children":1612},{},[1613],{"type":26,"tag":46,"props":1614,"children":1615},{},[1616],{"type":35,"value":834},{"type":26,"tag":808,"props":1618,"children":1619},{},[1620,1625,1630],{"type":26,"tag":812,"props":1621,"children":1622},{},[1623],{"type":35,"value":1624},"Requires learning new paradigm",{"type":26,"tag":812,"props":1626,"children":1627},{},[1628],{"type":35,"value":1629},"Can be overkill for simple use cases",{"type":26,"tag":812,"props":1631,"children":1632},{},[1633],{"type":35,"value":1634},"More dependencies (Reactor, RxJava)",{"type":26,"tag":854,"props":1636,"children":1637},{},[],{"type":26,"tag":54,"props":1639,"children":1641},{"id":1640},"_3-when-to-use-each",[1642],{"type":35,"value":1643},"3. When to Use Each?",{"type":26,"tag":1645,"props":1646,"children":1647},"table",{},[1648,1670],{"type":26,"tag":1649,"props":1650,"children":1651},"thead",{},[1652],{"type":26,"tag":1653,"props":1654,"children":1655},"tr",{},[1656,1662,1666],{"type":26,"tag":1657,"props":1658,"children":1659},"th",{},[1660],{"type":35,"value":1661},"Use Case",{"type":26,"tag":1657,"props":1663,"children":1664},{},[1665],{"type":35,"value":42},{"type":26,"tag":1657,"props":1667,"children":1668},{},[1669],{"type":35,"value":50},{"type":26,"tag":1671,"props":1672,"children":1673},"tbody",{},[1674,1693,1709,1725,1741,1757],{"type":26,"tag":1653,"props":1675,"children":1676},{},[1677,1683,1688],{"type":26,"tag":1678,"props":1679,"children":1680},"td",{},[1681],{"type":35,"value":1682},"Simple async call",{"type":26,"tag":1678,"props":1684,"children":1685},{},[1686],{"type":35,"value":1687},"✅",{"type":26,"tag":1678,"props":1689,"children":1690},{},[1691],{"type":35,"value":1692},"❌",{"type":26,"tag":1653,"props":1694,"children":1695},{},[1696,1701,1705],{"type":26,"tag":1678,"props":1697,"children":1698},{},[1699],{"type":35,"value":1700},"API call + transform",{"type":26,"tag":1678,"props":1702,"children":1703},{},[1704],{"type":35,"value":1687},{"type":26,"tag":1678,"props":1706,"children":1707},{},[1708],{"type":35,"value":1687},{"type":26,"tag":1653,"props":1710,"children":1711},{},[1712,1717,1721],{"type":26,"tag":1678,"props":1713,"children":1714},{},[1715],{"type":35,"value":1716},"Data pipeline (streaming)",{"type":26,"tag":1678,"props":1718,"children":1719},{},[1720],{"type":35,"value":1692},{"type":26,"tag":1678,"props":1722,"children":1723},{},[1724],{"type":35,"value":1687},{"type":26,"tag":1653,"props":1726,"children":1727},{},[1728,1733,1737],{"type":26,"tag":1678,"props":1729,"children":1730},{},[1731],{"type":35,"value":1732},"Reactive UI/backend",{"type":26,"tag":1678,"props":1734,"children":1735},{},[1736],{"type":35,"value":1692},{"type":26,"tag":1678,"props":1738,"children":1739},{},[1740],{"type":35,"value":1687},{"type":26,"tag":1653,"props":1742,"children":1743},{},[1744,1749,1753],{"type":26,"tag":1678,"props":1745,"children":1746},{},[1747],{"type":35,"value":1748},"Minimal dependencies",{"type":26,"tag":1678,"props":1750,"children":1751},{},[1752],{"type":35,"value":1687},{"type":26,"tag":1678,"props":1754,"children":1755},{},[1756],{"type":35,"value":1692},{"type":26,"tag":1653,"props":1758,"children":1759},{},[1760,1765,1769],{"type":26,"tag":1678,"props":1761,"children":1762},{},[1763],{"type":35,"value":1764},"Advanced composition",{"type":26,"tag":1678,"props":1766,"children":1767},{},[1768],{"type":35,"value":1692},{"type":26,"tag":1678,"props":1770,"children":1771},{},[1772],{"type":35,"value":1687},{"type":26,"tag":854,"props":1774,"children":1775},{},[],{"type":26,"tag":54,"props":1777,"children":1779},{"id":1778},"conclusion",[1780],{"type":35,"value":1781},"Conclusion",{"type":26,"tag":31,"props":1783,"children":1784},{},[1785,1787,1792,1794,1799],{"type":35,"value":1786},"Both ",{"type":26,"tag":15,"props":1788,"children":1790},{"className":1789},[],[1791],{"type":35,"value":42},{"type":35,"value":1793}," and Reactive Streams serve asynchronous needs, but they excel in different areas. For straightforward async calls, ",{"type":26,"tag":15,"props":1795,"children":1797},{"className":1796},[],[1798],{"type":35,"value":42},{"type":35,"value":1800}," is often enough. For complex, event-driven, and high-throughput systems, reactive programming shines.",{"type":26,"tag":31,"props":1802,"children":1803},{},[1804],{"type":35,"value":1805},"By mastering both, you’ll be ready to choose the right tool for every concurrency challenge in modern Java applications.",{"type":26,"tag":1807,"props":1808,"children":1810},"h1",{"id":1809},"further-reading",[1811],{"type":35,"value":1812},"Further Reading",{"type":26,"tag":808,"props":1814,"children":1815},{},[1816,1828,1838],{"type":26,"tag":812,"props":1817,"children":1818},{},[1819],{"type":26,"tag":1820,"props":1821,"children":1825},"a",{"href":1822,"rel":1823},"https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html",[1824],"nofollow",[1826],{"type":35,"value":1827},"CompletableFuture Javadoc",{"type":26,"tag":812,"props":1829,"children":1830},{},[1831],{"type":26,"tag":1820,"props":1832,"children":1835},{"href":1833,"rel":1834},"https://projectreactor.io/docs",[1824],[1836],{"type":35,"value":1837},"Project Reactor Documentation",{"type":26,"tag":812,"props":1839,"children":1840},{},[1841],{"type":26,"tag":1820,"props":1842,"children":1845},{"href":1843,"rel":1844},"https://www.reactivemanifesto.org/",[1824],[1846],{"type":35,"value":1847},"Reactive Manifesto",{"type":26,"tag":1849,"props":1850,"children":1851},"style",{},[1852],{"type":35,"value":1853},"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":106,"depth":106,"links":1855},[1856,1861,1866,1867],{"id":56,"depth":106,"text":59,"children":1857},[1858,1859,1860],{"id":73,"depth":116,"text":76},{"id":487,"depth":116,"text":490},{"id":795,"depth":116,"text":798},{"id":859,"depth":106,"text":862,"children":1862},[1863,1864,1865],{"id":900,"depth":116,"text":903},{"id":1226,"depth":116,"text":1229},{"id":1581,"depth":116,"text":1584},{"id":1640,"depth":106,"text":1643},{"id":1778,"depth":106,"text":1781},"markdown","content:posts:async-java-completable-vs-reactive.md","content","posts/async-java-completable-vs-reactive.md","posts/async-java-completable-vs-reactive","md",{"_path":1875,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":1876,"description":1877,"date":1878,"layout":12,"subtitle":1879,"image":1880,"optimized_image":1880,"category":15,"tags":1881,"author":21,"paginate":7,"body":1883,"_type":1868,"_id":3126,"_source":1870,"_file":3127,"_stem":3128,"_extension":1873},"/posts/mastering-concurrency","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","A Deep Dive into Java’s Multithreading and Parallel Computing","/assets/img/uploads/java-concurrency.png",[15,17,20,1882],"multithreading",{"type":23,"children":1884,"toc":3109},[1885,1893,1899,1904,1957,1970,1976,2004,2244,2250,2283,2288,2294,2314,2320,2325,2620,2626,2673,2679,2697,2703,2708,2714,2747,2752,3016,3022,3040,3044,3049,3054,3058,3100,3105],{"type":26,"tag":27,"props":1886,"children":1887},{},[1888],{"type":26,"tag":31,"props":1889,"children":1890},{},[1891],{"type":35,"value":1892},"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":26,"tag":54,"props":1894,"children":1896},{"id":1895},"understanding-javas-concurrency-model",[1897],{"type":35,"value":1898},"Understanding Java’s Concurrency Model",{"type":26,"tag":31,"props":1900,"children":1901},{},[1902],{"type":35,"value":1903},"Concurrency in Java enables applications to perform multiple tasks simultaneously, improving throughput and responsiveness. Java provides several concurrency primitives:",{"type":26,"tag":808,"props":1905,"children":1906},{},[1907,1917,1927,1937,1947],{"type":26,"tag":812,"props":1908,"children":1909},{},[1910,1915],{"type":26,"tag":46,"props":1911,"children":1912},{},[1913],{"type":35,"value":1914},"Threads",{"type":35,"value":1916},": The fundamental unit of execution, allowing tasks to run in parallel.",{"type":26,"tag":812,"props":1918,"children":1919},{},[1920,1925],{"type":26,"tag":46,"props":1921,"children":1922},{},[1923],{"type":35,"value":1924},"Executors",{"type":35,"value":1926},": High-level abstraction for managing thread pools and task execution.",{"type":26,"tag":812,"props":1928,"children":1929},{},[1930,1935],{"type":26,"tag":46,"props":1931,"children":1932},{},[1933],{"type":35,"value":1934},"Virtual Threads",{"type":35,"value":1936},": Lightweight, memory-efficient threads introduced in Java 21 for massive scalability.",{"type":26,"tag":812,"props":1938,"children":1939},{},[1940,1945],{"type":26,"tag":46,"props":1941,"children":1942},{},[1943],{"type":35,"value":1944},"Fork/Join Framework",{"type":35,"value":1946},": Designed for divide-and-conquer algorithms using parallel task execution.",{"type":26,"tag":812,"props":1948,"children":1949},{},[1950,1955],{"type":26,"tag":46,"props":1951,"children":1952},{},[1953],{"type":35,"value":1954},"Reactive Programming",{"type":35,"value":1956},": Asynchronous, non-blocking programming using event streams.",{"type":26,"tag":31,"props":1958,"children":1959},{},[1960,1962,1968],{"type":35,"value":1961},"Let's now take a deeper look at three major building blocks of concurrent programming in Java: classic threads, the ",{"type":26,"tag":15,"props":1963,"children":1965},{"className":1964},[],[1966],{"type":35,"value":1967},"ExecutorService",{"type":35,"value":1969},", and the new virtual threads.",{"type":26,"tag":54,"props":1971,"children":1973},{"id":1972},"classic-approach-java-threads",[1974],{"type":35,"value":1975},"Classic Approach: Java Threads",{"type":26,"tag":31,"props":1977,"children":1978},{},[1979,1981,1987,1989,1994,1996,2002],{"type":35,"value":1980},"The most basic unit of concurrency in Java is the ",{"type":26,"tag":15,"props":1982,"children":1984},{"className":1983},[],[1985],{"type":35,"value":1986},"Thread",{"type":35,"value":1988},". You can extend the ",{"type":26,"tag":15,"props":1990,"children":1992},{"className":1991},[],[1993],{"type":35,"value":1986},{"type":35,"value":1995}," class or implement the ",{"type":26,"tag":15,"props":1997,"children":1999},{"className":1998},[],[2000],{"type":35,"value":2001},"Runnable",{"type":35,"value":2003}," interface to define concurrent tasks.",{"type":26,"tag":78,"props":2005,"children":2007},{"className":81,"code":2006,"language":17,"meta":8,"style":8},"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",[2008],{"type":26,"tag":15,"props":2009,"children":2010},{"__ignoreMap":8},[2011,2038,2059,2107,2114,2121,2128,2148,2187,2213,2230,2237],{"type":26,"tag":87,"props":2012,"children":2013},{"class":89,"line":90},[2014,2019,2024,2029,2034],{"type":26,"tag":87,"props":2015,"children":2016},{"style":94},[2017],{"type":35,"value":2018},"class",{"type":26,"tag":87,"props":2020,"children":2021},{"style":130},[2022],{"type":35,"value":2023}," SimpleThreadExample",{"type":26,"tag":87,"props":2025,"children":2026},{"style":94},[2027],{"type":35,"value":2028}," extends",{"type":26,"tag":87,"props":2030,"children":2031},{"style":130},[2032],{"type":35,"value":2033}," Thread",{"type":26,"tag":87,"props":2035,"children":2036},{"style":100},[2037],{"type":35,"value":138},{"type":26,"tag":87,"props":2039,"children":2040},{"class":89,"line":106},[2041,2045,2049,2054],{"type":26,"tag":87,"props":2042,"children":2043},{"style":94},[2044],{"type":35,"value":147},{"type":26,"tag":87,"props":2046,"children":2047},{"style":94},[2048],{"type":35,"value":157},{"type":26,"tag":87,"props":2050,"children":2051},{"style":130},[2052],{"type":35,"value":2053}," run",{"type":26,"tag":87,"props":2055,"children":2056},{"style":100},[2057],{"type":35,"value":2058},"() {\n",{"type":26,"tag":87,"props":2060,"children":2061},{"class":89,"line":116},[2062,2066,2070,2074,2079,2083,2088,2093,2098,2103],{"type":26,"tag":87,"props":2063,"children":2064},{"style":100},[2065],{"type":35,"value":743},{"type":26,"tag":87,"props":2067,"children":2068},{"style":130},[2069],{"type":35,"value":748},{"type":26,"tag":87,"props":2071,"children":2072},{"style":100},[2073],{"type":35,"value":167},{"type":26,"tag":87,"props":2075,"children":2076},{"style":271},[2077],{"type":35,"value":2078},"\"Thread running: \"",{"type":26,"tag":87,"props":2080,"children":2081},{"style":94},[2082],{"type":35,"value":730},{"type":26,"tag":87,"props":2084,"children":2085},{"style":100},[2086],{"type":35,"value":2087}," Thread.",{"type":26,"tag":87,"props":2089,"children":2090},{"style":130},[2091],{"type":35,"value":2092},"currentThread",{"type":26,"tag":87,"props":2094,"children":2095},{"style":100},[2096],{"type":35,"value":2097},"().",{"type":26,"tag":87,"props":2099,"children":2100},{"style":130},[2101],{"type":35,"value":2102},"getName",{"type":26,"tag":87,"props":2104,"children":2105},{"style":100},[2106],{"type":35,"value":763},{"type":26,"tag":87,"props":2108,"children":2109},{"class":89,"line":141},[2110],{"type":26,"tag":87,"props":2111,"children":2112},{"style":100},[2113],{"type":35,"value":363},{"type":26,"tag":87,"props":2115,"children":2116},{"class":89,"line":191},[2117],{"type":26,"tag":87,"props":2118,"children":2119},{"style":100},[2120],{"type":35,"value":468},{"type":26,"tag":87,"props":2122,"children":2123},{"class":89,"line":238},[2124],{"type":26,"tag":87,"props":2125,"children":2126},{"emptyLinePlaceholder":110},[2127],{"type":35,"value":113},{"type":26,"tag":87,"props":2129,"children":2130},{"class":89,"line":262},[2131,2135,2139,2144],{"type":26,"tag":87,"props":2132,"children":2133},{"style":94},[2134],{"type":35,"value":122},{"type":26,"tag":87,"props":2136,"children":2137},{"style":94},[2138],{"type":35,"value":127},{"type":26,"tag":87,"props":2140,"children":2141},{"style":130},[2142],{"type":35,"value":2143}," ThreadDemo",{"type":26,"tag":87,"props":2145,"children":2146},{"style":100},[2147],{"type":35,"value":138},{"type":26,"tag":87,"props":2149,"children":2150},{"class":89,"line":282},[2151,2155,2159,2163,2167,2171,2175,2179,2183],{"type":26,"tag":87,"props":2152,"children":2153},{"style":94},[2154],{"type":35,"value":147},{"type":26,"tag":87,"props":2156,"children":2157},{"style":94},[2158],{"type":35,"value":152},{"type":26,"tag":87,"props":2160,"children":2161},{"style":94},[2162],{"type":35,"value":157},{"type":26,"tag":87,"props":2164,"children":2165},{"style":130},[2166],{"type":35,"value":162},{"type":26,"tag":87,"props":2168,"children":2169},{"style":100},[2170],{"type":35,"value":167},{"type":26,"tag":87,"props":2172,"children":2173},{"style":94},[2174],{"type":35,"value":172},{"type":26,"tag":87,"props":2176,"children":2177},{"style":100},[2178],{"type":35,"value":177},{"type":26,"tag":87,"props":2180,"children":2181},{"style":180},[2182],{"type":35,"value":183},{"type":26,"tag":87,"props":2184,"children":2185},{"style":100},[2186],{"type":35,"value":188},{"type":26,"tag":87,"props":2188,"children":2189},{"class":89,"line":291},[2190,2195,2199,2204,2208],{"type":26,"tag":87,"props":2191,"children":2192},{"style":100},[2193],{"type":35,"value":2194},"        SimpleThreadExample thread ",{"type":26,"tag":87,"props":2196,"children":2197},{"style":94},[2198],{"type":35,"value":211},{"type":26,"tag":87,"props":2200,"children":2201},{"style":94},[2202],{"type":35,"value":2203}," new",{"type":26,"tag":87,"props":2205,"children":2206},{"style":130},[2207],{"type":35,"value":2023},{"type":26,"tag":87,"props":2209,"children":2210},{"style":100},[2211],{"type":35,"value":2212},"();\n",{"type":26,"tag":87,"props":2214,"children":2215},{"class":89,"line":299},[2216,2221,2226],{"type":26,"tag":87,"props":2217,"children":2218},{"style":100},[2219],{"type":35,"value":2220},"        thread.",{"type":26,"tag":87,"props":2222,"children":2223},{"style":130},[2224],{"type":35,"value":2225},"start",{"type":26,"tag":87,"props":2227,"children":2228},{"style":100},[2229],{"type":35,"value":2212},{"type":26,"tag":87,"props":2231,"children":2232},{"class":89,"line":328},[2233],{"type":26,"tag":87,"props":2234,"children":2235},{"style":100},[2236],{"type":35,"value":363},{"type":26,"tag":87,"props":2238,"children":2239},{"class":89,"line":357},[2240],{"type":26,"tag":87,"props":2241,"children":2242},{"style":100},[2243],{"type":35,"value":468},{"type":26,"tag":71,"props":2245,"children":2247},{"id":2246},"limitations-of-raw-threads",[2248],{"type":35,"value":2249},"Limitations of Raw Threads",{"type":26,"tag":808,"props":2251,"children":2252},{},[2253,2263,2273],{"type":26,"tag":812,"props":2254,"children":2255},{},[2256,2261],{"type":26,"tag":46,"props":2257,"children":2258},{},[2259],{"type":35,"value":2260},"Performance Overhead",{"type":35,"value":2262},": Creating and destroying threads is expensive.",{"type":26,"tag":812,"props":2264,"children":2265},{},[2266,2271],{"type":26,"tag":46,"props":2267,"children":2268},{},[2269],{"type":35,"value":2270},"No Pooling",{"type":35,"value":2272},": Each thread is a one-off. No built-in reuse.",{"type":26,"tag":812,"props":2274,"children":2275},{},[2276,2281],{"type":26,"tag":46,"props":2277,"children":2278},{},[2279],{"type":35,"value":2280},"Scalability Issues",{"type":35,"value":2282},": System resources limit the number of threads you can create.",{"type":26,"tag":31,"props":2284,"children":2285},{},[2286],{"type":35,"value":2287},"Using raw threads for large-scale concurrent applications quickly becomes impractical.",{"type":26,"tag":54,"props":2289,"children":2291},{"id":2290},"modern-concurrency-executorservice-and-thread-pooling",[2292],{"type":35,"value":2293},"Modern Concurrency: ExecutorService and Thread Pooling",{"type":26,"tag":31,"props":2295,"children":2296},{},[2297,2299,2304,2306,2312],{"type":35,"value":2298},"To solve the limitations of raw threads, Java introduced the ",{"type":26,"tag":15,"props":2300,"children":2302},{"className":2301},[],[2303],{"type":35,"value":1967},{"type":35,"value":2305}," as part of ",{"type":26,"tag":15,"props":2307,"children":2309},{"className":2308},[],[2310],{"type":35,"value":2311},"java.util.concurrent",{"type":35,"value":2313},". It provides a high-level API to manage and reuse threads through pooling.",{"type":26,"tag":71,"props":2315,"children":2317},{"id":2316},"how-thread-pooling-works",[2318],{"type":35,"value":2319},"How Thread Pooling Works",{"type":26,"tag":31,"props":2321,"children":2322},{},[2323],{"type":35,"value":2324},"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":26,"tag":78,"props":2326,"children":2328},{"className":81,"code":2327,"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",[2329],{"type":26,"tag":15,"props":2330,"children":2331},{"__ignoreMap":8},[2332,2344,2356,2363,2383,2422,2457,2517,2581,2589,2606,2613],{"type":26,"tag":87,"props":2333,"children":2334},{"class":89,"line":90},[2335,2339],{"type":26,"tag":87,"props":2336,"children":2337},{"style":94},[2338],{"type":35,"value":97},{"type":26,"tag":87,"props":2340,"children":2341},{"style":100},[2342],{"type":35,"value":2343}," java.util.concurrent.ExecutorService;\n",{"type":26,"tag":87,"props":2345,"children":2346},{"class":89,"line":106},[2347,2351],{"type":26,"tag":87,"props":2348,"children":2349},{"style":94},[2350],{"type":35,"value":97},{"type":26,"tag":87,"props":2352,"children":2353},{"style":100},[2354],{"type":35,"value":2355}," java.util.concurrent.Executors;\n",{"type":26,"tag":87,"props":2357,"children":2358},{"class":89,"line":116},[2359],{"type":26,"tag":87,"props":2360,"children":2361},{"emptyLinePlaceholder":110},[2362],{"type":35,"value":113},{"type":26,"tag":87,"props":2364,"children":2365},{"class":89,"line":141},[2366,2370,2374,2379],{"type":26,"tag":87,"props":2367,"children":2368},{"style":94},[2369],{"type":35,"value":122},{"type":26,"tag":87,"props":2371,"children":2372},{"style":94},[2373],{"type":35,"value":127},{"type":26,"tag":87,"props":2375,"children":2376},{"style":130},[2377],{"type":35,"value":2378}," ExecutorExample",{"type":26,"tag":87,"props":2380,"children":2381},{"style":100},[2382],{"type":35,"value":138},{"type":26,"tag":87,"props":2384,"children":2385},{"class":89,"line":191},[2386,2390,2394,2398,2402,2406,2410,2414,2418],{"type":26,"tag":87,"props":2387,"children":2388},{"style":94},[2389],{"type":35,"value":147},{"type":26,"tag":87,"props":2391,"children":2392},{"style":94},[2393],{"type":35,"value":152},{"type":26,"tag":87,"props":2395,"children":2396},{"style":94},[2397],{"type":35,"value":157},{"type":26,"tag":87,"props":2399,"children":2400},{"style":130},[2401],{"type":35,"value":162},{"type":26,"tag":87,"props":2403,"children":2404},{"style":100},[2405],{"type":35,"value":167},{"type":26,"tag":87,"props":2407,"children":2408},{"style":94},[2409],{"type":35,"value":172},{"type":26,"tag":87,"props":2411,"children":2412},{"style":100},[2413],{"type":35,"value":177},{"type":26,"tag":87,"props":2415,"children":2416},{"style":180},[2417],{"type":35,"value":183},{"type":26,"tag":87,"props":2419,"children":2420},{"style":100},[2421],{"type":35,"value":188},{"type":26,"tag":87,"props":2423,"children":2424},{"class":89,"line":238},[2425,2430,2434,2439,2444,2448,2453],{"type":26,"tag":87,"props":2426,"children":2427},{"style":100},[2428],{"type":35,"value":2429},"        ExecutorService executor ",{"type":26,"tag":87,"props":2431,"children":2432},{"style":94},[2433],{"type":35,"value":211},{"type":26,"tag":87,"props":2435,"children":2436},{"style":100},[2437],{"type":35,"value":2438}," Executors.",{"type":26,"tag":87,"props":2440,"children":2441},{"style":130},[2442],{"type":35,"value":2443},"newFixedThreadPool",{"type":26,"tag":87,"props":2445,"children":2446},{"style":100},[2447],{"type":35,"value":167},{"type":26,"tag":87,"props":2449,"children":2450},{"style":251},[2451],{"type":35,"value":2452},"3",{"type":26,"tag":87,"props":2454,"children":2455},{"style":100},[2456],{"type":35,"value":259},{"type":26,"tag":87,"props":2458,"children":2459},{"class":89,"line":262},[2460,2465,2469,2474,2479,2483,2488,2493,2498,2503,2508,2513],{"type":26,"tag":87,"props":2461,"children":2462},{"style":94},[2463],{"type":35,"value":2464},"        for",{"type":26,"tag":87,"props":2466,"children":2467},{"style":100},[2468],{"type":35,"value":874},{"type":26,"tag":87,"props":2470,"children":2471},{"style":94},[2472],{"type":35,"value":2473},"int",{"type":26,"tag":87,"props":2475,"children":2476},{"style":100},[2477],{"type":35,"value":2478}," i ",{"type":26,"tag":87,"props":2480,"children":2481},{"style":94},[2482],{"type":35,"value":211},{"type":26,"tag":87,"props":2484,"children":2485},{"style":251},[2486],{"type":35,"value":2487}," 0",{"type":26,"tag":87,"props":2489,"children":2490},{"style":100},[2491],{"type":35,"value":2492},"; i ",{"type":26,"tag":87,"props":2494,"children":2495},{"style":94},[2496],{"type":35,"value":2497},"\u003C",{"type":26,"tag":87,"props":2499,"children":2500},{"style":251},[2501],{"type":35,"value":2502}," 5",{"type":26,"tag":87,"props":2504,"children":2505},{"style":100},[2506],{"type":35,"value":2507},"; i",{"type":26,"tag":87,"props":2509,"children":2510},{"style":94},[2511],{"type":35,"value":2512},"++",{"type":26,"tag":87,"props":2514,"children":2515},{"style":100},[2516],{"type":35,"value":188},{"type":26,"tag":87,"props":2518,"children":2519},{"class":89,"line":282},[2520,2525,2530,2534,2538,2543,2547,2551,2556,2560,2564,2568,2572,2576],{"type":26,"tag":87,"props":2521,"children":2522},{"style":100},[2523],{"type":35,"value":2524},"            executor.",{"type":26,"tag":87,"props":2526,"children":2527},{"style":130},[2528],{"type":35,"value":2529},"submit",{"type":26,"tag":87,"props":2531,"children":2532},{"style":100},[2533],{"type":35,"value":226},{"type":26,"tag":87,"props":2535,"children":2536},{"style":94},[2537],{"type":35,"value":231},{"type":26,"tag":87,"props":2539,"children":2540},{"style":100},[2541],{"type":35,"value":2542}," System.out.",{"type":26,"tag":87,"props":2544,"children":2545},{"style":130},[2546],{"type":35,"value":748},{"type":26,"tag":87,"props":2548,"children":2549},{"style":100},[2550],{"type":35,"value":167},{"type":26,"tag":87,"props":2552,"children":2553},{"style":271},[2554],{"type":35,"value":2555},"\"Task executed by: \"",{"type":26,"tag":87,"props":2557,"children":2558},{"style":94},[2559],{"type":35,"value":730},{"type":26,"tag":87,"props":2561,"children":2562},{"style":100},[2563],{"type":35,"value":2087},{"type":26,"tag":87,"props":2565,"children":2566},{"style":130},[2567],{"type":35,"value":2092},{"type":26,"tag":87,"props":2569,"children":2570},{"style":100},[2571],{"type":35,"value":2097},{"type":26,"tag":87,"props":2573,"children":2574},{"style":130},[2575],{"type":35,"value":2102},{"type":26,"tag":87,"props":2577,"children":2578},{"style":100},[2579],{"type":35,"value":2580},"()));\n",{"type":26,"tag":87,"props":2582,"children":2583},{"class":89,"line":291},[2584],{"type":26,"tag":87,"props":2585,"children":2586},{"style":100},[2587],{"type":35,"value":2588},"        }\n",{"type":26,"tag":87,"props":2590,"children":2591},{"class":89,"line":299},[2592,2597,2602],{"type":26,"tag":87,"props":2593,"children":2594},{"style":100},[2595],{"type":35,"value":2596},"        executor.",{"type":26,"tag":87,"props":2598,"children":2599},{"style":130},[2600],{"type":35,"value":2601},"shutdown",{"type":26,"tag":87,"props":2603,"children":2604},{"style":100},[2605],{"type":35,"value":2212},{"type":26,"tag":87,"props":2607,"children":2608},{"class":89,"line":328},[2609],{"type":26,"tag":87,"props":2610,"children":2611},{"style":100},[2612],{"type":35,"value":363},{"type":26,"tag":87,"props":2614,"children":2615},{"class":89,"line":357},[2616],{"type":26,"tag":87,"props":2617,"children":2618},{"style":100},[2619],{"type":35,"value":468},{"type":26,"tag":71,"props":2621,"children":2623},{"id":2622},"common-executor-types",[2624],{"type":35,"value":2625},"Common Executor Types:",{"type":26,"tag":808,"props":2627,"children":2628},{},[2629,2640,2651,2662],{"type":26,"tag":812,"props":2630,"children":2631},{},[2632,2638],{"type":26,"tag":15,"props":2633,"children":2635},{"className":2634},[],[2636],{"type":35,"value":2637},"newFixedThreadPool(n)",{"type":35,"value":2639},": Fixed number of threads.",{"type":26,"tag":812,"props":2641,"children":2642},{},[2643,2649],{"type":26,"tag":15,"props":2644,"children":2646},{"className":2645},[],[2647],{"type":35,"value":2648},"newCachedThreadPool()",{"type":35,"value":2650},": Dynamically creates threads as needed.",{"type":26,"tag":812,"props":2652,"children":2653},{},[2654,2660],{"type":26,"tag":15,"props":2655,"children":2657},{"className":2656},[],[2658],{"type":35,"value":2659},"newSingleThreadExecutor()",{"type":35,"value":2661},": One thread for all tasks.",{"type":26,"tag":812,"props":2663,"children":2664},{},[2665,2671],{"type":26,"tag":15,"props":2666,"children":2668},{"className":2667},[],[2669],{"type":35,"value":2670},"newScheduledThreadPool(n)",{"type":35,"value":2672},": Supports delayed and periodic tasks.",{"type":26,"tag":71,"props":2674,"children":2676},{"id":2675},"advantages",[2677],{"type":35,"value":2678},"Advantages:",{"type":26,"tag":808,"props":2680,"children":2681},{},[2682,2687,2692],{"type":26,"tag":812,"props":2683,"children":2684},{},[2685],{"type":35,"value":2686},"Efficient resource use through reuse.",{"type":26,"tag":812,"props":2688,"children":2689},{},[2690],{"type":35,"value":2691},"Better scalability and responsiveness.",{"type":26,"tag":812,"props":2693,"children":2694},{},[2695],{"type":35,"value":2696},"Built-in management of task queues and error handling.",{"type":26,"tag":54,"props":2698,"children":2700},{"id":2699},"the-future-virtual-threads-and-project-loom",[2701],{"type":35,"value":2702},"The Future: Virtual Threads and Project Loom",{"type":26,"tag":31,"props":2704,"children":2705},{},[2706],{"type":35,"value":2707},"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":26,"tag":71,"props":2709,"children":2711},{"id":2710},"key-features",[2712],{"type":35,"value":2713},"Key Features:",{"type":26,"tag":808,"props":2715,"children":2716},{},[2717,2727,2737],{"type":26,"tag":812,"props":2718,"children":2719},{},[2720,2725],{"type":26,"tag":46,"props":2721,"children":2722},{},[2723],{"type":35,"value":2724},"Minimal Memory Footprint",{"type":35,"value":2726},": Much lighter than traditional threads.",{"type":26,"tag":812,"props":2728,"children":2729},{},[2730,2735],{"type":26,"tag":46,"props":2731,"children":2732},{},[2733],{"type":35,"value":2734},"Unblocked I/O",{"type":35,"value":2736},": I/O operations don’t block the underlying platform thread.",{"type":26,"tag":812,"props":2738,"children":2739},{},[2740,2745],{"type":26,"tag":46,"props":2741,"children":2742},{},[2743],{"type":35,"value":2744},"Massive Scalability",{"type":35,"value":2746},": Suitable for applications requiring thousands or millions of concurrent tasks (e.g., web servers).",{"type":26,"tag":31,"props":2748,"children":2749},{},[2750],{"type":35,"value":2751},"Example:",{"type":26,"tag":78,"props":2753,"children":2755},{"className":81,"code":2754,"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",[2756],{"type":26,"tag":15,"props":2757,"children":2758},{"__ignoreMap":8},[2759,2779,2818,2857,2910,2934,2979,2987,2995,3002,3009],{"type":26,"tag":87,"props":2760,"children":2761},{"class":89,"line":90},[2762,2766,2770,2775],{"type":26,"tag":87,"props":2763,"children":2764},{"style":94},[2765],{"type":35,"value":122},{"type":26,"tag":87,"props":2767,"children":2768},{"style":94},[2769],{"type":35,"value":127},{"type":26,"tag":87,"props":2771,"children":2772},{"style":130},[2773],{"type":35,"value":2774}," VirtualThreadExample",{"type":26,"tag":87,"props":2776,"children":2777},{"style":100},[2778],{"type":35,"value":138},{"type":26,"tag":87,"props":2780,"children":2781},{"class":89,"line":106},[2782,2786,2790,2794,2798,2802,2806,2810,2814],{"type":26,"tag":87,"props":2783,"children":2784},{"style":94},[2785],{"type":35,"value":147},{"type":26,"tag":87,"props":2787,"children":2788},{"style":94},[2789],{"type":35,"value":152},{"type":26,"tag":87,"props":2791,"children":2792},{"style":94},[2793],{"type":35,"value":157},{"type":26,"tag":87,"props":2795,"children":2796},{"style":130},[2797],{"type":35,"value":162},{"type":26,"tag":87,"props":2799,"children":2800},{"style":100},[2801],{"type":35,"value":167},{"type":26,"tag":87,"props":2803,"children":2804},{"style":94},[2805],{"type":35,"value":172},{"type":26,"tag":87,"props":2807,"children":2808},{"style":100},[2809],{"type":35,"value":177},{"type":26,"tag":87,"props":2811,"children":2812},{"style":180},[2813],{"type":35,"value":183},{"type":26,"tag":87,"props":2815,"children":2816},{"style":100},[2817],{"type":35,"value":188},{"type":26,"tag":87,"props":2819,"children":2820},{"class":89,"line":116},[2821,2825,2829,2834,2839,2843,2847,2852],{"type":26,"tag":87,"props":2822,"children":2823},{"style":94},[2824],{"type":35,"value":416},{"type":26,"tag":87,"props":2826,"children":2827},{"style":100},[2828],{"type":35,"value":874},{"type":26,"tag":87,"props":2830,"children":2831},{"style":94},[2832],{"type":35,"value":2833},"var",{"type":26,"tag":87,"props":2835,"children":2836},{"style":100},[2837],{"type":35,"value":2838}," executor ",{"type":26,"tag":87,"props":2840,"children":2841},{"style":94},[2842],{"type":35,"value":211},{"type":26,"tag":87,"props":2844,"children":2845},{"style":100},[2846],{"type":35,"value":2438},{"type":26,"tag":87,"props":2848,"children":2849},{"style":130},[2850],{"type":35,"value":2851},"newVirtualThreadPerTaskExecutor",{"type":26,"tag":87,"props":2853,"children":2854},{"style":100},[2855],{"type":35,"value":2856},"()) {\n",{"type":26,"tag":87,"props":2858,"children":2859},{"class":89,"line":141},[2860,2865,2869,2873,2877,2881,2885,2889,2893,2898,2902,2906],{"type":26,"tag":87,"props":2861,"children":2862},{"style":94},[2863],{"type":35,"value":2864},"            for",{"type":26,"tag":87,"props":2866,"children":2867},{"style":100},[2868],{"type":35,"value":874},{"type":26,"tag":87,"props":2870,"children":2871},{"style":94},[2872],{"type":35,"value":2473},{"type":26,"tag":87,"props":2874,"children":2875},{"style":100},[2876],{"type":35,"value":2478},{"type":26,"tag":87,"props":2878,"children":2879},{"style":94},[2880],{"type":35,"value":211},{"type":26,"tag":87,"props":2882,"children":2883},{"style":251},[2884],{"type":35,"value":2487},{"type":26,"tag":87,"props":2886,"children":2887},{"style":100},[2888],{"type":35,"value":2492},{"type":26,"tag":87,"props":2890,"children":2891},{"style":94},[2892],{"type":35,"value":2497},{"type":26,"tag":87,"props":2894,"children":2895},{"style":251},[2896],{"type":35,"value":2897}," 10",{"type":26,"tag":87,"props":2899,"children":2900},{"style":100},[2901],{"type":35,"value":2507},{"type":26,"tag":87,"props":2903,"children":2904},{"style":94},[2905],{"type":35,"value":2512},{"type":26,"tag":87,"props":2907,"children":2908},{"style":100},[2909],{"type":35,"value":188},{"type":26,"tag":87,"props":2911,"children":2912},{"class":89,"line":191},[2913,2918,2922,2926,2930],{"type":26,"tag":87,"props":2914,"children":2915},{"style":100},[2916],{"type":35,"value":2917},"                executor.",{"type":26,"tag":87,"props":2919,"children":2920},{"style":130},[2921],{"type":35,"value":2529},{"type":26,"tag":87,"props":2923,"children":2924},{"style":100},[2925],{"type":35,"value":226},{"type":26,"tag":87,"props":2927,"children":2928},{"style":94},[2929],{"type":35,"value":231},{"type":26,"tag":87,"props":2931,"children":2932},{"style":100},[2933],{"type":35,"value":138},{"type":26,"tag":87,"props":2935,"children":2936},{"class":89,"line":238},[2937,2942,2946,2950,2955,2959,2963,2967,2971,2975],{"type":26,"tag":87,"props":2938,"children":2939},{"style":100},[2940],{"type":35,"value":2941},"                    System.out.",{"type":26,"tag":87,"props":2943,"children":2944},{"style":130},[2945],{"type":35,"value":748},{"type":26,"tag":87,"props":2947,"children":2948},{"style":100},[2949],{"type":35,"value":167},{"type":26,"tag":87,"props":2951,"children":2952},{"style":271},[2953],{"type":35,"value":2954},"\"Running in: \"",{"type":26,"tag":87,"props":2956,"children":2957},{"style":94},[2958],{"type":35,"value":730},{"type":26,"tag":87,"props":2960,"children":2961},{"style":100},[2962],{"type":35,"value":2087},{"type":26,"tag":87,"props":2964,"children":2965},{"style":130},[2966],{"type":35,"value":2092},{"type":26,"tag":87,"props":2968,"children":2969},{"style":100},[2970],{"type":35,"value":2097},{"type":26,"tag":87,"props":2972,"children":2973},{"style":130},[2974],{"type":35,"value":2102},{"type":26,"tag":87,"props":2976,"children":2977},{"style":100},[2978],{"type":35,"value":763},{"type":26,"tag":87,"props":2980,"children":2981},{"class":89,"line":262},[2982],{"type":26,"tag":87,"props":2983,"children":2984},{"style":100},[2985],{"type":35,"value":2986},"                });\n",{"type":26,"tag":87,"props":2988,"children":2989},{"class":89,"line":282},[2990],{"type":26,"tag":87,"props":2991,"children":2992},{"style":100},[2993],{"type":35,"value":2994},"            }\n",{"type":26,"tag":87,"props":2996,"children":2997},{"class":89,"line":291},[2998],{"type":26,"tag":87,"props":2999,"children":3000},{"style":100},[3001],{"type":35,"value":2588},{"type":26,"tag":87,"props":3003,"children":3004},{"class":89,"line":299},[3005],{"type":26,"tag":87,"props":3006,"children":3007},{"style":100},[3008],{"type":35,"value":363},{"type":26,"tag":87,"props":3010,"children":3011},{"class":89,"line":328},[3012],{"type":26,"tag":87,"props":3013,"children":3014},{"style":100},[3015],{"type":35,"value":468},{"type":26,"tag":71,"props":3017,"children":3019},{"id":3018},"when-to-use-virtual-threads",[3020],{"type":35,"value":3021},"When to Use Virtual Threads:",{"type":26,"tag":808,"props":3023,"children":3024},{},[3025,3030,3035],{"type":26,"tag":812,"props":3026,"children":3027},{},[3028],{"type":35,"value":3029},"I/O-bound applications (e.g., database access, HTTP clients).",{"type":26,"tag":812,"props":3031,"children":3032},{},[3033],{"type":35,"value":3034},"High-concurrency workloads (e.g., messaging systems).",{"type":26,"tag":812,"props":3036,"children":3037},{},[3038],{"type":35,"value":3039},"When simplifying thread management is a priority.",{"type":26,"tag":54,"props":3041,"children":3042},{"id":1778},[3043],{"type":35,"value":1781},{"type":26,"tag":31,"props":3045,"children":3046},{},[3047],{"type":35,"value":3048},"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":26,"tag":31,"props":3050,"children":3051},{},[3052],{"type":35,"value":3053},"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":26,"tag":54,"props":3055,"children":3056},{"id":1809},[3057],{"type":35,"value":1812},{"type":26,"tag":808,"props":3059,"children":3060},{},[3061,3071,3081,3090],{"type":26,"tag":812,"props":3062,"children":3063},{},[3064],{"type":26,"tag":1820,"props":3065,"children":3068},{"href":3066,"rel":3067},"https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html",[1824],[3069],{"type":35,"value":3070},"Java Concurrency API",{"type":26,"tag":812,"props":3072,"children":3073},{},[3074],{"type":26,"tag":1820,"props":3075,"children":3078},{"href":3076,"rel":3077},"https://openjdk.org/projects/loom/",[1824],[3079],{"type":35,"value":3080},"Project Loom (Virtual Threads)",{"type":26,"tag":812,"props":3082,"children":3083},{},[3084],{"type":26,"tag":1820,"props":3085,"children":3088},{"href":3086,"rel":3087},"https://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html",[1824],[3089],{"type":35,"value":1944},{"type":26,"tag":812,"props":3091,"children":3092},{},[3093],{"type":26,"tag":1820,"props":3094,"children":3097},{"href":3095,"rel":3096},"https://www.oreilly.com/library/view/effective-java-3rd/9780134686097/",[1824],[3098],{"type":35,"value":3099},"Effective Java - Joshua Bloch",{"type":26,"tag":31,"props":3101,"children":3102},{},[3103],{"type":35,"value":3104},"Deep understanding of Java's concurrency tools can significantly improve your system’s performance, scalability, and maintainability.",{"type":26,"tag":1849,"props":3106,"children":3107},{},[3108],{"type":35,"value":1853},{"title":8,"searchDepth":106,"depth":106,"links":3110},[3111,3112,3115,3120,3124,3125],{"id":1895,"depth":106,"text":1898},{"id":1972,"depth":106,"text":1975,"children":3113},[3114],{"id":2246,"depth":116,"text":2249},{"id":2290,"depth":106,"text":2293,"children":3116},[3117,3118,3119],{"id":2316,"depth":116,"text":2319},{"id":2622,"depth":116,"text":2625},{"id":2675,"depth":116,"text":2678},{"id":2699,"depth":106,"text":2702,"children":3121},[3122,3123],{"id":2710,"depth":116,"text":2713},{"id":3018,"depth":116,"text":3021},{"id":1778,"depth":106,"text":1781},{"id":1809,"depth":106,"text":1812},"content:posts:mastering-concurrency.md","posts/mastering-concurrency.md","posts/mastering-concurrency",1790510064615]