Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8851027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:02:38+00:00 2026-06-14T13:02:38+00:00

How can I limit number of threads that are being executed at the same

  • 0

How can I limit number of threads that are being executed at the same time?
Here is sample of my algorithm:

for(i = 0; i < 100000; i++) {
    Thread.start {
        // Do some work
    }
}

I would like to make sure that once number of threads in my application hits 100, algorithm will pause/wait until number of threads in the app goes below 100.

Currently “some work” takes some time to do and I end up with few thousands of threads in my app. Eventually it runs out of threads and “some work” crashes. I would like to fix it by limiting number of pools that it can use at one time.

Please let me know how to solve my issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:02:40+00:00Added an answer on June 14, 2026 at 1:02 pm

    I believe you are looking for a ThreadPoolExecutor in the Java Concurrency API. The idea here is that you can define a maximum number of threads in a pool and then instead of starting new Threads with a Runnable, just let the ThreadPoolExecutor take care of managing the upper limit for Threads.

    Start here: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html

    import java.util.concurrent.*;
    import java.util.*;
    
    def queue = new ArrayBlockingQueue<Runnable>( 50000 )
    def tPool = new ThreadPoolExecutor(5, 500, 20, TimeUnit.SECONDS, queue);
    
    for(i = 0; i < 5000; i++) {
        tPool.execute {
          println "Blah"
        }
    }
    

    Parameters for the ThreadBlockingQueue constructor: corePoolSize (5), this is the # of threads to create and to maintain if the system is idle, maxPoolSize (500) max number of threads to create, 3rd and 4th argument states that the pool should keep idle threads around for at least 20 seconds, and the queue argument is a blocking queue that stores queued tasks.

    What you’ll want to play around with is the queue sizes and also how to handle rejected tasks. If you need to execute 100k tasks, you’ll either have to have a queue that can hold 100k tasks, or you’ll have to have a strategy for handling a rejected tasks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to limit the number of characters that can be appended to an
Using cURL_multi is great for simultaneous connections, but what can limit the number of
How can I limit the number of rows returned using activity.managedQuery()? I am pretty
Is there an easy way to limit the number of times a view can
Can I Limit Group Items in the Content Query WebPart so that the output
Situation: I am writing a program in C that maintains a number of threads.
I have a number of threads (100's) that each execute for a few seconds
Is there a limit to the number of critical sections I can initialize and
Does anyone have any good resources that show creating an unlimited number of threads
Is there any way to limit the number of processors that the ThreadPool object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.