小言_互联网的博客

【Java】多线程-生产消费模式

327人阅读  评论(0)

案例一: 

import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        Var v = new Var();
        Customer c1 = new Customer(v);
        Customer c2 = new Customer(v);
        Product p = new Product(v);
        c1.setName("消费者1");
        c2.setName("消费者2");
        p.setName("生产者");
        p.start();
        c1.start();
        c2.start();
    }
}

//面包
class Var {
    public ArrayList<Integer> arrayList = new ArrayList<>();
}

//生产者
class Product extends Thread {
    private Var v;
    int count;
    public Product(Var v) {
        this.v = v;
    }
    @Override
    public void run() {
        while (true) {
            synchronized ("Block") {
                if (v.arrayList.size()>=10) {
                    "Block".notifyAll();
                    try {
                        "Block".wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    v.arrayList.add(count);
                    System.out.println("面包师"+Thread.currentThread().getName()+"正在生产"+v.arrayList.size()+"个面包");
                }
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

//消费者
class Customer extends Thread {
    private Var v;
    public Customer(Var v) {
        this.v = v;
    }

    @Override
    public void run() {
        while (true) {
            synchronized ("Block") {
                if (v.arrayList.size()==0) {
                    "Block".notify();
                    try {
                        "Block".wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("消费者"+Thread.currentThread().getName()+"在购买"+v.arrayList.size()+"号面包");
                    v.arrayList.remove(v.arrayList.size()-1);
                }
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

案例二:

import java.util.LinkedList;

public class MainClass {
    //共享数据
    private static final int MAX = 10;
    //仓库
    private static LinkedList<Integer> list = new LinkedList<>();
    //生产者内部类
    static class SCZ extends Thread {
        public SCZ(String name) {
            super(name);
        }
        public void sc() throws InterruptedException {
            while(true) {
                synchronized (list) {
                    //当生产数量等于最大库存时 线程进入等待状态 丢锁
                    while (list.size()==MAX) {
                        //唤醒其它线程
                        list.notifyAll();
                        System.out.println("当前已达最大库存,停止生产!");
                        //生产线程进入等待状态
                        list.wait();
                    }
                    list.add(1);
                    list.notifyAll();
                    System.out.println(Thread.currentThread().getName()+"正在生产面包!\t当前库存【"+list.size()+"】");
                    sleep(500);
                }
            }
        }

        @Override
        public void run() {
            try {
                sc();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //消费者内部类
    static class XFZ extends Thread {
        public XFZ(String name) {
            super(name);
        }
        public void xf() throws InterruptedException {
            while (true) {
                synchronized (list) {
                    while (list.size() == 0) {
                        list.notifyAll();
                        System.out.println("面包售完!");
                        list.wait();
                    }
                    list.poll();
                    list.notifyAll();
                    System.out.println(Thread.currentThread().getName()+"正在购买面包\t库存["+list.size()+"]");
                    sleep(500);
                }
            }
        }

        @Override
        public void run() {
            try {
                xf();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        //MainClass m = new MainClass();
        new SCZ("面包师").start();
        new XFZ("顾客").start();
    }
}

案例三(不使用notify()和wait()方法):

//存折(存钱) ATM(取钱)
public class Bank {
    public static void main(String[] args) {
        BK b = new BK();
        new Thread(new CQ(b),"存折").start();
        new Thread(new QK(b),"ATM").start();
    }
}
//临界资源
class BK {
    private float ye;
    public Object obj = "YaoShi";
    public void setYe(float ye) {
        this.ye = ye;
    }
    public float getYe() {
        return ye;
    }
}
//存钱
class CQ extends Thread {
    private BK b;
    public CQ(BK b) {
        this.b = b;
    }
    @Override
    public void run() {
        while (true) {
            float ck =(float)(Math.random()*99)+1;
            synchronized (b.obj) {
                b.setYe(b.getYe()+ck);
                System.out.println(Thread.currentThread().getName()+"存款:"+ck+"\t余额:"+b.getYe());
            }
            try {
                sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
//取钱
class QK extends Thread {
    private BK b;
    public QK(BK b) {
        this.b = b;
    }
    @Override
    public void run() {
        while (true) {
            float qk = (float) (Math.random()*99)+1;
            synchronized (b.obj) {

                if (b.getYe()-qk<=0) {
                    System.out.println("余额不足!无法取款!");
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    b.setYe(b.getYe()-qk);
                    System.out.println(Thread.currentThread().getName()+"取款:"+qk+"\t余额:"+b.getYe());
                }
            }
        }
    }
}

 

 

 


转载:https://blog.csdn.net/qq_33276916/article/details/101014100
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场