Edmond Sze
Edmond Sze

Reputation: 1

Limiting the number of spawned Threads

I've run into a problem which I hope you gurus can help with.

I'm designing a multi threaded Java application which I would like to limit the number of spawned threads to 5 at any one time. The main() program should paused and wait until a thread is available from the pool until resuming it's process.

At the moment here is what I have come up with BUT it seems that the way I am detecting the number of active threads is not very accurate.

Just wondering if there is another way to do this.

ExecutorService pool = Executors.newFixedThreadPool(5);

for(int i=0; i<10000; i++){    
  System.out.println("current number of threads: "+((ThreadPoolExecutor)pool).getActiveCount());

  while(true){
      if (((ThreadPoolExecutor)pool).getActiveCount() < 5)
          break;
      Thread.sleep(TimeUnit.SECONDS.toMillis(1));
      System.out.println("waiting ..... "+((ThreadPoolExecutor)pool).getActiveCount());
  }

  Runnable sampleThread = new SampleThread(100);
  pool.submit(sampleThread );
}

**************************************************
** Output:
**************************************************
current number of threads: 0
current number of threads: 1
current number of threads: 1
current number of threads: 1
current number of threads: 1

Is there an alternative way to achieve what i'm trying to do ? I did some research and nothing quite fits the bill.

Thanks in Advance, Edmond

Upvotes: 0

Views: 184

Answers (3)

Edmond Sze
Edmond Sze

Reputation: 1

thanks guys, sample thread is responsible for sending out email notifications to my clients.

Since sending out emails (up to 100) will take a long time i'm concerned that the thread queue will be overloaded and memory resources will be exhausted.

Is it something to be concerned about ?

Upvotes: 0

bjorn.frantzen
bjorn.frantzen

Reputation: 21

It is difficult to answer without knowing what SampleThread does. If it does nothing time consuming, then the thread might have finished before the loop continues. For instance

public static class SampleThread implements Runnable {
    @Override
    public void run() {
    }

}

returns

current number of threads: 0
current number of threads: 0
current number of threads: 0
current number of threads: 0
current number of threads: 0
current number of threads: 0
current number of threads: 0

but

public static class SampleThread implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }
}

returns

current number of threads: 0
current number of threads: 1
current number of threads: 2
current number of threads: 3
current number of threads: 4
current number of threads: 5
waiting ..... 0
current number of threads: 0
current number of threads: 1
current number of threads: 2
current number of threads: 3
current number of threads: 4
current number of threads: 5
waiting ..... 0

Can you edit the post with information about what SampleThread does?

Upvotes: 1

Voidpaw
Voidpaw

Reputation: 920

You are the newFixedThreadPool from java.util.concurrent.Executors for this - it aleady limits to 5 threads. Are you sure its not already limited to 5 threads without any further moderation?

Upvotes: 1

Related Questions