多线程案例:生产者和消费者

多线程案例:生产者和消费者

 1.生产和消费的产品抽象类:


public abstract class Product {

    public String name;
    public abstract String toString();
}

2.一个具体的产品类:

public class AProduct extends Product {

    public AProduct(String name) {

        this.name = name;
        // TODO Auto-generated constructor stub
    }
    public String toString() {

        // TODO Auto-generated method stub
        return this.name;
    }
}

3.容器类(仓库):

import java.util.ArrayList;

/*
 * 存放生产者和消费者的产品队列
 * */

public class Container {

    private ArrayList arrList = new ArrayList();
    private int LENGTH = 10;
    public boolean isFull() {

        return arrList.size()==LENGTH;
    }
    public boolean isEmpty() {

        return arrList.isEmpty();
    }

    /* 如果此处不加synchronized锁,那么也可以再调用push的地方加锁
    * 既然此处加了锁,那么再别的地方可以不加锁
    */

    public synchronized void push(Object o) {

        arrList.add(o);
    }
    // 如果此处不加synchronized锁,那么也可以再调用push的地方加锁
    public synchronized Object pop() {

        Object lastOne = arrList.get(arrList.size()- 1);
        arrList.remove(arrList.size()- 1);
        return lastOne;
    }
}

4.休息一会,生产者和消费者都要休息,因此作为抽象基类:

public abstract class Sleep {

    public void haveASleep() throws InterruptedException {

        Thread.sleep((long)(Math.random()* 3000));
    }
}

/*
 * 消费者线程
 * */

public class Consumer extends Sleep implements Runnable {

    private Container contain =null;
    public Consumer(Container contain) {

        this.contain = contain;
    }
    public void run() {

        // TODO Auto-generated method stub
        while(true) {

            synchronized(contain) {

                while(contain.isEmpty()) {

                    try{

                        contain.wait();
                    }catch(InterruptedException e) {

                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            consume();//消费
            try {

                haveASleep();
            }catch(InterruptedException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            synchronized(contain) {

                contain.notify();
            }
        }
    }
    private void consume() {

        Product a = (AProduct)contain.pop();
        System.out.println("消费了一个产品"+ a.toString());
    }
}

/*
 * 生产者线程
 * */

public class Producator extends Sleep implements Runnable {

    private Container contain = null;
    public Producator(Container contain) {

        super();
        this.contain = contain;
    }
    public void run() {

        // TODO Auto-generated method stub
        while(true) {

            synchronized(contain) {

                while(contain.isFull()) {

                    try{

                        contain.wait();// 阻塞当前线程,当前线程进入等待队列。这个时候只有等待别的线程来唤醒自己了。
                    }catch(InterruptedException e) {

                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            producer();// 生产一个产品
            try {

                haveASleep();
            }catch(InterruptedException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            synchronized(contain) {

                contain.notify();// 唤醒等待队列中正在等待的第一个线程,让其执行。
            }
        }
    }
    public void producer() {

        Product aProduct = new AProduct("pp:"+String.valueOf((int)(10*Math.random())));
        System.out.println("生产了一个产品:"+ aProduct.toString());
        contain.push(aProduct);
    }
}

5. 写一个测试:

public class TestMain {

    /**
     * @param args
     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Container contain = new Container();
        Producator p = new Producator(contain);
        Consumer c = new Consumer(contain);
        Thread pt =new Thread(p);
        Thread ct =new Thread(c);
        pt.start();
        ct.start();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/100530.html原文链接:https://javaforall.net

(0)
上一篇 2021年5月6日 下午2:00
下一篇 2021年5月6日 下午3:00


相关推荐

  • OpenCV之bitwise_and、bitwise_not等图像基本运算及掩膜

    OpenCV之bitwise_and、bitwise_not等图像基本运算及掩膜1 图像基本运算图像的基本运算有很多种 比如两幅图像可以相加 相减 相乘 相除 位运算 平方根 对数 绝对值等 图像也可以放大 缩小 旋转 还可以截取其中的一部分作为 ROI 感兴趣区域 进行操作 各个颜色通道还可以分别提取及对各个颜色通道进行各种运算操作 总之 对于图像可以进行的基本运算非常的多 只是挑了些常用的操作详解 voidadd InputArraysr InputA

    2026年3月19日
    2
  • 大型网站架构设计及技术总结

    大型网站架构设计及技术总结一个小型的网站 比如个人网站 可以使用最简单的 html 静态页面就实现了 配合一些图片达到美化效果 所有的页面均存放在一个目录下 这样的网站对系统架构 性能的要求都很简单 随着互联网业务的不断丰富 网站相关的技术经过这些年的发展 已经细分到很细的方方面面 尤其对于大型网站来说 所采用的技术更是涉及面非常广 从硬件到软件 编程语言 数据库 WebServer 防火墙等各个领域都有了很高的要求

    2026年3月17日
    2
  • Qos原理与配置

    Qos原理与配置

    2021年4月14日
    189
  • windows/linux环境python3出现pip is configured with locations that require TLS/SSL, however the..不可用的解决方法[通俗易懂]

    windows/linux环境python3出现pip is configured with locations that require TLS/SSL, however the..不可用的解决方法[通俗易懂]linux环境下首先明确问题出现原因,是因为openssl版本过低或者不存在so: 查看openssl安装包,发现缺少openssl-devel包 [root@localhost~]#rpm-aq|grepopenssl openssl-0.9.8e-20.el5 openssl-0.9.8e-20.el5 [root@localhost~]# yu…

    2025年7月13日
    6
  • C++11特性_object.equals

    C++11特性_object.equalsdecltype与auto关键字一样,用于进行编译时类型推导。decltype实际上有点像auto的反函数,auto可以让你声明一个变量,而decltype则可以从一个变量或表达式中得到类型,例如:intx=3;decltype(x)y=x;有人会问,decltype的实用之处在哪里呢,假如有一个加工产品的函数模板:templatevoidproc

    2025年10月11日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号