A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 暗影流光 中级黑马   /  2014-7-9 19:33  /  1078 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.concurrent.locks.*;

  2. public class Test23 {
  3.         public static void main(String[] args) {
  4.                 Resource r = new Resource();
  5.                 String x = "黄焖鸡";

  6.                 PutX px = new PutX(r, x);
  7.                 TakeX tx = new TakeX(r, x);
  8.                 Thread t0 = new Thread(px);
  9.                 Thread t1 = new Thread(px);
  10.                 Thread t2 = new Thread(tx);
  11.                 Thread t3 = new Thread(tx);

  12.                 t0.start();
  13.                 t1.start();
  14.                 t2.start();
  15.                 t3.start();
  16.         }
  17. }

  18. class Resource {

  19.         Lock lock = new ReentrantLock();
  20.         Condition full = lock.newCondition();
  21.         Condition empty = lock.newCondition();

  22.         private String[] item = new String[400];
  23.         private int taker = 0;
  24.         private int puter = 0;
  25.         private int count = 0;

  26.         public void put(String x) {
  27.                 while (true) {
  28.                         lock.lock();
  29.                         //用while不用if是因为当多个线程在这里等待后,被唤醒,需要重新判断这个条件
  30.                         while (count == item.length) {
  31.                                 try {
  32.                                         full.await();
  33.                                 } catch (InterruptedException e) {
  34.                                 }
  35.                         }
  36.                         item[puter] = x;
  37.                         System.out.println(Thread.currentThread().getName() + "..." + count
  38.                                         + "...put.." + x);
  39.                         if (puter == item.length - 1) {
  40.                                 puter = -1;
  41.                                 empty.signalAll();
  42.                         }
  43.                         ++count;
  44.                         ++puter;
  45.                         lock.unlock();
  46.                 }
  47.         }

  48.         public void take(String x) {
  49.                 while (true) {
  50.                         lock.lock();
  51.                         while (count == 0) {
  52.                                 try {
  53.                                         empty.await();
  54.                                 } catch (InterruptedException e) {
  55.                                 }
  56.                         }
  57.                         x = item[taker];
  58.                         System.out.println(Thread.currentThread().getName() + "..."
  59.                                         + (count - 1) + "..take......" + x);
  60.                         if (taker == item.length - 1) {
  61.                                 taker = -1;
  62.                                 full.signalAll();
  63.                         }
  64.                         --count;
  65.                         ++taker;
  66.                         lock.unlock();
  67.                 }
  68.         }
  69. }

  70. class PutX implements Runnable {
  71.         Resource r;
  72.         String x;

  73.         PutX(Resource r, String x) {
  74.                 this.r = r;
  75.                 this.x = x;
  76.         }

  77.         public void run() {
  78.                 r.put(x);
  79.         }
  80. }

  81. class TakeX implements Runnable {
  82.         Resource r;
  83.         String x;

  84.         TakeX(Resource r, String x) {
  85.                 this.r = r;
  86.                 this.x = x;
  87.         }

  88.         public void run() {
  89.                 r.take(x);
  90.         }
  91. }
复制代码


2 个回复

倒序浏览
没搞懂你要表达什么
回复 使用道具 举报
不能理解你的问题在哪
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马