Take the 2-minute tour ×

I have implemented a java version of Producer && Consumer pattern in java and I have noticed that my solution is an static way !!

Precicely I have the code in the main test like where i have 1 Producer(read a chunk of a file and put it to a buffer) and many* consumer (from a buffer keeps chunks ) in the example 4:

 Producer master= new Producer();

 Consumer one= new Consumer();
 Consumer two= new Consumer();
 Consumer three= new Consumer();
 Consumer four= new Consumer();


And let's ROCCccccck..

So it's all ok but, I would Like implement a dynamic way,in which the number of consumers are established before all starts... IF I compute the java code ??:

long usableFreeMemory= Runtime.getRuntime().maxMemory()-Runtime.getRuntime().totalMemory()

I obtain the free memory of JVM ?? or the free memory of the computer ( RAM) ??

After that i obtain a free memory how I could know how many Consumer (threads) I needs to compute the parsing of a huge file?? Which Math formula a I could use to create the necessary threads??


share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

As a strategy for the number of consumers to create I would suggest you inspect Runtime.availableProcessors(). This method will tell you "... the number of processors available to the Java virtual machine." (from the JavaDoc). I submit this as the best approach because if you have more consumers than available processors you any consumer past that point will have to wait for another to enter a sleep state before it can being, and therefore you effectively have numProcessors consumers anyway.

for(int i=0;i<Runtime.numProcessors();i++){
  new Consumer().start();

Of course if your program is distributed you could start a consumer for each processor on each machine.

share|improve this answer
+1 for mentioning numProcessors. I'd like to add that if the operations have many blocked calls, you may want to use a factor, e.g. 4 x numProcessors(), so each processor handles 4 threads. That may improve overall performance –  mhaller Jan 11 '11 at 20:01
@mhaller and @ M.jessup: I think that yours argumentation are interesting..So I'll try use a costant number of thread considering the number of processor available. –  Bomberlatinos9 Jan 12 '11 at 14:04
add comment

With your memory checks you obtain the memory of the JVM, so that´s good. Loading the memory values of the Operating System will not make sense for your purpose.

Is the consumer always doing the same, or will it just execute any Task / Runnable it gets supplied? If it's always the same then you could benchmark how much memory each consumer needs and based on that calculate an amount of Threads. Otherwise you would need to check dynamically what memory each Thread uses and depending on the free space in the JVM decide if it's wise to start another Thread.

Of course it's not only about memory, but also about CPU. So you might have enough memory to spawn new Threads, but if the CPU has no more free resources then you will get performance issues.

You can also have a look at the Java Excecutor interface and it's implementations, they might come in very handy for a producer / consumer pattern.

share|improve this answer
add comment

If you wish to create a dynamic number of Producers or Consumers, why don't you just add command line arguments to your program to specify their respective numbers, and parse those? I wouldn't try to use some heuristic based off of free memory, as this may vary on various platforms.

share|improve this answer
@Amir Afghani :Because I want a dynamic way....If a passes the number of threads...it's not dynamic..or not :) ?? –  Bomberlatinos9 Jan 11 '11 at 18:54
Uh, sure it is. Whats your definition of dynamic? In your case, my definition would be something along the lines of: You can specify the number of threads at runtime, as opposed to compile time. –  Amir Afghani Jan 11 '11 at 18:56
@All:THink that you have a producer that produce 1898989 strings in a synchronized buffer, and you want established, computing the resources of memory, create an reasonable numbers of thread that permits consume all the string into the buffer...it' clear now :O?? –  Bomberlatinos9 Jan 11 '11 at 18:58
Is this homework? –  Amir Afghani Jan 11 '11 at 19:00
I would say, instead of basing it on the amount of free memory on the system, base it off of the behaivor of the Producer. Expose some data on the Producer that determines how aggressively you need to consume the amount of data produced. –  Amir Afghani Jan 11 '11 at 19:02
show 2 more comments

Once you know how many consumers you want you can put them in an array

int consumerCount = 
Consumer[] consumers = new Consumer[consumerCount];
for(int i=0;i<consumerCount;i++)
    consumer[i] = new Consumer();
for(Consumer consumer: consumers) consumer.start();

for(Consumer consumer: consumers) consumer.stop();
share|improve this answer
add comment

Your Answer


