concurrent包中condition的使用

首先回忆上一篇关于读写锁的知识。

首先先看一个例子:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by lizhiqiang on 2016/12/22.
 */
public class testCondition {
    public static void main(String[] args){
        final BoundedBuffer buffer = new BoundedBuffer();
        ExecutorService service = Executors.newFixedThreadPool(10);
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(1);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("1"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(2);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("2"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(3);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("3"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(4);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("4"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(5);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("5"+buffer.take());
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                buffer.put(6);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("6"+buffer.take());
            }
        });
        service.shutdown();
    }
    public static class BoundedBuffer{
        final Object[] items = new Object[100];
        int count;
        public BoundedBuffer(){
            count=0;
        }
        public void put(Object x){
            items[++count] = x;
        }
        public Object take(){
            return items[count--];
        }
    }
}

这个例子实际上是从开启6个线程,分别对一个数组进行写读操作,执行结果:
24
16
43
53
65
31
发现写入的数据被其他线程改了,说明每个线程的操作并不是原子操作,需要加锁。
改后的代码为:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by lizhiqiang on 2016/12/22.
 */
public class testCondition {
    public static void main(String[] args){
        final BoundedBuffer buffer = new BoundedBuffer();
        ExecutorService service = Executors.newFixedThreadPool(10);
        final ReadWriteLock lock = new ReentrantReadWriteLock();
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(1);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("1"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(2);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("2"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(3);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("3"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(4);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("4"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(5);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("5"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                lock.writeLock().lock();
                buffer.put(6);
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("6"+buffer.take());
                lock.writeLock().unlock();
            }
        });
        service.shutdown();
    }
    public static class BoundedBuffer{
        final Object[] items = new Object[100];
        int count;
        public BoundedBuffer(){
            count=0;
        }
        public void put(Object x){
            items[++count] = x;
        }
        public Object take(){
            return items[count--];
        }
    }
}
执行结果为:
11
22
33
44
55
66
当然它们的输出顺序可能是不一样的,但是写入和读是对应的了。
以上部分是回忆上一篇关于读写锁的知识。
================================================================================================
下面开始Condition的内容
真实应用场景:传入队列任务,监听器从队列中获取任务。
但是如果我们take的时候,发现count为0怎么办?如果我们put的时候,如果count达到上限怎么办?
过去的做法经常过一会再次尝试,这种方式的实时性不是特别高,如何做到精准的实时性呢?
首先是想加一个通知的操作,即:如果take的时候发现count为0,则将线程进入等待。另一个线程put的时候,则唤醒之前的线程,让线程继续执行。
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.*;

/**
 * Created by lizhiqiang on 2016/12/22.
 */
public class testCondition {
    public static void main(String[] args){
        final BoundedBuffer buffer = new BoundedBuffer();
        ExecutorService service = Executors.newFixedThreadPool(2);
        service.execute(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    buffer.put(1);
                }
            }
        });
        service.execute(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    System.out.println(new Date() + ":" + buffer.take());
                }
            }
        });
        service.shutdown();
    }
    public static class BoundedBuffer{
        final Lock lock = new ReentrantLock();

        Condition notEmpty = lock.newCondition();
        Condition notFull = lock.newCondition();
        final Object[] items = new Object[3];
        int count;
        public BoundedBuffer(){
            count=0;
        }
        public void put(Object x){
            lock.lock();
            if(count==2){
                try {
                    notFull.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            ++count;
            items[count] = x;
            notEmpty.signal();
            lock.unlock();
        }
        public Object take(){
            lock.lock();
            Object result;
            if(count==0) {
                try {
                    notEmpty.await();

                } catch (InterruptedException e) {
                }
            }
            result = items[count--];
            notFull.signal();
            lock.unlock();
            return result;
        }
    }
}

测试结果为:

Thu Dec 22 16:41:10 CST 2016:1
Thu Dec 22 16:41:11 CST 2016:1
Thu Dec 22 16:41:12 CST 2016:1
Thu Dec 22 16:41:13 CST 2016:1
Thu Dec 22 16:41:14 CST 2016:1
Thu Dec 22 16:41:15 CST 2016:1
Thu Dec 22 16:41:16 CST 2016:1
Thu Dec 22 16:41:17 CST 2016:1
Thu Dec 22 16:41:18 CST 2016:1
Thu Dec 22 16:41:19 CST 2016:1
Thu Dec 22 16:41:20 CST 2016:1
Thu Dec 22 16:41:21 CST 2016:1
Thu Dec 22 16:41:22 CST 2016:1
Thu Dec 22 16:41:23 CST 2016:1
Thu Dec 22 16:41:24 CST 2016:1


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值