Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

import java.util.

Random;

class buff_zone {

   /*** 用栈实现缓冲区,定义缓冲区的容量大小,以及指向缓冲区顶部的指针 **/


   static final int size = 15;
   static int[] buf = new int[size];
   static int product_num = 0;

   static void produce() {


       int product = (new Random()).nextInt(100);
       System.out.println("生产了一个产品:" + product);
       buf[product_num ++] = product;
       System.out.print("当前缓冲区有" + product_num + "个产品:");
       for (int i = 0;i < product_num;i ++) {
           System.out.print(Integer.toString(buf[i]) + ' ');
      }
       System.out.println();
  }

   static void consume() {


       int product = buf[-- product_num];
       System.out.println("消费了一个产品:" + product);
       System.out.print("当前缓冲区有" + product_num + "个产品:");
       for (int i = 0;i < product_num;i ++) {
           System.out.print(Integer.toString(buf[i]) + ' ');
      }
       System.out.println();
  }

   static boolean isFull() {


       return product_num == size;
  }

   static boolean isEmpty() {


       return product_num == 0;
  }

}
public class sync_lock implements Runnable{

   static final int num = 30;

   static sync_lock instance = new sync_lock();

   final Object mutex = new Object();


   final Object full = new Object();
   final Object empty = new Object();

   @Override
   public void run() {
       int thread_id =
Integer.parseInt(Thread.currentThread().getName().substring(7));
       if (thread_id >= num) {
           synchronized (empty) {
               while (buff_zone.isEmpty()) { System.out.print(""); }
               synchronized (mutex) {
                   System.out.println("线程" + thread_id + ":这是一个消费者线程");
                   System.out.println("线程" + thread_id + ":进入缓冲区");
                   try{ Thread.sleep(300); } catch (InterruptedException e) {
e.printStackTrace(); }
                   buff_zone.consume();
                   System.out.println("线程" + thread_id + ":退出缓冲区");
                   System.out.println();
              }
          }
      } else {
           synchronized (full) {
               while (buff_zone.isFull()) { System.out.print(""); }
               synchronized (mutex) {
                   System.out.println("线程" + thread_id + ":这是一个生产者线程");
                   System.out.println("线程" + thread_id + ":进入缓冲区");
                   try{ Thread.sleep(300); } catch (InterruptedException e) {
e.printStackTrace(); }
                   buff_zone.produce();
                   System.out.println("线程" + thread_id + ":退出缓冲区");
                   System.out.println();
              }
          }
      }
  }

   public static void main(String[] args) {

       Thread[] producers = new Thread[num];


       Thread[] consumers = new Thread[num];

       for (int i = 0;i < num;i ++) {


           producers[i] = new Thread(instance);
           consumers[i] = new Thread(instance);
      }

       for (int i = 0;i < num;i ++) {


           producers[i].start();
           consumers[i].start();
      }

  }

You might also like