代码很简单,就不介绍了。需要注意的是,先有生产才有消费,所以与消费者关联的semaphore计数开始为0,而与生产者关联的semaphore计数开始为1。
实现:
[java] view plain copy
- import java.util.concurrent.Semaphore;
-
-
-
-
-
-
-
-
-
- public class ConsumerProducer {
-
- static class SharedData{
- int data = 0;
- Semaphore semproducer = new Semaphore(1);
-
- Semaphore semconsumer = new Semaphore(0);
-
- public int consume() throws InterruptedException{
- try {
- semconsumer.acquire();
- System.out.format(" Consumed: %d%n", data);
- Thread.sleep(10);
- return data;
- } catch (InterruptedException e) {
- throw e;
- } finally{
- semproducer.release();
- }
- }
-
- public void produce(int data) throws InterruptedException{
- try {
- semproducer.acquire();
- this.data = data;
- System.out.format("Produced: %d%n", data);
- Thread.sleep(10);
- } catch (InterruptedException e) {
- throw e;
- } finally{
- semconsumer.release();
- }
- }
- }
-
- static class ConsumerThread implements Runnable {
-
- private SharedData shared;
-
- public ConsumerThread(SharedData shared){
- this.shared=shared;
- }
-
- public void run(){
- for(int i=0;i<30;i++){
- try {
- int data = shared.consume();
-
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- static class ProducerThread implements Runnable{
- private SharedData shared;
- public ProducerThread(SharedData shared){
- this.shared=shared;
- }
- public void run(){
- for(int i=0;i<30;i++){
- try {
- shared.produce(i);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- public static void main(String[] args) throws Exception {
- SharedData shared = new SharedData();
-
- Thread producer = new Thread(new ProducerThread(shared),"producer");
- Thread consumer = new Thread(new ConsumerThread(shared),"consumer");
-
- consumer.start();
- producer.start();
- }
-
- }
测试输出:
Produced: 0
Consumed: 0
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
Produced: 4
Consumed: 4
Produced: 5
Consumed: 5
Produced: 6
Consumed: 6
Produced: 7
Consumed: 7
Produced: 8
Consumed: 8
Produced: 9
Consumed: 9
Produced: 10
Consumed: 10
Produced: 11
Consumed: 11
Produced: 12
Consumed: 12
Produced: 13
Consumed: 13
Produced: 14
Consumed: 14
Produced: 15
Consumed: 15
Produced: 16
Consumed: 16
Produced: 17
Consumed: 17
Produced: 18
Consumed: 18
Produced: 19
Consumed: 19
Produced: 20
Consumed: 20
Produced: 21
Consumed: 21
Produced: 22
Consumed: 22