0

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

Edmond Sze
  • 1
  • 1
  • 1
  • 1
    The `pool` will not have more than 5 active threads at any point. What's the concern? –  Nov 01 '13 at 18:43
  • What do you mean that they are not very accurate? –  Nov 01 '13 at 18:43
  • why can't you just keep submitting tasks and let the Executor handle it. – Mike R Nov 01 '13 at 18:44
  • 1
    Searching for (the task) "java blocking thread pool" found results like http://stackoverflow.com/questions/2001086/how-to-make-threadpoolexecutors-submit-method-block-if-it-is-saturated I'm not really sure what the posted code is supposed to do or show and it looks like, perhaps, the threads are *very* shortlived. – user2864740 Nov 01 '13 at 18:46
  • read this: http://www.javacodegeeks.com/2013/01/java-thread-pool-example-using-executors-and-threadpoolexecutor.html – Amir Afghani Nov 01 '13 at 18:48
  • you may need your own customize [thread pool](http://arashmd.blogspot.com/2013/06/java-threading.html#trpool). –  Nov 01 '13 at 20:01

3 Answers3

1

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?

Voidpaw
  • 910
  • 1
  • 5
  • 18
1

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?

0

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 ?

Edmond Sze
  • 1
  • 1
  • 1