java 环形阻塞队列简单实现

本文介绍了一种简单的阻塞队列实现方式,通过使用synchronized关键字和wait/notify机制来确保线程间的同步和等待通知。该实现适用于基本的学习和测试场景。
摘要由CSDN通过智能技术生成

java 阻塞队列简单实现

/**
 * 简单的阻塞队列
 * @author Administrator
 *
 */
public class MySelfBlockingQueue {

    int capacity = 10;
    int[] container;
    int headIndex = 0;//队列头部位置
    int tailIndex = 0;//队列尾部位置
    int size = 0;

    MySelfBlockingQueue(){
        container = new int[capacity];
    }

    MySelfBlockingQueue(int capacity){
        this.capacity = capacity;
        container = new int[capacity];
    }

    public synchronized int getSize(){
        return size;
    }

    public synchronized void putData(int data){
        while(getSize() == capacity){
            try {
                System.out.println("阻塞中-----------------");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        container[tailIndex] = data;
        size++;
        tailIndex = ++tailIndex == capacity ? 0 : tailIndex;
        System.out.println("添加成功");
        this.notifyAll();
    }

    public synchronized int getData(){
        while(getSize() == 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int rdata = container[headIndex];
        container[headIndex] = 0;
        size--;

        if(getSize() == 0){
            headIndex = 0;
            tailIndex = 0;
        }else{
            headIndex = ++headIndex == capacity ? 0 : headIndex;
        }
        this.notifyAll();
        return rdata;
    }

    public void clearQueue(){
        if(getSize() > 0){
            container = new int[capacity];
            headIndex = 0;
            tailIndex = 0;
            size = 0;
        }
    }

    public String getContent() {
        String str = "[";
        for(int i = 0 ; i < container.length ; i++){
            if(i < container.length - 1){
                str += container[i] + ",";
            }else{
                str += container[i];
            }
        }
        str += "]";
        return str;
    }

    public static void main(String[] args) {
        final MySelfBlockingQueue queue = new MySelfBlockingQueue(6);
        final CountDownLatch cdl = new CountDownLatch(2);
        new Thread(new Runnable() {
            public void run() {
                for(int i = 1 ; i < 8 ; i++){
                    queue.putData(i);
                    System.out.println(queue.getContent());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                cdl.countDown();
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(queue.getData());
                System.out.println(queue.getContent());
                cdl.countDown();
            }
        }).start();
        try {
            cdl.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("------------------------");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值