使用Condition实现有界缓存区

package thread;

import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 缓冲区,支持put、get数据,缓冲区的大小为100。
 * 如果试图在空的缓冲区上执行 get 操作,则在某一个项变得可用之前,线程将一直阻塞;
 * 如果试图在满的缓冲区上执行 put 操作,则在有空间变得可用之前,线程将一直阻塞。
 */
public class BoundedBuffer
{
    final Lock lock = new ReentrantLock();
    final Condition notFull = lock.newCondition();
    final Condition notEmpty = lock.newCondition();
    final Object[] items = new Object[100];
    int putptr, getptr, count;

    public void put(Object obj) throws InterruptedException
    {
        lock.lock();
        try
        {
            while (count == items.length)
            {
                notFull.await();
                System.out.println("put线程等待......");
            }
            items[putptr] = obj;
            System.out.println("put线程存入数据 "+obj);
            if (++putptr == items.length)
            {
                putptr = 0;
            }
            ++count;
            notEmpty.signal();
            System.out.println("唤醒get线程");
        }
        finally
        {
            lock.unlock();
        }
    }
    public Object get() throws InterruptedException
    {
        lock.lock();
        try
        {
            while (count == 0)
            {
                notEmpty.await();
                System.out.println("get线程等待......");
            }
            Object obj = items[getptr];
            System.out.println("get线程取出数据 "+obj);
            if (++getptr == items.length)
            {
                getptr = 0;
            }
            --count;
            notFull.signal();
            System.out.println("唤醒put线程");
            return obj;
        }
        finally
        {
            lock.unlock();
        }
    }
    public static void main(String[] args)
    {
        final BoundedBuffer boundedBuffer = new BoundedBuffer();
        new Thread(new Runnable()
        {
            public void run()
            {
                while(true)
                {
                    try
                    {
                        boundedBuffer.put(new Random().nextInt(100));
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable()
        {
            public void run()
            {
                while(true)
                {
                    try
                    {
                        boundedBuffer.get();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

执行结果如下:

put线程存入数据 38
唤醒get线程
put线程存入数据 21
唤醒get线程
put线程存入数据 90
唤醒get线程
put线程存入数据 78
唤醒get线程
put线程存入数据 24
唤醒get线程
get线程等待......
get线程取出数据 50
唤醒put线程
get线程取出数据 97
唤醒put线程
get线程取出数据 40
唤醒put线程
get线程取出数据 38
唤醒put线程
get线程取出数据 58
唤醒put线程
get线程取出数据 40
唤醒put线程
get线程取出数据 66
唤醒put线程
get线程取出数据 84
唤醒put线程
get线程取出数据 70
唤醒put线程
get线程取出数据 14
唤醒put线程
get线程取出数据 28
唤醒put线程
get线程取出数据 19
唤醒put线程
get线程取出数据 47
唤醒put线程
get线程取出数据 83
唤醒put线程
get线程取出数据 67
唤醒put线程
get线程取出数据 12
唤醒put线程
get线程取出数据 72
唤醒put线程
get线程取出数据 27
唤醒put线程
get线程取出数据 72
唤醒put线程
get线程取出数据 19
唤醒put线程
get线程取出数据 67
唤醒put线程
get线程取出数据 16
唤醒put线程
get线程取出数据 9
唤醒put线程
get线程取出数据 98
唤醒put线程
get线程取出数据 90
唤醒put线程
get线程取出数据 85
唤醒put线程
get线程取出数据 69
唤醒put线程
get线程取出数据 3
唤醒put线程
get线程取出数据 65
唤醒put线程
get线程取出数据 3
......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值