I have a simple producer/consumer program with three classes, can
anyone tell me why the commented out code (that synchronizes on the
buffer object) in Producer.java and Consumer.java is not needed?
--- PC.java (main class) --------------------------------------------
import java.util.ArrayList;
class PC
{
public static ArrayList buffer;
public static int bufferMaxSize;
public static void main(String[] args)
{
// Get the variables to use from the command-line...
int max, numProducers, producerProb, numConsumers, consumerProb;
try
{
max = Integer.parseInt(args[0]);
numProducers = Integer.parseInt(args[1]);
producerProb = Integer.parseInt(args[2]);
numConsumers = Integer.parseInt(args[3]);
consumerProb = Integer.parseInt(args[4]);
}
catch(Exception e)
{
System.out.println("java PC [max buffer size] [num
producers] [producer %] [num consumers] [consumer %]");
return;
}
// Initialise the buffer...
buffer = new ArrayList();
bufferMaxSize = max;
// Create producers...
Producer[] producers = new Producer[numProducers];
for(int i = 0; i < producers.length; i++)
producers[i] = new Producer(("Producer " + (i+1)),
producerProb);
// Create consumers...
Consumer[] consumers = new Consumer[numConsumers];
for(int i = 0; i < consumers.length; i++)
consumers[i] = new Consumer(("Consumer " + (i+1)),
consumerProb);
// Start the producers running...
for(int i = 0; i < producers.length; i++)
producers[i].start();
// Start the consumers running...
for(int i = 0; i < consumers.length; i++)
consumers[i].start();
}
}
--- Producer.java ---------------------------------------------------
import java.util.Random;
class Producer extends Thread
{
private int probability;
Producer(String name, int probability)
{
super(name);
this.probability = probability;
}
public void run()
{
while(true)
{
try { this.sleep(1000); } catch(InterruptedException e) { }
Random random = new Random(System.currentTimeMillis());
int check = random.nextInt(100) + 1;
if(check < probability)
{
// *** THE LINE BELOW IS NOT NEEDED! ***
//synchronized(PC.buffer)
//{
// Add an item to the buffer...
if(PC.buffer.size() == PC.bufferMaxSize)
System.out.println(getName() + ": buffer full");
else
{
String item = "Item created by " + getName();
PC.buffer.add(item);
System.out.println(getName() + ": added \"" + item
+ "\"");
}
//}
}
}
}
}
--- Consumer.java ---------------------------------------------------
import java.util.Random;
class Consumer extends Thread
{
private int probability;
Consumer(String name, int probability)
{
super(name);
this.probability = probability;
}
public void run()
{
while(true)
{
try { this.sleep(1000); } catch(InterruptedException e) { }
Random random = new Random(System.currentTimeMillis());
int check = random.nextInt(100) + 1;
if(check < probability)
{
// *** THE LINE BELOW IS NOT NEEDED! ***
//synchronized(PC.buffer)
//{
// Remove an item from the buffer...
if(PC.buffer.size() == 0)
System.out.println(getName() + ": buffer empty");
else
{
String item = (String)PC.buffer.remove(0);
System.out.println(getName() + ": removed \"" +
item + "\"");
}
//}
}
}
}
}