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();
master.start();
one.start();
two.start();
three.start();
four.start();
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()
+Runtime.getRuntime().freeMemory();
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??
thanks..
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.
Of course if your program is distributed you could start a consumer for each processor on each machine.