Created
May 21, 2013 09:02
-
-
Save dragansah/5618464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProducerConsumer | |
{ | |
public static final int BUFFER_SIZE = 1000; | |
public interface Producer | |
{ | |
/** Appends a random integer at the end of the buffer */ | |
void produce(Queue<Integer> buffer); | |
} | |
public interface Consumer | |
{ | |
/** Removes the last element from the buffer */ | |
void consume(Queue<Integer> buffer); | |
} | |
public static class ProducerImpl implements Producer | |
{ | |
Random r = new Random(System.nanoTime()); | |
@Override | |
public void produce(Queue<Integer> buffer) | |
{ | |
buffer.add(r.nextInt()); | |
System.out.println("ProducerImpl1: Adding another item to the end of the queue"); | |
} | |
} | |
public static class ConsumerImpl implements Consumer | |
{ | |
@Override | |
public void consume(Queue<Integer> buffer) | |
{ | |
buffer.remove(); | |
System.out.println("ConsumerImpl1: Consuming the last item"); | |
} | |
} | |
public static class ProducerImpl2 implements Producer | |
{ | |
Random r = new Random(System.nanoTime()); | |
@Override | |
public void produce(Queue<Integer> buffer) | |
{ | |
buffer.add(r.nextInt()); | |
System.out.println("ProducerImpl2: Adding another item to the end of the queue"); | |
} | |
} | |
public static class ConsumerImpl2 implements Consumer | |
{ | |
@Override | |
public void consume(Queue<Integer> buffer) | |
{ | |
buffer.remove(); | |
System.out.println("ConsumerImpl2: Consuming the last item"); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
final int PRODUCERS = 10; | |
final int CONSUMERS = 20; | |
final Queue<Integer> buffer = new LinkedList<Integer>(); | |
for (int i = 0; i < PRODUCERS; i++) | |
new Thread(new Runnable() | |
{ | |
public void run() | |
{ | |
Producer p = new ProducerImpl(); | |
while (true) | |
{ | |
p.produce(buffer); | |
if (buffer.size() > BUFFER_SIZE) | |
throw new IllegalStateException("The Buffer must not be greater than " + BUFFER_SIZE); | |
} | |
} | |
}).start(); | |
for (int i = 0; i < CONSUMERS; i++) | |
new Thread(new Runnable() | |
{ | |
public void run() | |
{ | |
Consumer c = new ConsumerImpl(); | |
while (true) | |
c.consume(buffer); | |
} | |
}).start(); | |
final Queue<Integer> buffer2 = new LinkedList<Integer>(); | |
for (int i = 0; i < PRODUCERS; i++) | |
new Thread(new Runnable() | |
{ | |
public void run() | |
{ | |
Producer p = new ProducerImpl2(); | |
while (true) | |
{ | |
p.produce(buffer2); | |
if (buffer2.size() > BUFFER_SIZE) | |
throw new IllegalStateException("The Buffer must not be greater than " + BUFFER_SIZE); | |
} | |
} | |
}).start(); | |
for (int i = 0; i < CONSUMERS; i++) | |
new Thread(new Runnable() | |
{ | |
public void run() | |
{ | |
Consumer c = new ConsumerImpl2(); | |
while (true) | |
c.consume(buffer2); | |
} | |
}).start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment