[{"data":1,"prerenderedAt":1864},["ShallowReactive",2],{"page-en-\u002Fblog\u002Fspring-boot-virtual-threads":3,"related-en-\u002Fblog\u002Fspring-boot-virtual-threads":1845},{"id":4,"title":5,"body":6,"date":1832,"description":1833,"extension":1834,"meta":1835,"navigation":178,"path":1836,"seo":1837,"stem":1838,"tags":1839,"translation":1843,"__hash__":1844},"pages_en\u002Fen\u002Fblog\u002Fspring-boot-virtual-threads.md","Virtual Threads in Spring Boot with Java 25",{"type":7,"value":8,"toc":1818},"minimark",[9,14,23,26,52,56,59,90,93,135,144,147,261,265,274,280,298,305,309,316,341,348,655,661,665,679,1009,1022,1026,1040,1200,1210,1214,1225,1454,1464,1468,1475,1510,1513,1543,1550,1554,1557,1632,1642,1680,1683,1687,1777,1781,1801,1805,1814],[10,11,13],"h2",{"id":12},"introduction","Introduction",[15,16,17,18,22],"p",{},"Virtual threads, final since Java 21, made the \"one thread per request\" model scalable again. In Java 21, however, ",[19,20,21],"code",{},"synchronized"," blocks pinned a virtual thread to its carrier thread, which caused unexpected bottlenecks in many libraries. JEP 491 in Java 24 largely solved that problem, and Java 25 LTS is the best target for running virtual threads in production with confidence.",[15,24,25],{},"This post covers enabling virtual threads in a Spring Boot application, what changes, the bottlenecks to watch out for and how to observe them.",[27,28,33],"pre",{"className":29,"code":30,"language":31,"meta":32,"style":32},"language-bash shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","java -version          # openjdk version \"25\"\n","bash","",[19,34,35],{"__ignoreMap":32},[36,37,40,44,48],"span",{"class":38,"line":39},"line",1,[36,41,43],{"class":42},"sBMFI","java",[36,45,47],{"class":46},"sfazB"," -version",[36,49,51],{"class":50},"sHwdD","          # openjdk version \"25\"\n",[10,53,55],{"id":54},"_1-enabling-it-with-one-line","1. Enabling it with one line",[15,57,58],{},"For Spring Boot 3.2 and later (Boot 4 included):",[27,60,64],{"className":61,"code":62,"language":63,"meta":32,"style":32},"language-properties shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","spring.threads.virtual.enabled=true\nspring.main.keep-alive=true\n","properties",[19,65,66,80],{"__ignoreMap":32},[36,67,68,72,76],{"class":38,"line":39},[36,69,71],{"class":70},"swJcz","spring.threads.virtual.enabled",[36,73,75],{"class":74},"sMK4o","=",[36,77,79],{"class":78},"sTEyZ","true\n",[36,81,83,86,88],{"class":38,"line":82},2,[36,84,85],{"class":70},"spring.main.keep-alive",[36,87,75],{"class":74},[36,89,79],{"class":78},[15,91,92],{},"With this setting, Spring Boot moves the following onto virtual threads:",[94,95,96,104,121,129],"ul",{},[97,98,99,103],"li",{},[100,101,102],"strong",{},"Web requests:"," Tomcat and Jetty handle every incoming HTTP request on a virtual thread.",[97,105,106,112,113,116,117,120],{},[100,107,108,111],{},[19,109,110],{},"@Async",":"," The default ",[19,114,115],{},"applicationTaskExecutor"," becomes a ",[19,118,119],{},"SimpleAsyncTaskExecutor"," that starts a new virtual thread per task.",[97,122,123,128],{},[100,124,125,111],{},[19,126,127],{},"@Scheduled"," Scheduled tasks run on virtual threads.",[97,130,131,134],{},[100,132,133],{},"Messaging:"," Listener containers such as Kafka and RabbitMQ are configured to use virtual threads.",[15,136,137,140,141,143],{},[19,138,139],{},"spring.main.keep-alive=true"," matters: virtual threads are daemon threads. If your application only has ",[19,142,127],{}," tasks and no platform thread keeps the JVM alive, the JVM may exit immediately. This setting guarantees that the JVM stays up.",[15,145,146],{},"A simple endpoint to check:",[27,148,151],{"className":149,"code":150,"language":43,"meta":32,"style":32},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","@RestController\nclass ThreadController {\n\n    @GetMapping(\"\u002Fthread\")\n    String thread() {\n        return Thread.currentThread().toString();\n        \u002F\u002F VirtualThread[#52]\u002Frunnable@ForkJoinPool-1-worker-3\n    }\n}\n",[19,152,153,162,173,180,203,218,243,249,255],{"__ignoreMap":32},[36,154,155,158],{"class":38,"line":39},[36,156,157],{"class":74},"@",[36,159,161],{"class":160},"spNyl","RestController\n",[36,163,164,167,170],{"class":38,"line":82},[36,165,166],{"class":160},"class",[36,168,169],{"class":42}," ThreadController",[36,171,172],{"class":74}," {\n",[36,174,176],{"class":38,"line":175},3,[36,177,179],{"emptyLinePlaceholder":178},true,"\n",[36,181,183,186,189,192,195,198,200],{"class":38,"line":182},4,[36,184,185],{"class":74},"    @",[36,187,188],{"class":160},"GetMapping",[36,190,191],{"class":74},"(",[36,193,194],{"class":74},"\"",[36,196,197],{"class":46},"\u002Fthread",[36,199,194],{"class":74},[36,201,202],{"class":74},")\n",[36,204,206,209,213,216],{"class":38,"line":205},5,[36,207,208],{"class":160},"    String",[36,210,212],{"class":211},"s2Zo4"," thread",[36,214,215],{"class":74},"()",[36,217,172],{"class":74},[36,219,221,225,228,231,234,237,240],{"class":38,"line":220},6,[36,222,224],{"class":223},"s7zQu","        return",[36,226,227],{"class":78}," Thread",[36,229,230],{"class":74},".",[36,232,233],{"class":211},"currentThread",[36,235,236],{"class":74},"().",[36,238,239],{"class":211},"toString",[36,241,242],{"class":74},"();\n",[36,244,246],{"class":38,"line":245},7,[36,247,248],{"class":50},"        \u002F\u002F VirtualThread[#52]\u002Frunnable@ForkJoinPool-1-worker-3\n",[36,250,252],{"class":38,"line":251},8,[36,253,254],{"class":74},"    }\n",[36,256,258],{"class":38,"line":257},9,[36,259,260],{"class":74},"}\n",[10,262,264],{"id":263},"_2-why-java-24-matters-jep-491","2. Why Java 24+ matters: JEP 491",[15,266,267,268,270,271,273],{},"In Java 21, if a virtual thread made a blocking I\u002FO call inside a ",[19,269,21],{}," block, it could not unmount from its carrier platform thread. Since the number of carriers defaults to the number of CPU cores, a handful of pinned threads could stall the whole application. JDBC drivers, connection pools and older libraries use ",[19,272,21],{},", so this was a serious problem.",[15,275,276,277,279],{},"With JEP 491 in Java 24, ",[19,278,21],{}," no longer causes pinning. Cases that still pin:",[94,281,282,288],{},[97,283,284,287],{},[100,285,286],{},"Native frames:"," Blocking inside native code called through JNI or the Foreign Function API.",[97,289,290,293,294,297],{},[100,291,292],{},"Class initializers:"," Blocking inside a class's static initializer (",[19,295,296],{},"static { }",").",[15,299,300,301,304],{},"In practice, the refactorings to ",[19,302,303],{},"ReentrantLock"," you did for Java 21 are no longer required.",[10,306,308],{"id":307},"_3-the-real-bottleneck-the-connection-pool","3. The real bottleneck: the connection pool",[15,310,311,312,315],{},"Virtual threads happily accept thousands of concurrent requests, but your database will not accept thousands of connections. When the HikariCP pool is exhausted, threads wait for a connection; that wait is cheap, but if ",[19,313,314],{},"connection-timeout"," is exceeded the request fails.",[27,317,319],{"className":61,"code":318,"language":63,"meta":32,"style":32},"spring.datasource.hikari.maximum-pool-size=30\nspring.datasource.hikari.connection-timeout=5000\n",[19,320,321,331],{"__ignoreMap":32},[36,322,323,326,328],{"class":38,"line":39},[36,324,325],{"class":70},"spring.datasource.hikari.maximum-pool-size",[36,327,75],{"class":74},[36,329,330],{"class":78},"30\n",[36,332,333,336,338],{"class":38,"line":82},[36,334,335],{"class":70},"spring.datasource.hikari.connection-timeout",[36,337,75],{"class":74},[36,339,340],{"class":78},"5000\n",[15,342,343,344,347],{},"Blindly growing the pool is not the answer; the database can only handle so many connections. The same applies to outbound calls: you don't want to send 5,000 concurrent requests to a rate-limited API. Use a ",[19,345,346],{},"Semaphore"," to limit concurrency:",[27,349,351],{"className":149,"code":350,"language":43,"meta":32,"style":32},"@Service\nclass PaymentGateway {\n\n    private final Semaphore permits = new Semaphore(50);\n    private final RestClient restClient;\n\n    PaymentGateway(RestClient.Builder builder) {\n        this.restClient = builder.baseUrl(\"https:\u002F\u002Fpayments.example.com\").build();\n    }\n\n    PaymentResponse charge(PaymentRequest request) throws InterruptedException {\n        permits.acquire();\n        try {\n            return restClient.post()\n                    .uri(\"\u002Fcharges\")\n                    .body(request)\n                    .retrieve()\n                    .body(PaymentResponse.class);\n        } finally {\n            permits.release();\n        }\n    }\n}\n",[19,352,353,360,369,373,403,418,422,446,479,483,488,515,528,536,552,572,587,597,615,626,639,645,650],{"__ignoreMap":32},[36,354,355,357],{"class":38,"line":39},[36,356,157],{"class":74},[36,358,359],{"class":160},"Service\n",[36,361,362,364,367],{"class":38,"line":82},[36,363,166],{"class":160},[36,365,366],{"class":42}," PaymentGateway",[36,368,172],{"class":74},[36,370,371],{"class":38,"line":175},[36,372,179],{"emptyLinePlaceholder":178},[36,374,375,378,381,384,387,389,392,394,396,400],{"class":38,"line":182},[36,376,377],{"class":160},"    private",[36,379,380],{"class":160}," final",[36,382,383],{"class":160}," Semaphore",[36,385,386],{"class":78}," permits ",[36,388,75],{"class":74},[36,390,391],{"class":223}," new",[36,393,383],{"class":211},[36,395,191],{"class":74},[36,397,399],{"class":398},"sbssI","50",[36,401,402],{"class":74},");\n",[36,404,405,407,409,412,415],{"class":38,"line":205},[36,406,377],{"class":160},[36,408,380],{"class":160},[36,410,411],{"class":160}," RestClient",[36,413,414],{"class":78}," restClient",[36,416,417],{"class":74},";\n",[36,419,420],{"class":38,"line":220},[36,421,179],{"emptyLinePlaceholder":178},[36,423,424,427,429,432,434,437,441,444],{"class":38,"line":245},[36,425,426],{"class":211},"    PaymentGateway",[36,428,191],{"class":74},[36,430,431],{"class":160},"RestClient",[36,433,230],{"class":74},[36,435,436],{"class":160},"Builder",[36,438,440],{"class":439},"sHdIc"," builder",[36,442,443],{"class":74},")",[36,445,172],{"class":74},[36,447,448,451,454,456,458,460,463,465,467,470,472,474,477],{"class":38,"line":251},[36,449,450],{"class":74},"        this.",[36,452,453],{"class":78},"restClient ",[36,455,75],{"class":74},[36,457,440],{"class":78},[36,459,230],{"class":74},[36,461,462],{"class":211},"baseUrl",[36,464,191],{"class":74},[36,466,194],{"class":74},[36,468,469],{"class":46},"https:\u002F\u002Fpayments.example.com",[36,471,194],{"class":74},[36,473,297],{"class":74},[36,475,476],{"class":211},"build",[36,478,242],{"class":74},[36,480,481],{"class":38,"line":257},[36,482,254],{"class":74},[36,484,486],{"class":38,"line":485},10,[36,487,179],{"emptyLinePlaceholder":178},[36,489,491,494,497,499,502,505,507,510,513],{"class":38,"line":490},11,[36,492,493],{"class":160},"    PaymentResponse",[36,495,496],{"class":211}," charge",[36,498,191],{"class":74},[36,500,501],{"class":160},"PaymentRequest",[36,503,504],{"class":439}," request",[36,506,443],{"class":74},[36,508,509],{"class":160}," throws",[36,511,512],{"class":160}," InterruptedException",[36,514,172],{"class":74},[36,516,518,521,523,526],{"class":38,"line":517},12,[36,519,520],{"class":78},"        permits",[36,522,230],{"class":74},[36,524,525],{"class":211},"acquire",[36,527,242],{"class":74},[36,529,531,534],{"class":38,"line":530},13,[36,532,533],{"class":223},"        try",[36,535,172],{"class":74},[36,537,539,542,544,546,549],{"class":38,"line":538},14,[36,540,541],{"class":223},"            return",[36,543,414],{"class":78},[36,545,230],{"class":74},[36,547,548],{"class":211},"post",[36,550,551],{"class":74},"()\n",[36,553,555,558,561,563,565,568,570],{"class":38,"line":554},15,[36,556,557],{"class":74},"                    .",[36,559,560],{"class":211},"uri",[36,562,191],{"class":74},[36,564,194],{"class":74},[36,566,567],{"class":46},"\u002Fcharges",[36,569,194],{"class":74},[36,571,202],{"class":74},[36,573,575,577,580,582,585],{"class":38,"line":574},16,[36,576,557],{"class":74},[36,578,579],{"class":211},"body",[36,581,191],{"class":74},[36,583,584],{"class":78},"request",[36,586,202],{"class":74},[36,588,590,592,595],{"class":38,"line":589},17,[36,591,557],{"class":74},[36,593,594],{"class":211},"retrieve",[36,596,551],{"class":74},[36,598,600,602,604,606,609,611,613],{"class":38,"line":599},18,[36,601,557],{"class":74},[36,603,579],{"class":211},[36,605,191],{"class":74},[36,607,608],{"class":78},"PaymentResponse",[36,610,230],{"class":74},[36,612,166],{"class":78},[36,614,402],{"class":74},[36,616,618,621,624],{"class":38,"line":617},19,[36,619,620],{"class":74},"        }",[36,622,623],{"class":223}," finally",[36,625,172],{"class":74},[36,627,629,632,634,637],{"class":38,"line":628},20,[36,630,631],{"class":78},"            permits",[36,633,230],{"class":74},[36,635,636],{"class":211},"release",[36,638,242],{"class":74},[36,640,642],{"class":38,"line":641},21,[36,643,644],{"class":74},"        }\n",[36,646,648],{"class":38,"line":647},22,[36,649,254],{"class":74},[36,651,653],{"class":38,"line":652},23,[36,654,260],{"class":74},[15,656,657,658,230],{},"In the platform-thread world, the pool size limited concurrency; with virtual threads you have to set that limit ",[100,659,660],{},"explicitly",[10,662,664],{"id":663},"_4-blocking-code-is-normal-again","4. Blocking code is normal again",[15,666,667,668,670,671,674,675,678],{},"With virtual threads, blocking APIs such as ",[19,669,431],{},", ",[19,672,673],{},"JdbcClient"," and ",[19,676,677],{},"JdbcTemplate"," scale again. You can write simple, readable code without moving to reactive programming:",[27,680,682],{"className":149,"code":681,"language":43,"meta":32,"style":32},"@Service\nclass OrderService {\n\n    private final JdbcClient jdbc;\n    private final RestClient inventory;\n\n    OrderService(JdbcClient jdbc, RestClient.Builder builder) {\n        this.jdbc = jdbc;\n        this.inventory = builder.baseUrl(\"http:\u002F\u002Finventory\").build();\n    }\n\n    OrderView find(long id) {\n        var order = jdbc.sql(\"select id, product_id, quantity from orders where id = :id\")\n                .param(\"id\", id)\n                .query(Order.class)\n                .single();\n        var stock = inventory.get()\n                .uri(\"\u002Fstock\u002F{productId}\", order.productId())\n                .retrieve()\n                .body(Stock.class);\n        return new OrderView(order, stock);\n    }\n}\n",[19,683,684,690,699,703,717,730,734,760,773,803,807,811,831,859,882,900,909,927,955,963,980,1001,1005],{"__ignoreMap":32},[36,685,686,688],{"class":38,"line":39},[36,687,157],{"class":74},[36,689,359],{"class":160},[36,691,692,694,697],{"class":38,"line":82},[36,693,166],{"class":160},[36,695,696],{"class":42}," OrderService",[36,698,172],{"class":74},[36,700,701],{"class":38,"line":175},[36,702,179],{"emptyLinePlaceholder":178},[36,704,705,707,709,712,715],{"class":38,"line":182},[36,706,377],{"class":160},[36,708,380],{"class":160},[36,710,711],{"class":160}," JdbcClient",[36,713,714],{"class":78}," jdbc",[36,716,417],{"class":74},[36,718,719,721,723,725,728],{"class":38,"line":205},[36,720,377],{"class":160},[36,722,380],{"class":160},[36,724,411],{"class":160},[36,726,727],{"class":78}," inventory",[36,729,417],{"class":74},[36,731,732],{"class":38,"line":220},[36,733,179],{"emptyLinePlaceholder":178},[36,735,736,739,741,743,745,748,750,752,754,756,758],{"class":38,"line":245},[36,737,738],{"class":211},"    OrderService",[36,740,191],{"class":74},[36,742,673],{"class":160},[36,744,714],{"class":439},[36,746,747],{"class":74},",",[36,749,411],{"class":160},[36,751,230],{"class":74},[36,753,436],{"class":160},[36,755,440],{"class":439},[36,757,443],{"class":74},[36,759,172],{"class":74},[36,761,762,764,767,769,771],{"class":38,"line":251},[36,763,450],{"class":74},[36,765,766],{"class":78},"jdbc ",[36,768,75],{"class":74},[36,770,714],{"class":78},[36,772,417],{"class":74},[36,774,775,777,780,782,784,786,788,790,792,795,797,799,801],{"class":38,"line":257},[36,776,450],{"class":74},[36,778,779],{"class":78},"inventory ",[36,781,75],{"class":74},[36,783,440],{"class":78},[36,785,230],{"class":74},[36,787,462],{"class":211},[36,789,191],{"class":74},[36,791,194],{"class":74},[36,793,794],{"class":46},"http:\u002F\u002Finventory",[36,796,194],{"class":74},[36,798,297],{"class":74},[36,800,476],{"class":211},[36,802,242],{"class":74},[36,804,805],{"class":38,"line":485},[36,806,254],{"class":74},[36,808,809],{"class":38,"line":490},[36,810,179],{"emptyLinePlaceholder":178},[36,812,813,816,819,821,824,827,829],{"class":38,"line":517},[36,814,815],{"class":160},"    OrderView",[36,817,818],{"class":211}," find",[36,820,191],{"class":74},[36,822,823],{"class":160},"long",[36,825,826],{"class":439}," id",[36,828,443],{"class":74},[36,830,172],{"class":74},[36,832,833,836,839,841,843,845,848,850,852,855,857],{"class":38,"line":530},[36,834,835],{"class":160},"        var",[36,837,838],{"class":78}," order ",[36,840,75],{"class":74},[36,842,714],{"class":78},[36,844,230],{"class":74},[36,846,847],{"class":211},"sql",[36,849,191],{"class":74},[36,851,194],{"class":74},[36,853,854],{"class":46},"select id, product_id, quantity from orders where id = :id",[36,856,194],{"class":74},[36,858,202],{"class":74},[36,860,861,864,867,869,871,874,876,878,880],{"class":38,"line":538},[36,862,863],{"class":74},"                .",[36,865,866],{"class":211},"param",[36,868,191],{"class":74},[36,870,194],{"class":74},[36,872,873],{"class":46},"id",[36,875,194],{"class":74},[36,877,747],{"class":74},[36,879,826],{"class":78},[36,881,202],{"class":74},[36,883,884,886,889,891,894,896,898],{"class":38,"line":554},[36,885,863],{"class":74},[36,887,888],{"class":211},"query",[36,890,191],{"class":74},[36,892,893],{"class":78},"Order",[36,895,230],{"class":74},[36,897,166],{"class":78},[36,899,202],{"class":74},[36,901,902,904,907],{"class":38,"line":574},[36,903,863],{"class":74},[36,905,906],{"class":211},"single",[36,908,242],{"class":74},[36,910,911,913,916,918,920,922,925],{"class":38,"line":589},[36,912,835],{"class":160},[36,914,915],{"class":78}," stock ",[36,917,75],{"class":74},[36,919,727],{"class":78},[36,921,230],{"class":74},[36,923,924],{"class":211},"get",[36,926,551],{"class":74},[36,928,929,931,933,935,937,940,942,944,947,949,952],{"class":38,"line":599},[36,930,863],{"class":74},[36,932,560],{"class":211},[36,934,191],{"class":74},[36,936,194],{"class":74},[36,938,939],{"class":46},"\u002Fstock\u002F{productId}",[36,941,194],{"class":74},[36,943,747],{"class":74},[36,945,946],{"class":78}," order",[36,948,230],{"class":74},[36,950,951],{"class":211},"productId",[36,953,954],{"class":74},"())\n",[36,956,957,959,961],{"class":38,"line":617},[36,958,863],{"class":74},[36,960,594],{"class":211},[36,962,551],{"class":74},[36,964,965,967,969,971,974,976,978],{"class":38,"line":628},[36,966,863],{"class":74},[36,968,579],{"class":211},[36,970,191],{"class":74},[36,972,973],{"class":78},"Stock",[36,975,230],{"class":74},[36,977,166],{"class":78},[36,979,402],{"class":74},[36,981,982,984,986,989,991,994,996,999],{"class":38,"line":641},[36,983,224],{"class":223},[36,985,391],{"class":223},[36,987,988],{"class":211}," OrderView",[36,990,191],{"class":74},[36,992,993],{"class":78},"order",[36,995,747],{"class":74},[36,997,998],{"class":78}," stock",[36,1000,402],{"class":74},[36,1002,1003],{"class":38,"line":647},[36,1004,254],{"class":74},[36,1006,1007],{"class":38,"line":652},[36,1008,260],{"class":74},[15,1010,1011,1014,1015,1018,1019,230],{},[100,1012,1013],{},"Don't pool virtual threads."," Passing a virtual thread factory to ",[19,1016,1017],{},"Executors.newFixedThreadPool(...)"," defeats the whole purpose. Create a new one per task: ",[19,1020,1021],{},"Executors.newVirtualThreadPerTaskExecutor()",[10,1023,1025],{"id":1024},"_5-scoped-values-instead-of-threadlocal","5. Scoped values instead of ThreadLocal",[15,1027,1028,1029,1032,1033,1035,1036,1039],{},"Each of millions of virtual threads may carry its own ",[19,1030,1031],{},"ThreadLocal"," copy; using ",[19,1034,1031],{}," to cache large objects (formatters, buffers) can turn into a memory problem. ",[100,1037,1038],{},"Scoped values"," (JEP 506), final in Java 25, offer an immutable alternative bound to a well-defined scope:",[27,1041,1043],{"className":149,"code":1042,"language":43,"meta":32,"style":32},"public final class RequestContext {\n    public static final ScopedValue\u003CString> TENANT = ScopedValue.newInstance();\n}\n\n\u002F\u002F In a filter\nScopedValue.where(RequestContext.TENANT, tenantId)\n        .run(() -> chain.doFilter(request, response));\n\n\u002F\u002F Deep inside a service\nString tenant = RequestContext.TENANT.get();\n",[19,1044,1045,1060,1096,1100,1104,1109,1136,1170,1174,1179],{"__ignoreMap":32},[36,1046,1047,1050,1052,1055,1058],{"class":38,"line":39},[36,1048,1049],{"class":160},"public",[36,1051,380],{"class":160},[36,1053,1054],{"class":160}," class",[36,1056,1057],{"class":42}," RequestContext",[36,1059,172],{"class":74},[36,1061,1062,1065,1068,1070,1073,1076,1079,1082,1085,1087,1089,1091,1094],{"class":38,"line":82},[36,1063,1064],{"class":160},"    public",[36,1066,1067],{"class":160}," static",[36,1069,380],{"class":160},[36,1071,1072],{"class":160}," ScopedValue",[36,1074,1075],{"class":74},"\u003C",[36,1077,1078],{"class":160},"String",[36,1080,1081],{"class":74},">",[36,1083,1084],{"class":78}," TENANT ",[36,1086,75],{"class":74},[36,1088,1072],{"class":78},[36,1090,230],{"class":74},[36,1092,1093],{"class":211},"newInstance",[36,1095,242],{"class":74},[36,1097,1098],{"class":38,"line":175},[36,1099,260],{"class":74},[36,1101,1102],{"class":38,"line":182},[36,1103,179],{"emptyLinePlaceholder":178},[36,1105,1106],{"class":38,"line":205},[36,1107,1108],{"class":50},"\u002F\u002F In a filter\n",[36,1110,1111,1114,1116,1119,1121,1124,1126,1129,1131,1134],{"class":38,"line":220},[36,1112,1113],{"class":78},"ScopedValue",[36,1115,230],{"class":74},[36,1117,1118],{"class":211},"where",[36,1120,191],{"class":74},[36,1122,1123],{"class":78},"RequestContext",[36,1125,230],{"class":74},[36,1127,1128],{"class":78},"TENANT",[36,1130,747],{"class":74},[36,1132,1133],{"class":78}," tenantId",[36,1135,202],{"class":74},[36,1137,1138,1141,1144,1147,1150,1153,1155,1158,1160,1162,1164,1167],{"class":38,"line":245},[36,1139,1140],{"class":74},"        .",[36,1142,1143],{"class":211},"run",[36,1145,1146],{"class":74},"(()",[36,1148,1149],{"class":160}," ->",[36,1151,1152],{"class":78}," chain",[36,1154,230],{"class":74},[36,1156,1157],{"class":211},"doFilter",[36,1159,191],{"class":74},[36,1161,584],{"class":78},[36,1163,747],{"class":74},[36,1165,1166],{"class":78}," response",[36,1168,1169],{"class":74},"));\n",[36,1171,1172],{"class":38,"line":251},[36,1173,179],{"emptyLinePlaceholder":178},[36,1175,1176],{"class":38,"line":257},[36,1177,1178],{"class":50},"\u002F\u002F Deep inside a service\n",[36,1180,1181,1183,1186,1188,1190,1192,1194,1196,1198],{"class":38,"line":485},[36,1182,1078],{"class":160},[36,1184,1185],{"class":78}," tenant ",[36,1187,75],{"class":74},[36,1189,1057],{"class":78},[36,1191,230],{"class":74},[36,1193,1128],{"class":78},[36,1195,230],{"class":74},[36,1197,924],{"class":211},[36,1199,242],{"class":74},[15,1201,1202,1203,1206,1207,1209],{},"The value disappears automatically when the scope ends; there is no risk of forgetting to call ",[19,1204,1205],{},"remove()",". Libraries (Spring Security, MDC and so on) still use ",[19,1208,1031],{},", so a full migration isn't possible yet, but prefer scoped values in your own code.",[10,1211,1213],{"id":1212},"_6-parallel-calls-with-structured-concurrency","6. Parallel calls with structured concurrency",[15,1215,1216,1217,1220,1221,1224],{},"To call several services in parallel within one request, structured concurrency (still a ",[100,1218,1219],{},"preview"," in Java 25, requires ",[19,1222,1223],{},"--enable-preview",") is a great fit:",[27,1226,1228],{"className":149,"code":1227,"language":43,"meta":32,"style":32},"ProductPage load(long productId) throws InterruptedException {\n    try (var scope = StructuredTaskScope.open()) {\n        var product = scope.fork(() -> catalog.product(productId));\n        var reviews = scope.fork(() -> reviewService.reviews(productId));\n        var price   = scope.fork(() -> pricing.price(productId));\n\n        scope.join();   \u002F\u002F if one fails, the others are cancelled\n\n        return new ProductPage(product.get(), reviews.get(), price.get());\n    }\n}\n",[19,1229,1230,1253,1282,1317,1350,1383,1387,1403,1407,1446,1450],{"__ignoreMap":32},[36,1231,1232,1235,1238,1240,1242,1245,1247,1250],{"class":38,"line":39},[36,1233,1234],{"class":160},"ProductPage",[36,1236,1237],{"class":211}," load",[36,1239,191],{"class":74},[36,1241,823],{"class":160},[36,1243,1244],{"class":78}," productId",[36,1246,443],{"class":74},[36,1248,1249],{"class":78}," throws InterruptedException ",[36,1251,1252],{"class":74},"{\n",[36,1254,1255,1258,1261,1264,1267,1269,1272,1274,1277,1280],{"class":38,"line":82},[36,1256,1257],{"class":223},"    try",[36,1259,1260],{"class":74}," (",[36,1262,1263],{"class":160},"var",[36,1265,1266],{"class":78}," scope ",[36,1268,75],{"class":74},[36,1270,1271],{"class":78}," StructuredTaskScope",[36,1273,230],{"class":74},[36,1275,1276],{"class":211},"open",[36,1278,1279],{"class":74},"())",[36,1281,172],{"class":74},[36,1283,1284,1286,1289,1291,1294,1296,1299,1301,1303,1306,1308,1311,1313,1315],{"class":38,"line":175},[36,1285,835],{"class":160},[36,1287,1288],{"class":78}," product ",[36,1290,75],{"class":74},[36,1292,1293],{"class":78}," scope",[36,1295,230],{"class":74},[36,1297,1298],{"class":211},"fork",[36,1300,1146],{"class":74},[36,1302,1149],{"class":160},[36,1304,1305],{"class":78}," catalog",[36,1307,230],{"class":74},[36,1309,1310],{"class":211},"product",[36,1312,191],{"class":74},[36,1314,951],{"class":78},[36,1316,1169],{"class":74},[36,1318,1319,1321,1324,1326,1328,1330,1332,1334,1336,1339,1341,1344,1346,1348],{"class":38,"line":182},[36,1320,835],{"class":160},[36,1322,1323],{"class":78}," reviews ",[36,1325,75],{"class":74},[36,1327,1293],{"class":78},[36,1329,230],{"class":74},[36,1331,1298],{"class":211},[36,1333,1146],{"class":74},[36,1335,1149],{"class":160},[36,1337,1338],{"class":78}," reviewService",[36,1340,230],{"class":74},[36,1342,1343],{"class":211},"reviews",[36,1345,191],{"class":74},[36,1347,951],{"class":78},[36,1349,1169],{"class":74},[36,1351,1352,1354,1357,1359,1361,1363,1365,1367,1369,1372,1374,1377,1379,1381],{"class":38,"line":205},[36,1353,835],{"class":160},[36,1355,1356],{"class":78}," price   ",[36,1358,75],{"class":74},[36,1360,1293],{"class":78},[36,1362,230],{"class":74},[36,1364,1298],{"class":211},[36,1366,1146],{"class":74},[36,1368,1149],{"class":160},[36,1370,1371],{"class":78}," pricing",[36,1373,230],{"class":74},[36,1375,1376],{"class":211},"price",[36,1378,191],{"class":74},[36,1380,951],{"class":78},[36,1382,1169],{"class":74},[36,1384,1385],{"class":38,"line":220},[36,1386,179],{"emptyLinePlaceholder":178},[36,1388,1389,1392,1394,1397,1400],{"class":38,"line":245},[36,1390,1391],{"class":78},"        scope",[36,1393,230],{"class":74},[36,1395,1396],{"class":211},"join",[36,1398,1399],{"class":74},"();",[36,1401,1402],{"class":50},"   \u002F\u002F if one fails, the others are cancelled\n",[36,1404,1405],{"class":38,"line":251},[36,1406,179],{"emptyLinePlaceholder":178},[36,1408,1409,1411,1413,1416,1418,1420,1422,1424,1427,1430,1432,1434,1436,1439,1441,1443],{"class":38,"line":257},[36,1410,224],{"class":223},[36,1412,391],{"class":223},[36,1414,1415],{"class":211}," ProductPage",[36,1417,191],{"class":74},[36,1419,1310],{"class":78},[36,1421,230],{"class":74},[36,1423,924],{"class":211},[36,1425,1426],{"class":74},"(),",[36,1428,1429],{"class":78}," reviews",[36,1431,230],{"class":74},[36,1433,924],{"class":211},[36,1435,1426],{"class":74},[36,1437,1438],{"class":78}," price",[36,1440,230],{"class":74},[36,1442,924],{"class":211},[36,1444,1445],{"class":74},"());\n",[36,1447,1448],{"class":38,"line":485},[36,1449,254],{"class":74},[36,1451,1452],{"class":38,"line":490},[36,1453,260],{"class":74},[15,1455,1456,1457,1459,1460,1463],{},"Each ",[19,1458,1298],{}," starts a new virtual thread. If one subtask fails, the others are cancelled and the failure is thrown from ",[19,1461,1462],{},"join()",". There are no thread leaks; no subtask outlives the scope. The preview API has changed between releases, so check the release notes before using it in production.",[10,1465,1467],{"id":1466},"_7-observability-and-diagnostics","7. Observability and diagnostics",[15,1469,1470,1471,1474],{},"The ",[19,1472,1473],{},"-Djdk.tracePinnedThreads"," option from Java 21 has been removed. Track pinning with JFR instead:",[27,1476,1478],{"className":29,"code":1477,"language":31,"meta":32,"style":32},"java -XX:StartFlightRecording=duration=60s,filename=app.jfr -jar app.jar\njfr print --events jdk.VirtualThreadPinned app.jfr\n",[19,1479,1480,1493],{"__ignoreMap":32},[36,1481,1482,1484,1487,1490],{"class":38,"line":39},[36,1483,43],{"class":42},[36,1485,1486],{"class":46}," -XX:StartFlightRecording=duration=60s,filename=app.jfr",[36,1488,1489],{"class":46}," -jar",[36,1491,1492],{"class":46}," app.jar\n",[36,1494,1495,1498,1501,1504,1507],{"class":38,"line":82},[36,1496,1497],{"class":42},"jfr",[36,1499,1500],{"class":46}," print",[36,1502,1503],{"class":46}," --events",[36,1505,1506],{"class":46}," jdk.VirtualThreadPinned",[36,1508,1509],{"class":46}," app.jfr\n",[15,1511,1512],{},"To dump all threads (virtual ones included) of a running application:",[27,1514,1516],{"className":29,"code":1515,"language":31,"meta":32,"style":32},"jcmd \u003Cpid> Thread.dump_to_file -format=json threads.json\n",[19,1517,1518],{"__ignoreMap":32},[36,1519,1520,1523,1526,1529,1532,1534,1537,1540],{"class":38,"line":39},[36,1521,1522],{"class":42},"jcmd",[36,1524,1525],{"class":74}," \u003C",[36,1527,1528],{"class":46},"pi",[36,1530,1531],{"class":78},"d",[36,1533,1081],{"class":74},[36,1535,1536],{"class":46}," Thread.dump_to_file",[36,1538,1539],{"class":46}," -format=json",[36,1541,1542],{"class":46}," threads.json\n",[15,1544,1545,1546,1549],{},"Classic ",[19,1547,1548],{},"jstack"," output doesn't show virtual threads; the JSON dump lists them along with the executor they belong to.",[10,1551,1553],{"id":1552},"_8-a-simple-load-test","8. A simple load test",[15,1555,1556],{},"Consider an endpoint that waits 200 ms on I\u002FO:",[27,1558,1560],{"className":149,"code":1559,"language":43,"meta":32,"style":32},"@GetMapping(\"\u002Fslow\")\nString slow() throws InterruptedException {\n    Thread.sleep(200);   \u002F\u002F simulates a database \u002F HTTP call\n    return \"ok\";\n}\n",[19,1561,1562,1579,1592,1613,1628],{"__ignoreMap":32},[36,1563,1564,1566,1568,1570,1572,1575,1577],{"class":38,"line":39},[36,1565,157],{"class":74},[36,1567,188],{"class":160},[36,1569,191],{"class":74},[36,1571,194],{"class":74},[36,1573,1574],{"class":46},"\u002Fslow",[36,1576,194],{"class":74},[36,1578,202],{"class":74},[36,1580,1581,1583,1586,1588,1590],{"class":38,"line":82},[36,1582,1078],{"class":160},[36,1584,1585],{"class":211}," slow",[36,1587,215],{"class":74},[36,1589,1249],{"class":78},[36,1591,1252],{"class":74},[36,1593,1594,1597,1599,1602,1604,1607,1610],{"class":38,"line":175},[36,1595,1596],{"class":78},"    Thread",[36,1598,230],{"class":74},[36,1600,1601],{"class":211},"sleep",[36,1603,191],{"class":74},[36,1605,1606],{"class":398},"200",[36,1608,1609],{"class":74},");",[36,1611,1612],{"class":50},"   \u002F\u002F simulates a database \u002F HTTP call\n",[36,1614,1615,1618,1621,1624,1626],{"class":38,"line":182},[36,1616,1617],{"class":223},"    return",[36,1619,1620],{"class":74}," \"",[36,1622,1623],{"class":46},"ok",[36,1625,194],{"class":74},[36,1627,417],{"class":74},[36,1629,1630],{"class":38,"line":205},[36,1631,260],{"class":74},[15,1633,1634,1635,1638,1639,111],{},"1,000 concurrent connections with ",[19,1636,1637],{},"oha"," or ",[19,1640,1641],{},"hey",[27,1643,1645],{"className":29,"code":1644,"language":31,"meta":32,"style":32},"oha -z 30s -c 1000 http:\u002F\u002Flocalhost:8080\u002Fslow\nhey -z 30s -c 1000 http:\u002F\u002Flocalhost:8080\u002Fslow\n",[19,1646,1647,1666],{"__ignoreMap":32},[36,1648,1649,1651,1654,1657,1660,1663],{"class":38,"line":39},[36,1650,1637],{"class":42},[36,1652,1653],{"class":46}," -z",[36,1655,1656],{"class":46}," 30s",[36,1658,1659],{"class":46}," -c",[36,1661,1662],{"class":398}," 1000",[36,1664,1665],{"class":46}," http:\u002F\u002Flocalhost:8080\u002Fslow\n",[36,1667,1668,1670,1672,1674,1676,1678],{"class":38,"line":82},[36,1669,1641],{"class":42},[36,1671,1653],{"class":46},[36,1673,1656],{"class":46},[36,1675,1659],{"class":46},[36,1677,1662],{"class":398},[36,1679,1665],{"class":46},[15,1681,1682],{},"With Tomcat's default pool of 200 threads, throughput caps out around 1,000 requests per second (200 threads \u002F 0.2 s); with virtual threads that ceiling disappears and throughput grows with the number of concurrent connections. These numbers are purely illustrative; real results depend on your workload and on your database and downstream limits. Measure with your own scenario.",[10,1684,1686],{"id":1685},"_9-platform-thread-or-virtual-thread","9. Platform thread or virtual thread?",[1688,1689,1690,1705],"table",{},[1691,1692,1693],"thead",{},[1694,1695,1696,1699,1702],"tr",{},[1697,1698],"th",{},[1697,1700,1701],{},"Platform thread",[1697,1703,1704],{},"Virtual thread",[1706,1707,1708,1720,1731,1742,1753,1764],"tbody",{},[1694,1709,1710,1714,1717],{},[1711,1712,1713],"td",{},"Managed by",[1711,1715,1716],{},"Operating system",[1711,1718,1719],{},"JVM",[1694,1721,1722,1725,1728],{},[1711,1723,1724],{},"Memory",[1711,1726,1727],{},"~1 MB stack",[1711,1729,1730],{},"A few KB, grows as needed",[1694,1732,1733,1736,1739],{},[1711,1734,1735],{},"Count",[1711,1737,1738],{},"Thousands",[1711,1740,1741],{},"Millions",[1694,1743,1744,1747,1750],{},[1711,1745,1746],{},"Creation cost",[1711,1748,1749],{},"High, pooled",[1711,1751,1752],{},"Low, not pooled",[1694,1754,1755,1758,1761],{},[1711,1756,1757],{},"Best for",[1711,1759,1760],{},"CPU-bound work",[1711,1762,1763],{},"I\u002FO-bound work",[1694,1765,1766,1769,1772],{},[1711,1767,1768],{},"Concurrency limit",[1711,1770,1771],{},"Pool size",[1711,1773,1774,1776],{},[19,1775,346],{},", connection pool",[10,1778,1780],{"id":1779},"_10-when-not-to-use-them","10. When not to use them",[94,1782,1783,1789,1795],{},[97,1784,1785,1788],{},[100,1786,1787],{},"CPU-bound work:"," Image processing, encryption and heavy computation don't get faster with virtual threads; a bounded platform-thread pool is a better fit.",[97,1790,1791,1794],{},[100,1792,1793],{},"Already reactive applications:"," Moving a well-functioning WebFlux application to virtual threads brings little gain. For new projects, virtual threads + Spring MVC is usually the simpler choice.",[97,1796,1797,1800],{},[100,1798,1799],{},"Native-heavy work:"," Blocking in JNI calls still pins.",[10,1802,1804],{"id":1803},"conclusion","Conclusion",[15,1806,1807,1808,1810,1811,1813],{},"With Java 25, virtual threads are largely free of the pinning limitations of Java 21. In Spring Boot they are enabled with a single property and let you run blocking, readable code with high concurrency. The thing to watch is no longer the number of threads but limited resources such as database connections and downstream services. Protect them with a ",[19,1809,346],{}," and proper pool settings, prefer scoped values over ",[19,1812,1031],{},", and don't forget to measure with JFR.",[1815,1816,1817],"style",{},"html pre.shiki code .sBMFI, html code.shiki .sBMFI{--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}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);}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 .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html pre.shiki code .sbssI, html code.shiki .sbssI{--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C}html pre.shiki code .sHdIc, html code.shiki .sHdIc{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic}",{"title":32,"searchDepth":82,"depth":82,"links":1819},[1820,1821,1822,1823,1824,1825,1826,1827,1828,1829,1830,1831],{"id":12,"depth":82,"text":13},{"id":54,"depth":82,"text":55},{"id":263,"depth":82,"text":264},{"id":307,"depth":82,"text":308},{"id":663,"depth":82,"text":664},{"id":1024,"depth":82,"text":1025},{"id":1212,"depth":82,"text":1213},{"id":1466,"depth":82,"text":1467},{"id":1552,"depth":82,"text":1553},{"id":1685,"depth":82,"text":1686},{"id":1779,"depth":82,"text":1780},{"id":1803,"depth":82,"text":1804},"2026-11-19","Enabling virtual threads in Spring Boot, how Java 24+ fixed the pinning problem, the connection-pool bottleneck, scoped values, structured concurrency and observability tips.","md",{},"\u002Fen\u002Fblog\u002Fspring-boot-virtual-threads",{"title":5,"description":1833},"en\u002Fblog\u002Fspring-boot-virtual-threads",[43,1840,1841,1842],"spring","concurrency","guide","\u002Fblog\u002Fspring-boot-virtual-threads","6EDcJ-42caltXc0QwEj9GCf-LoBopKb5FGaMAcK0t0I",[1846,1852,1858],{"path":1847,"title":1848,"description":1849,"date":1850,"tags":1851},"\u002Fen\u002Fblog\u002Fspring-boot-4-whats-new","Spring Boot 4 and Spring Framework 7: What's New and How to Migrate","The main changes in Spring Boot 4 and Spring Framework 7 — Jakarta EE 11, modular auto-configuration, JSpecify null-safety, API versioning, HTTP service clients, built-in resilience, Jackson 3 and the steps to migrate from 3.5.","2026-11-18",[43,1840,1842],{"path":1853,"title":1854,"description":1855,"date":1856,"tags":1857},"\u002Fen\u002Fblog\u002Fjava-25-features","Java 25 LTS: What Changed Since Java 21?","The features that became final between Java 21 and 25 — unnamed variables, stream gatherers, compact source files, scoped values, flexible constructor bodies, the AOT cache and compact object headers.","2026-11-17",[43,1842],{"path":1859,"title":1860,"description":1861,"date":1862,"tags":1863},"\u002Fen\u002Fblog\u002Fjava-21-features","What's New in Java 21: Virtual Threads, Records and Pattern Matching","The Java 21 LTS features that change day-to-day code the most — virtual threads, records, switch pattern matching, record patterns, sealed classes and sequenced collections.","2026-11-01",[43,1842],1795055401673]