zbb20181009 多线程编程:阻塞、并发队列

最近,一直在跟设计的任务调度模块周旋,目前终于完成了第一阶段的调试。今天,我想借助博客园平台把最近在设计过程中,使用队列和集合的一些基础知识给大家总结一下,方便大家以后直接copy。本文都是一些没有技术含量的东西,只是做个总结,牛哥还请绕路。

老习惯,还是先跟各位纸上谈会儿兵,首先说说队列,他主要分为并发队列和阻塞队列,在多线程业务场景中使用最为普遍,我就主要结合我所做过的业务谈谈我对它们的看法,关于它们的API和官方解释就不提了。

并发队列

并发队列:最常见的业务场景就是多个线程共享同一个队列中的所有资源,就拿我们公司的业务场景来说,当用户通过多个渠道下单后,然后就会有多个不同的客户端通道同时去获取订单并处理订单,为了加快订单处理速度我们使用并发队列来充当任务源头,为了加快处理订单速度,结合多线程并发来满足需求。

并发队列没什么可说的,就是一个简单的多线程编程操作,小Demo送给各位:

/**
 *  并发队列ConcurrentLinkedQueue的使用
 */

public class ConcurrentQueue {

    public static void main(String[] args){
        ToyotaYQ yq = new ToyotaYQ();
        new Thread(yq,"ToyotaYQ_001").start();
        new Thread(yq,"ToyotaYQ_002").start();
        new Thread(yq,"ToyotaYQ_003").start();
    }

}

/**
 * 任务来源
 */
class MQ{
    private static Queue<String> queue = null;    //并发队列(线程安全)

    /**
     * 初始化并发队列
     */
    public static Queue<String> initQueue(){
        if(queue == null){
            queue = new ConcurrentLinkedQueue<String>();
        }
        String tasklist = "JF1GH78F18G036149,JF1SH95F6AG110830,JF1SJ94D7DG010387,JF1SH92F9CG269249,JF1SH92F5BG215090,JF1SH92F5BG222556,JF1SH92F4CG279994,JF1BR96D7CG114298,JF1BR96D0BG078632,JF1SH95F9AG094011,JF1SH98FXAG186997,JF1BM92D8BG022510,JF1BM92DXAG013855,JF1BM94D8EG036618";
        String[] split = tasklist.split(",");
        List<String> task = Arrays.asList(split);    //数组转集合
        queue.addAll(task);        //按照集合中元素的顺序将集合中全部元素放进队列

        return queue;
    }
}

/**
 * 制单客户端
 */
class ToyotaYQ implements Runnable{

    private static final Object lock = new Object();
    private static Queue<String> queueYQ = MQ.initQueue();

    @Override
    public void run() {
        while(true){
            synchronized (lock){    //尽量减小锁的粒度和范围
                String thisVIN = queueYQ.poll();
                if(thisVIN == null){
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "成功制单:" + thisVIN + "。剩余:" + queueYQ.size() + "个任务");
            }
        }
    }
}

阻塞队列

阻塞队列:最常见的业务场景就是生产者不断生产任务放进阻塞队列中,消费者不断从阻塞队列中获取任务;当阻塞队列中填满数据时,所有生产者端的线程自动阻塞,当阻塞队列中数据为空时,所有消费端的线程自动阻塞。这些操作BlockingQueue都已经包办了,不用我们程序员去操心了。

阻塞队列我们常用的有:LinkedBlockingQueueArrayBlockingQueue,它们在各方面还是很大的区别的;ArrayBlockingQueue在put,take操作使用了同一个锁,两者操作不能同时进行,而LinkedBlockingQueue使用了不同的锁,put操作和take操作可同时进行,以此来提高整个队列的并发性能。

作为开发者,使用阻塞队列需要注意的一点是:如果构造一个LinkedBlockingQueue对象,而没有指定其容量大小,LinkedBlockingQueue会默认一个类似无限大小的容量(Integer.MAX_VALUE),这样的话,如果生产者的速度一旦大于消费者的速度,也许还没有等到队列满阻塞产生,系统内存就有可能已被消耗殆尽了。

阻塞队列的一些常用方法

下面是我根据这几天设计的任务调度功能模拟的一个小Demo,只不过项目中使用了MQ服务,这里用阻塞队列完成可以代替:

public class BlockQueueDemo {

    public static void main(String[] args){
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(2); //定长为2的阻塞队列
        //ExecutorService:真正的线程池接口
        ExecutorService service = Executors.newCachedThreadPool();  //缓存线程池
        //创建3个生产者:
        ProducerDemo p1 = new ProducerDemo("车鉴定web端",queue);
        ProducerDemo p2 = new ProducerDemo("车鉴定APP端",queue);
        ProducerDemo p3 = new ProducerDemo("车鉴定接口端",queue);
        ProducerDemo p4 = new ProducerDemo("车鉴定M栈",queue);
        //创建三个消费者:
        ConsumerDemo c1 = new ConsumerDemo("ToyotaYQ_001",queue);
        ConsumerDemo c2 = new ConsumerDemo("ToyotaYQ_002",queue);
        ConsumerDemo c3 = new ConsumerDemo("ToyotaYQ_003",queue);

        //启动线程
        service.execute(p1);
        service.execute(p2);
        service.execute(p3);
        service.execute(p4);
        service.execute(c1);
        service.execute(c2);
        service.execute(c3);

    }
}

/**
 * 生产者
 */
class ProducerDemo implements Runnable {
    private String producerName;
    private BlockingQueue queue;//阻塞队列
    private Random r = new Random();

    //构造函数,传入生产者名称和操作的阻塞队列
    public ProducerDemo(String producerName,BlockingQueue queue) {
        this.producerName = producerName;
        this.queue = queue;
    }

    @Override
    public void run() {
        while(true){
            try {
                int task = r.nextInt(100);  //产生随机数
                System.out.println(producerName + "开始生产任务:" + task);
                queue.put(task);  //生产者向队列中放入一个随机数
                Thread.sleep(5000);  //减缓生产者生产的速度,如果队列为空,消费者就会阻塞不会进行消费直到有数据被生产出来
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class ConsumerDemo implements Runnable{
    private String consumerName;
    private BlockingQueue queue;//阻塞队列

    //构造函数,传入消费者名称和操作的阻塞队列
    public ConsumerDemo(String consumerName,BlockingQueue queue) {
        this.consumerName = consumerName;
        this.queue = queue;
    }

    @Override
    public void run() {
        while(true){
            try {
                System.out.println(consumerName + "开始消费任务---" + queue.take());//消费者从阻塞队列中消费一个随机数
                //Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

开发中各位最常用最熟悉的不过也是集合了,但是前几天在设计中突然想自己控制任务的分配和修改,这就需要用到灵活操作集合中的内容了,其它也没什么,但是删除集合中的元素这一点我们还是必须要很熟练的,虽然是需要借助迭代器来删除的,但是还是记录一下吧,方便以后copy。

删除List集合中的某元素:

public class ListDemo {

    public static void main(String[] args){
        ArrayList<String> arrList = new ArrayList<String>();
        String[] arr = {"一丰","广丰","宝马","奥迪","保时捷","沃尔沃","悍马","路虎","凯迪拉克"};
        arrList.addAll(Arrays.asList(arr));     //将数组转成集合

        //删除前:
        for (String thisItem:arrList){
            System.out.println("---"+thisItem);
        }
        System.out.println("#########################");

        //使用迭代器删除集合中的元素
        Iterator it = arrList.iterator();
        while(it.hasNext()){    //it.hasNext()判断是否还有下一个元素
            if("悍马".equals(it.next())){     //it.next()代表下一个元素
                it.remove();        //【记得:remove()方法一定要调用迭代器的,不能调用List集合的】
            }
        }

        //删除后:
        for (String thisItem:arrList){
            System.out.println("---"+thisItem);
        }

    }
}
原文地址:https://www.cnblogs.com/super-admin/p/9763186.html