} } }

    应用Lock来实现临盆者和花费者题目

    添加时间:2013-5-18 点击量:


      1 package com.thread;
    
    2
    3 import java.util.LinkedList;
    4 import java.util.concurrent.locks.Condition;
    5 import java.util.concurrent.locks.Lock;
    6 import java.util.concurrent.locks.ReentrantLock;
    7
    8
    9 /
    10 应用Lock来实现临盆者和花费者题目
    11
    12 @author 刘玲
    13
    14 /
    15 public class ProducerConsumer {
    16 public static void main(String[] args) {
    17 Basket b = new Basket();
    18 Product p = new Product(b);
    19 Consumer c = new Consumer(b);
    20 Consumer c1 = new Consumer(b);
    21 new Thread(p).start();
    22 new Thread(c).start();
    23 new Thread(c1).start();
    24 }
    25 }
    26 //馒头
    27 class ManTou{
    28 int id;
    29 public ManTou(int id) {
    30 this.id = id;
    31 }
    32 @Override
    33 public String toString() {
    34 return ManTou+id;
    35 }
    36 }
    37
    38 //装馒头的篮子
    39 class Basket{
    40 int max = 6;
    41 LinkedList<ManTou> manTous = new LinkedList<ManTou>();
    42 Lock lock = new ReentrantLock(); //锁对象
    43 Condition full = lock.newCondition(); //用来监控篮子是否满的Condition实例
    44 Condition empty = lock.newCondition(); //用来监控篮子是否空的Condition实例
    45 //往篮子里面放馒头
    46 public void push(ManTou m){
    47 lock.lock();
    48 try {
    49 while(max == manTous.size()){
    50 System.out.println(篮子是满的,待会儿再临盆...);
    51 full.await();
    52 }
    53 manTous.add(m);
    54 empty.signal();
    55 } catch (InterruptedException e) {
    56 e.printStackTrace();
    57 }finally{
    58 lock.unlock();
    59 }
    60 }
    61 //往篮子里面取馒头
    62 public ManTou pop(){
    63 ManTou m = null;
    64 lock.lock();
    65 try {
    66 while(manTous.size() == 0){
    67 System.out.println(篮子是空的,待会儿再吃...);
    68 empty.await();
    69 }
    70 m = manTous.removeFirst();
    71 full.signal();
    72 } catch (InterruptedException e) {
    73 e.printStackTrace();
    74 }finally{
    75 lock.unlock();
    76 return m;
    77 }
    78 }
    79 }
    80 //临盆者
    81 class Product implements Runnable{
    82 Basket basket;
    83 public Product(Basket basket) {
    84 this.basket = basket;
    85 }
    86 public void run() {
    87 forint i = 0; i < 40; i++) {
    88 ManTou m = new ManTou(i);
    89 basket.push(m);
    90 System.out.println(临盆了+m);
    91 try {
    92 Thread.sleep((int)(Math.random()2000));
    93 } catch (InterruptedException e) {
    94 e.printStackTrace();
    95 }
    96
    97 }
    98 }
    99 }
    100
    101 //花费者
    102 class Consumer implements Runnable{
    103 Basket basket;
    104 public Consumer(Basket basket) {
    105 this.basket = basket;
    106 }
    107 public void run() {
    108 forint i = 0; i < 20; i++) {
    109 try {
    110 Thread.sleep((int)(Math.random()2000));
    111 } catch (InterruptedException e) {
    112 e.printStackTrace();
    113 }
    114 ManTou m = basket.pop();
    115 System.out.println(花费了+m);
    116 }
    117 }
    118 }


    我俩之间有着强烈的吸引力。短短几个小时后,我俩已经明白:我们的心是一个整体的两半,我俩的心灵是孪生兄妹,是知己。她让我感到更有活力,更完美,更幸福。即使她不在我身边,我依然还是感到幸福,因为她总是以这样或者那样的方式出现在我心头。——恩里克·巴里奥斯《爱的文明》
    分享到: