[Java] java 堆栈类(生产者消费者 线程同步) →→→→→进入此内容的聊天室

来自 , 2019-11-29, 写在 Java, 查看 104 次.
URL http://www.code666.cn/view/5c572eca
  1. class SyncStack { // 同步堆栈类
  2.         private int index = 0; // 堆栈指针初始值为0
  3.         private char[] buffer = new char[6]; // 堆栈有6个字符的空间
  4.  
  5.         public synchronized void push(char c) { // 加上互斥锁
  6.                 while (index == buffer.length) { // 堆栈已满,不能压栈
  7.                         try {
  8.                                 this.wait(); // 等待,直到有数据出栈
  9.                         } catch (InterruptedException e) {
  10.                         }
  11.                 }
  12.  
  13.                 this.notify(); // 通知其它线程把数据出栈
  14.                 buffer[index] = c; // 数据入栈
  15.                 index++; // 指针向上移动
  16.         }
  17.  
  18.         public synchronized char pop() { // 加上互斥锁
  19.                 while (index == 0) { // 堆栈无数据,不能出栈
  20.                         try {
  21.                                 this.wait(); // 等待其它线程把数据入栈
  22.                         } catch (InterruptedException e) {
  23.                         }
  24.                 }
  25.  
  26.                 this.notify(); // 通知其它线程入栈
  27.                 index--; // 指针向下移动
  28.                 return buffer[index]; // 数据出栈
  29.         }
  30. }
  31.  
  32. class Producer implements Runnable { // 生产者类
  33.         SyncStack theStack;
  34.  
  35.         // 生产者类生成的字母都保存到同步堆栈中
  36.  
  37.         public Producer(SyncStack s) {
  38.                 theStack = s;
  39.         }
  40.  
  41.         public void run() {
  42.                 char c;
  43.                 for (int i = 0; i < 20; i++) {
  44.                         c = (char) (Math.random() * 26 + 'A');
  45.                         // 随机产生20个字符
  46.                         theStack.push(c); // 把字符入栈
  47.                         System.out.println("Produced: " + c); // 打印字符
  48.                         try {
  49.                                 Thread.sleep((int) (Math.random() * 1000));
  50.                                 /* 每产生一个字符线程就睡眠 */
  51.                         } catch (InterruptedException e) {
  52.                         }
  53.                 }
  54.         }
  55. }
  56.  
  57. class Consumer implements Runnable { // 消费者类
  58.         SyncStack theStack;
  59.  
  60.         // 消费者类获得的字符都来自同步堆栈
  61.  
  62.         public Consumer(SyncStack s) {
  63.                 theStack = s;
  64.         }
  65.  
  66.         public void run() {
  67.                 char c;
  68.                 for (int i = 0; i < 20; i++) {
  69.                         c = theStack.pop(); // 从堆栈中读取字符
  70.                         System.out.println("Consumed: " + c);
  71.                         // 打印字符
  72.                         try {
  73.                                 Thread.sleep((int) (Math.random() * 1000));
  74.                                 /* 每读取一个字符线程就睡眠 */
  75.                         } catch (InterruptedException e) {
  76.                         }
  77.                 }
  78.         }
  79. }
  80.  
  81. public class SyncTest {
  82.         public static void main(String args[]) {
  83.                 SyncStack stack = new SyncStack();
  84.                 // 下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象
  85.                 Runnable source = new Producer(stack);
  86.                 Runnable sink = new Consumer(stack);
  87.                 Thread t1 = new Thread(source); // 线程实例化
  88.                 Thread t2 = new Thread(sink); // 线程实例化
  89.                 t1.start(); // 线程启动
  90.                 t2.start(); // 线程启动
  91.         }
  92. }
  93.  

回复 "java 堆栈类(生产者消费者 线程同步)"

这儿你可以回复上面这条便签

captcha