java lock condition

针对线程的同步,java提供了很多种方法,比如synchronized关键字,lock方法等,都可以实现对临界区资源的访问保护,防止多个线程并发操作临界区资源而导致的问题。有的时候,光有同步还不够,还需要加入线程之间的通信机制,比如典型的消费者生产者问题。这里,基于lock+condition(条件锁)来实现典型的消费者生产者问题,例子是一个环形数组,当数组满或者空的时候就等待,当非满或者非空就通知其他线程。
代码中采用了两个条件变量,一般的情况下,我们使用一个条件变量,一个条件变量时,若发出singal,还是没有办法正确的通知到线程,也许通知的还是要等待的线程,但两个条件变量,则能够保证每次通知的线程都是能正常执行的,这种实现方式实际是优化了线程的调度效率,值得推荐和学习。

public class BasciSingal {
    public static void main(String[] args) {
        final BoundedBuffer buff = new BoundedBuffer();

        Runnable take = new Runnable() {
            @Override
            public void run() {
                try {
                    buff.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Runnable put = new Runnable() {
            @Override
            public void run() {
                try {
                    Random r = new Random();
                    buff.put(r.nextInt());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        ExecutorService execute = Executors.newCachedThreadPool();

        for (int i = 0; i < 11; i++) {
            execute.submit(take);
        }
        for (int i = 0; i < 11; i++) {
            execute.submit(put);
        }

        execute.shutdown();
    }
}

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 = 0;
    int takeptr = 0;
    int count = 0;

    public void put(Object x) throws InterruptedException {
        lock.lock();
        try {
            while (count == items.length) {
                System.out.printf("%s put wait\n", Thread.currentThread().getName());
                notFull.await();
            }

            System.out.printf("%s put %s \n", Thread.currentThread().getName(), x.toString());
            items[putptr] = x;
            if (++putptr == items.length) putptr = 0;
            ++count;
            notEmpty.signal();
        } finally {
            lock.unlock();
        }
    }

    public Object take() throws InterruptedException {
        lock.lock();
        try {
            while (count == 0) {
                System.out.printf("%s take wait\n", Thread.currentThread().getName());
                notEmpty.await();
            }

            Object x = items[takeptr];
            if (++takeptr == items.length) takeptr = 0;
            --count;
            System.out.printf("%s take %s \n", Thread.currentThread().getName(), x.toString());
            notFull.signal();
            return x;
        } finally {
            lock.unlock();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值