3.1.11生产者消费者(n:n)

来源:互联网 发布:java的职业规划 编辑:程序博客网 时间:2024/06/05 20:45

package demo;import java.util.ArrayList;import java.util.List;/** * Created by sunyifeng on 17/10/16. */public class MyStack {    private List list = new ArrayList();    synchronized public void push() {        try {            while (list.size() == 1) {                this.wait();            }            list.add("sunyf=" + Math.random());            this.notifyAll();            System.out.println("push=" + list.size());        } catch (InterruptedException e) {            e.printStackTrace();        }    }    synchronized public String pop() {        String returnValue = "";        try {            while (list.size() == 0) {                System.out.println("pop操作中的:" + Thread.currentThread().getName() + "线程是wait状态");                this.wait();            }            returnValue = "" + list.get(0);            list.remove(0);            this.notifyAll();            System.out.println("pop=" + list.size());        } catch (InterruptedException e) {            e.printStackTrace();        }        return returnValue;    }}
package demo;/** * Created by sunyifeng on 17/10/16. */public class P {    private MyStack myStack;    public P(MyStack myStack) {        super();        this.myStack = myStack;    }    public void pushService(){        myStack.push();    }}
package demo;/** * Created by sunyifeng on 17/10/16. */public class C {    private MyStack myStack;    public C(MyStack myStack) {        super();        this.myStack = myStack;    }    public void popService(){        System.out.println("pop=" + myStack.pop());    }}
package demo;/** * Created by sunyifeng on 17/10/16. */public class ThreadP extends Thread {    private P p;    public ThreadP(P p) {        super();        this.p = p;    }    @Override    public void run(){        while (true) {            p.pushService();        }    }}
package demo;/** * Created by sunyifeng on 17/10/16. */public class ThreadC extends Thread {    private C c;    public ThreadC(C c) {        super();        this.c = c;    }    @Override    public void run(){        while (true) {            c.popService();        }    }}
package demo;/** * Created by sunyifeng on 17/10/16. */public class Run {    public static void main(String[] args) {        MyStack myStack = new MyStack();        P p1 = new P(myStack);        P p2 = new P(myStack);        P p3 = new P(myStack);        P p4 = new P(myStack);        P p5 = new P(myStack);        P p6 = new P(myStack);        ThreadP threadP1 = new ThreadP(p1);        ThreadP threadP2 = new ThreadP(p2);        ThreadP threadP3 = new ThreadP(p3);        ThreadP threadP4 = new ThreadP(p4);        ThreadP threadP5 = new ThreadP(p5);        ThreadP threadP6 = new ThreadP(p5);        threadP1.start();        threadP2.start();        threadP3.start();        threadP4.start();        threadP5.start();        threadP6.start();        //        C c1 = new C(myStack);        C c2 = new C(myStack);        C c3 = new C(myStack);        C c4 = new C(myStack);        C c5 = new C(myStack);        C c6 = new C(myStack);        C c7 = new C(myStack);        C c8 = new C(myStack);        ThreadC threadC1 = new ThreadC(c1);        ThreadC threadC2 = new ThreadC(c2);        ThreadC threadC3 = new ThreadC(c3);        ThreadC threadC4 = new ThreadC(c4);        ThreadC threadC5 = new ThreadC(c5);        ThreadC threadC6 = new ThreadC(c5);        ThreadC threadC7 = new ThreadC(c5);        ThreadC threadC8 = new ThreadC(c5);        threadC1.start();        threadC2.start();        threadC3.start();        threadC4.start();        threadC5.start();        threadC6.start();        threadC7.start();        threadC8.start();    }}
运行结果:

push=1
pop=0
pop=sunyf=0.00672334326936852
pop操作中的:Thread-6线程是wait状态
pop操作中的:Thread-11线程是wait状态
pop操作中的:Thread-8线程是wait状态
pop操作中的:Thread-13线程是wait状态
pop操作中的:Thread-12线程是wait状态
pop操作中的:Thread-10线程是wait状态
pop操作中的:Thread-9线程是wait状态
pop操作中的:Thread-7线程是wait状态
push=1
pop=0
pop=sunyf=0.9209681161168464
pop操作中的:Thread-7线程是wait状态
pop操作中的:Thread-9线程是wait状态
pop操作中的:Thread-10线程是wait状态
pop操作中的:Thread-12线程是wait状态
pop操作中的:Thread-13线程是wait状态
pop操作中的:Thread-8线程是wait状态
pop操作中的:Thread-11线程是wait状态
pop操作中的:Thread-6线程是wait状态
push=1

结果说明:

1、list对象size没有超过1。

2、notifyAll不会造成假死。

原创粉丝点击