0% found this document useful (0 votes)
15 views2 pages

Sync Demo2

The document defines a BuffZone class that implements a buffer with a fixed capacity to store integer products. It contains methods to produce and consume products by adding/removing from the buffer array. A SyncLock class is defined that implements Runnable to simulate multiple producer and consumer threads. The run method uses locks and waits on empty/full conditions to synchronize access to the buffer. Main starts an equal number of producer and consumer threads to test the synchronized buffer.

Uploaded by

winter smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Sync Demo2

The document defines a BuffZone class that implements a buffer with a fixed capacity to store integer products. It contains methods to produce and consume products by adding/removing from the buffer array. A SyncLock class is defined that implements Runnable to simulate multiple producer and consumer threads. The run method uses locks and waits on empty/full conditions to synchronize access to the buffer. Main starts an equal number of producer and consumer threads to test the synchronized buffer.

Uploaded by

winter smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Random;

class buff_zone {

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


   static final int size = 10;
   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 = 20;

   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