Java—多线程实现生产者消费者模型

采用线程实现“生产者-消费者”编程的基础模型

源代码

消费者代码:
public class Consumer implements Runnable {BlockingQueue<Integer> blockingQueue;int n;CountDownLatch countDownLatch;public Consumer(BlockingQueue<Integer> blockingQueue, int n, CountDownLatch countDownLatch) {this.blockingQueue = blockingQueue;this.n = n;this.countDownLatch=countDownLatch;}@Overridepublic void run() {for(int i=0;i<n;i++){try {int cur=blockingQueue.take();isPrime(cur);/*   System.out.println("消费"+blockingQueue.size());*/} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("消费者完成");countDownLatch.countDown();}int isPrime(int n){	//返回1表示判断为质数,0为非质数,在此没有进行输入异常检测double n_sqrt;if(n==2 || n==3) return 1;if(n%6!=1 && n%6!=5) return 0;n_sqrt=Math.floor(Math.sqrt((float)n));for(int i=5;i<=n_sqrt;i+=6){if(n%(i)==0 | n%(i+2)==0) return 0;}return 1;}}生产者代码:
public class Producer implements Runnable{BlockingQueue<Integer> blockingQueue;int n;CountDownLatch countDownLatch;public Producer(BlockingQueue<Integer> blockingQueue, int n,CountDownLatch countDownLatch) {this.blockingQueue = blockingQueue;this.n = n;this.countDownLatch=countDownLatch;}@Overridepublic void run() {Random ra =new Random();for(int i=0;i<n;i++){try {blockingQueue.put(ra.nextInt(2000000000)+1);/*    System.out.println("生产"+blockingQueue.size());*/} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("生产者完成");countDownLatch.countDown();}
}主类:
public class Model {public  static void excute(int producerNum,int consumerNum,int num,CountDownLatch countDownLatch){BlockingQueue<Integer> blockingQueue=new LinkedBlockingQueue<>(num);for(int i=0;i<producerNum;i++){new Thread(new Producer(blockingQueue,num/producerNum,countDownLatch)).start();}for(int i=0;i<consumerNum;i++){new Thread(new Consumer(blockingQueue,num/consumerNum,countDownLatch)).start();}}public static void main(String[] args) {CountDownLatch countDownLatch=new CountDownLatch(6);long s=System.currentTimeMillis();excute(2,4,1000000,countDownLatch);try {countDownLatch.await();} catch (InterruptedException e) {e.printStackTrace();}System.out.println((double) (System.currentTimeMillis()-s)/1000);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392046.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

动态链接库.so和静态链接库.a的区别

静态链接库&#xff1a; •扩展名&#xff1a;.a  •编译行为&#xff1a;在编译的时候&#xff0c;将函数库直接整合到执行程序中&#xff08;所以利用静态库编译生成的文档会更大&#xff09; •独立执行的状态&#xff1a;编译成功的可执行文件可以独立运行&#xff0c;不…

华为鸿蒙系统封闭,谷歌正式“除名”华为!“亲儿子”荣耀表示:暂不考虑,鸿蒙OS处境尴尬...

我们都知道&#xff0c;目前智能手机最常用操作系统就是IOS和安卓&#xff0c;占据手机系统超过99%的市场份额。由于IOS系统的封闭性&#xff0c;国内手机厂商基本上都是使用谷歌的开源安卓系统。当然华为也不例外&#xff0c;一直使用的都是安卓系统。可以说&#xff0c;安卓系…

使用vue-cli脚手架搭建简单项目框架

1.首先已经安装了node,最好版本6以上。 2.安装淘宝镜像 大家都知道国内直接使用 npm 的官方镜像是非常慢的&#xff0c;这里推荐使用淘宝 NPM 镜像。这样就可以直接使用cnpm了。 npm install -g cnpm --registryhttps://registry.npm.taobao.org如果过程出差&#xff0c;是否安…

sap中泰国有预扣税设置吗_泰国餐厅密度细分:带有K-means聚类的python

sap中泰国有预扣税设置吗Hi! I am Tung, and this is my first stories for my weekend project. What inspired this project is that I have studied to become data scientist for almost two years now mostly from Youtube, coding sites and of course, Medium ,but my l…

自动化yaml文件_从YAML到TypeScript:开发人员对云自动化的看法

自动化yaml文件The rise of managed cloud services, cloud-native, and serverless applications brings both new possibilities and challenges. More and more practices from software development processes like version control, code review, continuous integration,…

SQL SERVER-Extendevent系统视图

--获得扩展事件的事件select name,description from sys.dm_xe_objects where object_typeevent order by name--获得各事件的字段 select c.name,c.description from sys.dm_xe_object_columns c inner join sys.dm_xe_objects o on o.namec.object_name where o.name…

Java—简单的注册页面

根据所提供的界面&#xff0c;编写 register.html 文件 源代码 empty.jsp <% page contentType"text/html;charsetUTF-8" language"java" %> <html> <head><title>error</title> </head> <body> <H1><…

【深度学习系列】用PaddlePaddle和Tensorflow实现经典CNN网络AlexNet

上周我们用PaddlePaddle和Tensorflow实现了图像分类&#xff0c;分别用自己手写的一个简单的CNN网络simple_cnn和LeNet-5的CNN网络识别cifar-10数据集。在上周的实验表现中&#xff0c;经过200次迭代后的LeNet-5的准确率为60%左右&#xff0c;这个结果差强人意&#xff0c;毕竟…

图片获取像素坐标html,HTML5画布Canvas图片抽取、像素信息获取、命中检测

今天主要介绍canvas中比较强大的功能比如将画布内容抽取为图片获取、修改画布的像素信息以及画布的命中检测首先我仍然需要创建画布图片抽取首先要明确的一点是toDataURL()是canvas对象自身的方法而不是环境对象的这个方法会将canvas的内容抽取为一张图片(base64编码)我们来看一…

CentOS6 下Samba服务器的安装与配置

原地址&#xff1a;http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html 一、简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件&#xff0c;而SMB是Server Message Block的缩写&#xff0c;即为服务器消息块 &#xff0c;SMB主要是作为Microsoft的网…

傅里叶变换 直观_A / B测试的直观模拟

傅里叶变换 直观Many of us have heard, read, or even performed an A/B Test before, which means we have conducted a statistical test at some point. Most of the time, we have worked with data from first or third-party sources and performed these tests with ea…

tableau for循环_Tableau for Data Science and Data Visualization-速成课程

tableau for循环Tableau is software that can help you see and understand your data. It is used for data science and data visualization. Tableau allows you to connect to almost any database, drag and drop to create visualizations, and share with a click.Tabl…

请求接口时使用时间戳

&tnew Date().getTime()转载于:https://www.cnblogs.com/Glant/p/11271960.html

Java—servlet简单使用

【步骤 1】创建一个名为 input.html 的 HTML 页面&#xff0c;其中包括一个表单&#xff0c;表单中包含两 个文本域&#xff0c;分别供用户输入学号和姓名&#xff0c;该页面也包含提交和重置按钮。 【步骤 2】定义一个名为 com.demo.Student 类&#xff0c;其中包括学号 sno 和…

phpstrom+phpstudy+postman

1.打开phpstudy xdebug 扩展 2.修改php.ini [XDebug]xdebug.profiler_output_dir"D:\phpStudy\tmp\xdebug"xdebug.trace_output_dir"D:\phpStudy\tmp\xdebug"zend_extension"D:\phpStudy\php\php-5.5.38\ext\php_xdebug.dll";是否允许Xdebug跟踪…

SIP协议

SIP协议 SIP协议主要包括 SIP头 SIP内容 和附加内容三个部分 项目格式备注示例SIP头一行&#xff0c;以\r\n结尾REGISTER sip:172.30.2.35 SIP/2.0\r\nSIP内容很多行&#xff0c;每行为Key&#xff0c;Value的形式CSeq: 1 REGISTER\r\n附加内容很多行1 SIP头 项目格式含义示例I…

android emmc 命令,使用CoreELEC的ceemmc工具将系统写入emmc

最近在折腾电视盒子&#xff0c;CoreELEC是专门为晶晨CPU开发系统&#xff0c;个人觉的非常不错&#xff0c;相关资料可以百度。这里介绍将卡载系统刷入emmc内置存储的方法。因为找不到相关的教程&#xff0c;只在官网上找到了ceemmc这个工具的使用说明&#xff0c;所以搬过来。…

ios 自定义字体_如何仅用几行代码在iOS应用中创建一致的自定义字体

ios 自定义字体by Yuichi Fujiki藤木雄一 In this article, youll learn how to create a unified custom look throughout your app with these simple tricks.在本文中&#xff0c;您将学习如何使用这些简单的技巧在整个应用程序中创建统一的自定义外观。 我们想做什么 (Wh…

truncate 、delete与drop区别

相同点&#xff1a; 1.truncate和不带where子句的delete、以及drop都会删除表内的数据。 2.drop、truncate都是DDL语句(数据定义语言),执行后会自动提交。 不同点&#xff1a; 1. truncate 和 delete 只删除数据不删除表的结构(定义)drop 语句将删除表的结构被依赖的约束(const…

Java—jsp编程

1. 编写 login.jsp&#xff0c;登录时只输入一个昵称。但要检查昵称是否已经被其他用户使用。 源代码 Login.jsp <% page contentType"text/html;charsetUTF-8" language"java" %><%request.setCharacterEncoding("UTF-8"); //设置编…