国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java_Thread生產者與消費者 Demo

2019-11-14 23:42:44
字體:
來源:轉載
供稿:網友
java_Thread生產者與消費者 Demo
  1 package com.bjsxt.Thread.Demo;  2 public class PRoducerConsumer {  3     /**  4      * 生產者與消費者  5      * @param args  6      */  7     public static void main(String[] args) {// 模擬線程  8         SyncStack ss = new SyncStack();  9         Producer p = new Producer(ss); 10         Consumer c = new Consumer(ss); 11         new Thread(p).start();// 開啟線程 12         new Thread(c).start();// 開啟線程 13     } 14 } 15  16 /** 17  * Woto類 18  */ 19 class WoTo { 20     int id; 21     WoTo(int id) { 22         this.id = id; 23     } 24     public String toString() { 25         return "WoTo : " + id; 26     } 27 } 28  29 /** 30  * 框類(用來裝饅頭) 31  * @author wenfei 32  */ 33 class SyncStack { 34     int index = 0; 35     WoTo[] arrwt = new WoTo[6]; 36  37     public synchronized void push(WoTo wt) { 38  39         while (index == arrwt.length) { 40  41             try { 42                 this.wait();// 暫定當前對象 43  44             } catch (InterruptedException e) { 45                 e.printStackTrace(); 46             } 47         } 48         this.notify();// 叫醒當前線程 49         arrwt[index] = wt; 50         index++; 51     } 52  53     public synchronized WoTo pop() { 54         while (index == 0) { 55  56             try { 57                 this.wait(); 58             } catch (InterruptedException e) { 59                 e.printStackTrace(); 60             } 61         } 62         this.notify(); 63         index--; 64         return arrwt[index]; 65     } 66 } 67  68 /** 69  * 生產者 70  *  71  * @author wenfei 72  */ 73 class Producer implements Runnable { 74     SyncStack ss = null; 75  76     Producer(SyncStack ss) { 77         this.ss = ss; 78     } 79  80     @Override 81     public void run() { 82         // 生產wt 83         for (int i = 0; i <= 100; i++) { 84             WoTo wt = new WoTo(i); 85             ss.push(wt);// 往籃子里裝窩頭 86             System.out.println("生產了--->" + wt); 87             try { 88                 // Thread.sleep(1000);//每生產一個睡眠一秒 89                 Thread.sleep((int) Math.random() * 1000); 90             } catch (InterruptedException e) { 91                 // TODO Auto-generated catch block 92                 e.printStackTrace(); 93             } 94         } 95     } 96  97 } 98  99 /**100  * 消費者101  * 102  * @author wenfei103  */104 class Consumer implements Runnable {105     SyncStack ss = null;106 107     Consumer(SyncStack ss) {108         this.ss = ss;109     }110 111     @Override112     public void run() {113         for (int i = 0; i <= 100; i++) {114             WoTo wt = ss.pop();115             System.out.println("消費了--->" + wt);116             try {117                 // Thread.sleep(1000);//每消費一個睡眠一秒118                 Thread.sleep((int) Math.random() * 1000);//119             } catch (InterruptedException e) {120                 // TODO Auto-generated catch block121                 e.printStackTrace();122             }123             // System.out.println(wt);124         }125     }126 127 }


上一篇:spring學習筆記1

下一篇:Mockito Hello World

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江口县| 元阳县| 满洲里市| 巴楚县| 江城| 彰化县| 舞钢市| 上虞市| 尉氏县| 宁阳县| 上杭县| 阳新县| 滦南县| 托克逊县| 方正县| 海淀区| 南部县| 梧州市| 峨山| 乌兰浩特市| 邛崃市| 新巴尔虎左旗| 交口县| 德兴市| 泾川县| 绥棱县| 莆田市| 福清市| 五原县| 拉萨市| 广汉市| 会东县| 库伦旗| 陕西省| 黑河市| 井研县| 汉川市| 蓝田县| 那坡县| 高唐县| 宣汉县|