生产者/消费者模式实现

1.一生产与一消费:操作值

public class P {
    private String lock;
    public P(String lock){
        super();
        this.lock = lock;
    }
    public void setValue(){
        try {
            synchronized (lock){
            if(!ValueObject.value.equals("")){
                    lock.wait();
                }
                String value = System.currentTimeMillis() + "_" + 
                System.nanoTime();
                System.out.println("set的值是" + value);
                ValueObject.value = value;
                lock.notify();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    class C {
        private String lock;

        public C(String lock) {
            super();
            this.lock = lock;
        }

        public void getValue() {
            try {
                synchronized (lock) {
                    if (ValueObject.value.equals("")) {
                        lock.wait();
                    }
                    System.out.println("get的值是" + ValueObject.value);
                    ValueObject.value = "";
                    lock.notify();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    class ThreadP extends Thread {
        private P p;
        public ThreadP(P p){
            super();
            this.p = p;
        }
        
        @Override
        public void run(){
            while (true){
                p.setValue();
            }
        }
    }
    class ThreadC extends Thread {
        private C c;
        public ThreadC(C c){
            super();
            this.c = c;
        }

        @Override
        public void run(){
            while (true){
                c.setValue();
            }
        }
    }
}
public class Run{
    public static void main(String[] args){
        String lock = new String("");
        P p = new P(lock);
        C c = new C(lock);
        ThreadP pThread = new ThreadP(p);
        ThreadC cThread = new ThreadC(r);
        pThread.start();
        cThread.start();
    }
}

上述代码是一生产一消费进行数据的交互在控制台中打印的日志get和set是交替运行的。
但是如果是多生产者和多消费者,那在运行中就极有可能出现假死状态(即所有线程均处于WAITING状态)

2.多生产与多消费:操作值-假死
假死的现象其实就是线程进入WAITING状态,则程序就不再执行任何业务功能了,整个项目呈停止状态。这在使用生产者与消费者模式时经常遇到。

public class P{
  private String lock;
  public P(String lock){
    super();
    this.lock = lock;
  }
  public void setValue(){
    synchronized (lock){
      while(!ValueObject.Value.equals("")){
        System.out.println(“生产者”+        
        Thread.currentThread().getName()+"WAITING了");
        lock.wait();
      }
      System.out.println("生产者"+
      Thread.currentThread().getName()+"RUNNABLE了");
      String value = 
      System.currentTimeMillis()+"_"+System.nanoTime();
      ValueObject.value=value;
      lock.notify();
    }
  }
}
public class C{
  private String lock;
  public C(String lock){
    super();
    this.lock = lock;
  }
  public void setValue(){
    synchronized (lock){
      while(ValueObject.Value.equals("")){
        System.out.println(“消费者”+        
        Thread.currentThread().getName()+"WAITING了");
        lock.wait();
      }
      System.out.println("消费者"+
      Thread.currentThread().getName()+"RUNNABLE了");
      ValueObject.value="";
      lock.notify();
    }
  }
}
class ThreadP extends Thread {
        private P p;
        public ThreadP(P p){
            super();
            this.p = p;
        }
        
        @Override
        public void run(){
            while (true){
                p.setValue();
            }
        }
    }
   class ThreadC extends Thread {
        private C c;
        public ThreadC(C c){
            super();
            this.c = c;
        }

        @Override
        public void run(){
            while (true){
                c.setValue();
            }
        }
    }
}

public class Run{
  public static void main(String[] args){
    String lock = new String("");
    P p = new P(lock);
    C r = new C(lock);
    ThreadP[] pThread = new ThreadP[2];
    ThreadC[] rThread = new ThreadC[2];
    for(int i=0;i<2;i++){
      pThread[i] = new ThreadP(p);
      pThread[i].setName("生产者"+(i+1));
      rThread[i] = new ThreadC(r);
      rThread[i].setName("消费者" + (i+1));
      pThread[i].start();
      rThread[i].start();
    }
    Thread.sleep(5000);
    Thread[] threadArray = new       
    Thread[Thread.currentThread().getThreadGroup().activeCount()];
    Thread.currentThread().getThreadGroup()
    .enumerate(threadArray);
    for(int i = 0;i

在代码中确实已经通过wait/notify进行通信了,但不保证notify唤醒的是异类,也许是同类,比如“生产者”唤醒“生产者”或“消费者”唤醒“消费者”这样的情况。如果按这样的情况运行的比率积少成多,就会导致所有的线程都不能继续运行下去,大家都在等待,都呈WAITING状态,程序最后也就呈假死状态。

如何解决唤醒同类的问题?
将notify()改成notifyAll(),也就是不光唤醒同类,将异类也一同唤醒就解决了。

你可能感兴趣的:(生产者/消费者模式实现)