使用Condition实现有界缓存区

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35917800/article/details/79555584
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
......

猜你喜欢

转载自blog.csdn.net/qq_35917800/article/details/79555584