<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6" -->

---
title: How can you limit the number of concurrent requests? -...
description: Three approaches to limiting concurrent access to a resource in Java are compared: using a Semaphore (the recommended, explicit approach), using a fixed-thread...
canonical: https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How can you limit the number of concurrent requests? - Cracking the Java Coding Interview | daily.dev
og:description: Three approaches to limiting concurrent access to a resource in Java are compared: using a Semaphore (the recommended, explicit approach), using a fixed-thread...
og:url: https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6
og:image: https://api.daily.dev/og/posts/gXpLVyag6.png
og:image:alt: How can you limit the number of concurrent requests? - Cracking the Java Coding Interview
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How can you limit the number of concurrent requests? - Cracking the Java Coding Interview

**[Java \(Official Oracle\)](https://daily.dev/sources/java-oracle)** · 1 min read · 0 upvotes · 1 comments

## Summary

Three approaches to limiting concurrent access to a resource in Java are compared: using a Semaphore (the recommended, explicit approach), using a fixed-thread ExecutorService (works but hides intent and is risky), and using the Stream Gatherers API with `Gatherers.mapConcurrent()` which internally uses a semaphore and runs each mapping in a virtual thread. A warning is given to avoid using the gatherer pattern with parallel streams.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=q-coQ6MBjeE>

## Questions this post answers

### How do I limit the number of concurrent requests to a resource in Java?

Use a Semaphore from the JDK, since it makes the intent of limiting concurrent access explicit in the code. An alternative is gatherers.mapConcurrent from Stream Gatherers, which takes a max concurrency integer and a mapper, running each mapping on a virtual thread while internally using a semaphore to cap active mappings.

_daily.dev surfaces concurrency patterns like this for developers designing safer request-limiting code._

### Why shouldn't I use a fixed-size executor service to limit concurrency in Java?

Fixing the number of threads in an executor service works but hides the true intent of limiting concurrent access, making it a dangerous, opaque pattern for an application. A Semaphore or Stream Gatherers' mapConcurrent expresses the limiting intent directly in code, which is safer and clearer to maintain.

_Developers comparing Java concurrency approaches can find these trade-offs discussed on daily.dev._

### Can I use gatherers.mapConcurrent with a parallel stream in Java?

No, mapConcurrent from Stream Gatherers should not be used with a parallel stream, as doing so causes serious problems. It should be used with a normal, sequential stream, where each mapping runs on a virtual thread and concurrency is capped by the max concurrency integer passed to it, keeping the code simple and correct.

_daily.dev helps Java developers avoid subtle concurrency pitfalls like misusing parallel streams._

## Community discussion

Top comments from developers on daily.dev.

**@henrybrown** · 0 upvotes

> For Kotlin code using coroutines we can use `kotlinx.coroutines.sync.Semaphore` rather than `java.util.concurrent.Semaphore` :
>
> ```kotlin
> val semaphore = Semaphore(permits = 10)
> semaphore.withPermit {
>     launch { service.readData() }
> }
> ```
>
> The Kotlin version is suspending, not thread-blocking.

## Similar posts on daily.dev

- [JEP Cafe \#24 – Inside.java](https://daily.dev/posts/jep-cafe-24-inside-java-qfjrlhjaw) · Inside Java · 0 upvotes · 0 comments
- [Virtual Threads After JDK 24: What Changed for Production Java](https://daily.dev/posts/virtual-threads-after-jdk-24-what-changed-for-production-java-1iad53gy7) · InfoQ · 0 upvotes · 0 comments
- [High-performance programming with Java streams](https://daily.dev/posts/high-performance-programming-with-java-streams-sh9mglzje) · InfoWorld · 0 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java)

[View this post on daily.dev](https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How can you limit the number of concurrent requests? - Cracking the Java Coding Interview","url":"https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6"},"datePublished":"2026-07-22T10:14:45.982Z","dateModified":"2026-09-13T20:54:45.793Z","description":"Three approaches to limiting concurrent access to a resource in Java are compared: using a Semaphore (the recommended, explicit approach), using a fixed-thread...","image":"https://i.ytimg.com/vi/q-coQ6MBjeE/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/q-coQ6MBjeE/sddefault.jpg","isAccessibleForFree":true,"articleSection":"Java (Official Oracle)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Java (Official Oracle)","logo":"https://media.daily.dev/image/upload/s--SM9hIYUY--/f_auto,q_auto/v1773648791/logos/java-oracle?_a=BAMAMiiu0","url":"https://daily.dev/sources/java-oracle"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"java","timeRequired":"PT1M","video":{"@type":"VideoObject","name":"How can you limit the number of concurrent requests? - Cracking the Java Coding Interview","description":"Three approaches to limiting concurrent access to a resource in Java are compared: using a Semaphore (the recommended, explicit approach), using a fixed-thread...","thumbnailUrl":"https://i.ytimg.com/vi/q-coQ6MBjeE/sddefault.jpg","uploadDate":"2026-07-22T10:14:45.982Z","duration":"PT1M","url":"https://api.daily.dev/r/gXpLVyag6","embedUrl":"https://www.youtube.com/embed/q-coQ6MBjeE"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Java (Official Oracle)","item":"https://daily.dev/sources/java-oracle"},{"@type":"ListItem","position":3,"name":"How can you limit the number of concurrent requests? - Cracking the Java Coding Interview"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6","comment":[{"@type":"Comment","text":"For Kotlin code using coroutines we can use kotlinx.coroutines.sync.Semaphore rather than java.util.concurrent.Semaphore :\nval semaphore = Semaphore(permits = 10)\nsemaphore.withPermit {\n    launch { service.readData() }\n}\n\nThe Kotlin version is suspending, not thread-blocking.","datePublished":"2026-07-22T14:19:15.999Z","url":"https://daily.dev/posts/gXpLVyag6#c-2GqeRFHym","author":{"@type":"Person","name":"Henry Brown","url":"https://daily.dev/henrybrown","image":"https://media.daily.dev/image/upload/s--xh6Y11mn--/f_auto/v1785596734/avatars/avatar_ar2RGt16f6KdqrVRTRuue?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-can-you-limit-the-number-of-concurrent-requests---cracking-the-java-coding-interview-gxplvyag6#faq","mainEntity":[{"@type":"Question","name":"How do I limit the number of concurrent requests to a resource in Java?","acceptedAnswer":{"@type":"Answer","text":"Use a Semaphore from the JDK, since it makes the intent of limiting concurrent access explicit in the code. An alternative is gatherers.mapConcurrent from Stream Gatherers, which takes a max concurrency integer and a mapper, running each mapping on a virtual thread while internally using a semaphore to cap active mappings. daily.dev surfaces concurrency patterns like this for developers designing safer request-limiting code."}},{"@type":"Question","name":"Why shouldn't I use a fixed-size executor service to limit concurrency in Java?","acceptedAnswer":{"@type":"Answer","text":"Fixing the number of threads in an executor service works but hides the true intent of limiting concurrent access, making it a dangerous, opaque pattern for an application. A Semaphore or Stream Gatherers' mapConcurrent expresses the limiting intent directly in code, which is safer and clearer to maintain. Developers comparing Java concurrency approaches can find these trade-offs discussed on daily.dev."}},{"@type":"Question","name":"Can I use gatherers.mapConcurrent with a parallel stream in Java?","acceptedAnswer":{"@type":"Answer","text":"No, mapConcurrent from Stream Gatherers should not be used with a parallel stream, as doing so causes serious problems. It should be used with a normal, sequential stream, where each mapping runs on a virtual thread and concurrency is capped by the max concurrency integer passed to it, keeping the code simple and correct. daily.dev helps Java developers avoid subtle concurrency pitfalls like misusing parallel streams."}}]}
```

