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.pswp.cn/news/392046.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392046.shtml
英文地址,請注明出處:http://en.pswp.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"); //設置編…