多线程生产者与消费者问题的总结

本文介绍了一个使用Java实现的生产者消费者模式案例。通过一个共享资源类Resource,利用synchronized关键字和wait/notifyAll方法实现线程间的同步和通信。两个生产者线程和两个消费者线程演示了该模式的基本工作流程。


  1. class Resource{  
  2.     private String name;  
  3.     private int count;  
  4.     private boolean flag;  
  5.       
  6.     public synchronized void set(String name){  
  7.         while(flag){  
  8.             try {  
  9.                 wait();  
  10.             } catch (InterruptedException e) {  
  11.                 e.printStackTrace();  
  12.             }  
  13.         }  
  14.         this.name = name+“—”+count++;  
  15.         System.out.println(Thread.currentThread().getName()+”生产者”+this.name);  
  16.         flag = true;  
  17.         notifyAll();  
  18.           
  19.     }  
  20.     public synchronized void get(){  
  21.         while(!flag){  
  22.             try{  
  23.             wait();  
  24.             }catch(InterruptedException e){  
  25.                 e.printStackTrace();  
  26.             }  
  27.         }  
  28.         System.out.println(Thread.currentThread().getName()+”–消费者–”+this.name);  
  29.         flag = false;  
  30.         notifyAll();  
  31.     }  
  32. }  
  33.   
  34. class Producer implements Runnable{  
  35.     private Resource res;  
  36.       
  37.     Producer(Resource res){  
  38.         this.res = res;  
  39.           
  40.     }  
  41.       
  42.     public void run(){  
  43.         while(true){  
  44.             res.set(”+商品+”);  
  45.         }  
  46.     }  
  47. }  
  48.   
  49. class Consumer implements Runnable{  
  50.     private Resource res;  
  51.     Consumer(Resource res){  
  52.         this.res = res;  
  53.     }  
  54.       
  55.     public void run(){  
  56.         while(true){  
  57.             res.get();  
  58.         }  
  59.     }  
  60. }  
  61.   
  62. public class ProducerConsumer {  
  63.     public static void main(String[] args) {  
  64.         Resource res = new Resource();  
  65.         Producer pro = new Producer(res);  
  66.         Consumer con = new Consumer(res);  
  67.         Thread t1 = new Thread(pro);  
  68.         Thread t2 = new Thread(con);  
  69.         Thread t3 = new Thread(pro);  
  70.         Thread t4 = new Thread(con);  
  71.           
  72.         t1.start();  
  73.         t2.start();  
  74.         t3.start();  
  75.         t4.start();  
  76.   
  77.     }  
  78.   
  79. }  
class Resource{
    private String name;
    private int count;
    private boolean flag;

    public synchronized void set(String name){
        while(flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.name = name+"---"+count++;
        System.out.println(Thread.currentThread().getName()+"生产者"+this.name);
        flag = true;
        notifyAll();

    }
    public synchronized void get(){
        while(!flag){
            try{
            wait();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"--消费者--"+this.name);
        flag = false;
        notifyAll();
    }
}

class Producer implements Runnable{
    private Resource res;

    Producer(Resource res){
        this.res = res;

    }

    public void run(){
        while(true){
            res.set("+商品+");
        }
    }
}

class Consumer implements Runnable{
    private Resource res;
    Consumer(Resource res){
        this.res = res;
    }

    public void run(){
        while(true){
            res.get();
        }
    }
}

public class ProducerConsumer {
    public static void main(String[] args) {
        Resource res = new Resource();
        Producer pro = new Producer(res);
        Consumer con = new Consumer(res);
        Thread t1 = new Thread(pro);
        Thread t2 = new Thread(con);
        Thread t3 = new Thread(pro);
        Thread t4 = new Thread(con);

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值