zoukankan      html  css  js  c++  java
  • 经典javaThead 生产者 消费者

    在网找的一个代码,经过修改后,我认为更有代表性。因为这个代码到处都是,所以我不知道谁是原创,也就不好标明出处了。希望原作者见谅!

     1 class MainThread {
     2     public static void main(String[] args) {
     3         Queue queue = new Queue();
     4         Producer producer = new Producer(queue);
     5         Consumer consumer = new Consumer(queue);
     6         new Thread(producer).start();
     7         new Thread(consumer).start();
     8     }
     9 }
    10 
    11 /* 注意:wait notify notifyAll只能在同步方法或内步块中调用 */
    12 class Queue {
    13     int product = 0;
    14 
    15     public synchronized void setProduct() {
    16         if (this.product > 10) {// 如果队列已满,则调用wait等待消费者取走产品
    17             try {
    18                 wait();
    19             } catch (Exception e) {
    20                 e.printStackTrace();
    21             }
    22         }
    23         this.product++/* 开始放置产品到队列中 */
    24         System.out.println("Product, product left:" + product);
    25         notify();// 生产产品后通知消费者取走产品
    26         try {
    27             Thread.sleep(20);
    28         } catch (InterruptedException e) {
    29             // TODO Auto-generated catch block
    30             e.printStackTrace();
    31         }
    32     }
    33 
    34     public synchronized void getProduct() {
    35         if (this.product <= 0) {// 如果队列是空的,则调用wait等待生产者生产产品
    36             try {
    37                 wait();
    38             } catch (Exception e) {
    39                 e.printStackTrace();
    40             }
    41         }
    42         /* 开始从队列取走产品 */
    43         this.product--;
    44         System.out.println("Consume, product left:" + product);
    45         notify();// 取走产品后通知生产者继续生产产品
    46         try {
    47             Thread.sleep(20);
    48         } catch (InterruptedException e) {
    49             // TODO Auto-generated catch block
    50             e.printStackTrace();
    51         }
    52     }
    53 }
    54 
    55 class Producer implements Runnable {
    56     Queue queue;
    57 
    58     Producer(Queue queue) {
    59         this.queue = queue;
    60     }
    61 
    62     public void run() {// 生产线程
    63         for (int i = 1; i <= 10; i++) {
    64             queue.setProduct();
    65         }
    66     }
    67 }
    68 
    69 class Consumer implements Runnable {
    70     Queue queue;
    71 
    72     Consumer(Queue queue) {
    73         this.queue = queue;
    74     }
    75 
    76     public void run() {// 消费线程
    77         for (int i = 1; i <= 10; i++) {
    78             queue.getProduct();
    79         }

    80     }

    81 } 


     

  • 相关阅读:
    POJ 3352&&3177 (割边 && 边双连通分量)
    .net程序员的盲点(二):两个“属性”引起的歧异
    .net程序员的盲点(一):参数修饰符ref,out ,params的区别
    .net程序员的盲点(一):参数修饰符ref,out ,params的区别
    args argc argv 的意思以及英文缩写
    .net程序员的盲点(一):参数修饰符ref,out ,params的区别
    .net程序员的盲点(一):参数修饰符ref,out ,params的区别
    .net程序员的盲点(一):参数修饰符ref,out ,params的区别
    args argc argv 的意思以及英文缩写
    args argc argv 的意思以及英文缩写
  • 原文地址:https://www.cnblogs.com/zhonghan/p/1442373.html
Copyright © 2011-2022 走看看