• Java生产者消费者


     1 public class ProducerConsumer {
     2     public static void main(String[] args) {
     3         SyncStack ss = new SyncStack();
     4         Producer p = new Producer(ss);
     5         Consumer c = new Consumer(ss);
     6         new Thread(p).start();
     7         new Thread(p).start();
     8         new Thread(p).start();
     9         new Thread(c).start();
    10     }
    11 }
    12 
    13 class WoTou {
    14     int id; 
    15     WoTou(int id) {
    16         this.id = id;
    17     }
    18     public String toString() {
    19         return "WoTou : " + id;
    20     }
    21 }
    22 
    23 class SyncStack {
    24     int index = 0;
    25     WoTou[] arrWT = new WoTou[6];
    26     
    27     public synchronized void push(WoTou wt) {
    28         while(index == arrWT.length) {
    29             try {
    30                 this.wait();
    31             } catch (InterruptedException e) {
    32                 e.printStackTrace();
    33             }
    34         }
    35         this.notifyAll();        
    36         arrWT[index] = wt;
    37         index ++;
    38     }
    39     
    40     public synchronized WoTou pop() {
    41         while(index == 0) {
    42             try {
    43                 this.wait();
    44             } catch (InterruptedException e) {
    45                 e.printStackTrace();
    46             }
    47         }
    48         this.notifyAll();
    49         index--;
    50         return arrWT[index];
    51     }
    52 }
    53 
    54 class Producer implements Runnable {
    55     SyncStack ss = null;
    56     Producer(SyncStack ss) {
    57         this.ss = ss;
    58     }
    59     
    60     public void run() {
    61         for(int i=0; i<20; i++) {
    62             WoTou wt = new WoTou(i);
    63             ss.push(wt);
    64 System.out.println("生产了:" + wt);
    65             try {
    66                 Thread.sleep((int)(Math.random() * 200));
    67             } catch (InterruptedException e) {
    68                 e.printStackTrace();
    69             }            
    70         }
    71     }
    72 }
    73 
    74 class Consumer implements Runnable {
    75     SyncStack ss = null;
    76     Consumer(SyncStack ss) {
    77         this.ss = ss;
    78     }
    79     
    80     public void run() {
    81         for(int i=0; i<20; i++) {
    82             WoTou wt = ss.pop();
    83 System.out.println("消费了: " + wt);
    84             try {
    85                 Thread.sleep((int)(Math.random() * 1000));
    86             } catch (InterruptedException e) {
    87                 e.printStackTrace();
    88             }            
    89         }
    90     }
    91 }
  • 相关阅读:
    针对大数据量 高并发量网站的解决方案
    session cookie 在用户登录中的使用
    彻底弄懂HTTP缓存机制及原理
    cookie 和session 的区别详解
    常见的浏览器兼容性问题
    内容分发网络(CDN)
    表现与数据分离、Web语义化
    关于bootstrap样式重写,无法覆盖的问题
    判断一个字符串中出现次数最多的字符
    [译]SQL Passion Week 10: 计划缓存
  • 原文地址:https://www.cnblogs.com/yanghailu/p/11783203.html
Copyright © 2020-2023  润新知