先进先出原则的队列结构来模拟线程间通信的过程

  今晚读了篇文章有点意思,讲了多个线程之间通信的过程 (此处附上链接:多个线程之间是如何进行通信的呢?_江户川柯南234的博客-CSDN博客_线程之间如何通讯)。文中简单demo吸引了我的注意,尤其是第二个模拟共享资源池,里面的index字段用的很巧妙,完美的实现了后进先出的队列结构。

  文末作者一段话让我不淡定了

  此处附上我简单实现的先进先出结构,欢迎大家来找茬,顺便帮忙想一下其他的解决方案

package threadCommunication;

public class CommunicationDemo3 {

    public static void main(String[] args) {
        SyncStack stack = new SyncStack();
//下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象
        Thread t1 = new Thread(new Producer(stack)); //线程实例化
        Thread t2 = new Thread(new Consumer(stack)); //线程实例化
        t2.start(); //线程启动
        t1.start(); //线程启动
    }

    static class SyncStack   // 同步堆栈类,可以一次放入多个数据
    {
        private int addIndex = 0; // 堆栈指针初始值为0
        private int popIndex = 0; // 堆栈指针初始值为0
        private int index = 0; // 堆栈指针初始值为0
        private char[] buffer = new char[5]; // 堆栈有5个字符的空间

        public synchronized void push(char c) // 入栈同步方法
        {
            if (index == buffer.length)  //  堆栈已满,不能入栈
            {
                try {
                    this.wait();        //等待出栈线程将数据出栈
                } catch (InterruptedException e) {
                }
            }
            index++;           // 指针加1,栈内空间减少
            buffer[addIndex] = c; // 数据入栈
            addIndex++;           // 数据入栈位置,下次再push在当前数据后一位填数
            if (addIndex >= buffer.length) {
                addIndex = 0;
            }
            this.notify();     // 通知其他线程把数据出栈
        }

        public synchronized char pop()    // 出栈同步方法
        {
            if (index == 0) {
                try {
                    this.wait();      //等待入栈线程把数据入栈
                } catch (InterruptedException e) {
                }
            }

            char result = buffer[popIndex];
            this.notify(); //通知其他线程入栈
            index--; //指针向下移动
            popIndex++;//数据出栈位置,下次再pop读取当前数据后一位
            if (popIndex >= buffer.length) {
                popIndex = 0;
            }
            return result; //数据出栈
        }
    }

    static class Producer implements Runnable   //生产者类
    {
        SyncStack s;          //生产者类生成的字母都保存到同步堆栈中

        public Producer(SyncStack s) {
            this.s = s;
        }

        public void run() {
            char ch;
            for (int i = 0; i < 6; i++) {
                try {
                    Thread.sleep((int) (Math.random() * 1000));

                } catch (InterruptedException e) {
                }
                ch = (char) (Math.random() * 26 + 'A'); //随机产生5个字符
                s.push(ch); //把字符入栈
                System.out.println("Push " + ch + " in Stack"); // 打印字符入栈
            }
        }
    }

    static class Consumer implements Runnable    //消费者类
    {
        SyncStack s;       //消费者类获得的字符都来自同步堆栈

        public Consumer(SyncStack s) {
            this.s = s;
        }

        public void run() {
            char ch;
            for (int i = 0; i < 6; i++) {
                try {
                    Thread.sleep((int) (Math.random() * 3000));
                } catch (InterruptedException e) {
                }
                ch = s.pop(); //从堆栈中读取字符
                System.out.println("Pop  " + ch + " from Stack"); //打印字符出栈
            }
        }
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值