disruptor笔记3-多生产者和多消费者操作

disruptor 多生产者和多消费者

单个消费者消费时,有几个消费者就需要启动几个线程,如果消费者过多,会使服务器存在党内存溢出甚至宕机的危险。

但是多消费者模式,并不会存在这样的危险,多消费者使用的是线程池管理消费者线程。

  • 测试类

    package com.tkn.disruptor.heigh.multi;
    
    import com.lmax.disruptor.*;
    import com.lmax.disruptor.dsl.ProducerType;
    
    import java.util.UUID;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     *  多生产者和多消费者
     */
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
            //1 创建RingBuffer
            RingBuffer<Order> ringBuffer = RingBuffer.create(ProducerType.MULTI, new EventFactory<Order>() {
    
                @Override
                public Order newInstance() {
                    return new Order();
                }
            }, 1024 * 1024, new YieldingWaitStrategy());
    
            //2 通过ringBuffer 创建一个屏障
            SequenceBarrier barrier = ringBuffer.newBarrier();
    
            //3 创建多个消费者数组:
            Consumer[] consumers=  new Consumer[10];
            for (int i = 0; i < consumers.length; i++) {
                consumers[i]=new Consumer("C"+i);
            }
            //4 构建多消费者工作池
            WorkerPool<Order> workerPool=new WorkerPool<>(ringBuffer,
                    barrier,
                    new EventExceptionHandle(),
                    consumers);
            //5 设置多个消费者的sequence序号 用于单独统计消费进度, 并且设置到ringbuffer中
            ringBuffer.addGatingSequences(workerPool.getWorkerSequences());
    
            //6 启动workerPool
            ExecutorService pool = Executors.newFixedThreadPool(5);
            workerPool.start(pool);
    
            final CountDownLatch countDownLatch=new CountDownLatch(1);
    
            for (int i = 0; i < 100; i++) {
                final Producer producer=new Producer(ringBuffer);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            countDownLatch.await();
    
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        for (int j = 0; j < 100; j++) {
                            producer.setData(UUID.randomUUID().toString());
                        }
                    }
                }).start();
    
            }
            Thread.sleep(2000);
            System.err.println("----------线程创建完毕,开始生产数据----------");
            countDownLatch.countDown();
    
            Thread.sleep(10000);
    
            System.err.println("任务总数:" + consumers[2].getCount());
            pool.shutdown();
    
        }
    
    
       static class EventExceptionHandle implements ExceptionHandler<Order>{
    
            @Override
            public void handleEventException(Throwable throwable, long l, Order order) {
    
            }
    
            @Override
            public void handleOnStartException(Throwable throwable) {
    
            }
    
            @Override
            public void handleOnShutdownException(Throwable throwable) {
    
            }
        }
    }
    
    
  • 对象类

    package com.tkn.disruptor.heigh.multi;
    
    import lombok.Data;
    
    @Data
    public class Order {
        private String id;
        private String name;
        private double price;
    }
    
    
  • 生产者

    package com.tkn.disruptor.heigh.multi;
    
    import com.lmax.disruptor.RingBuffer;
    
    // 生产者
    public class Producer {
        private  RingBuffer<Order> ringBuffer;
        public Producer(RingBuffer<Order> ringBuffer) {
            this.ringBuffer=ringBuffer;
        }
    
        public void setData(String id) {
            long sequence = ringBuffer.next();
            try {
                Order order = ringBuffer.get(sequence);
                order.setId(id);
            }finally {
                ringBuffer.publish(sequence);
            }
        }
    }
    
    
  • 消费者

    package com.tkn.disruptor.heigh.multi;
    
    import com.lmax.disruptor.WorkHandler;
    
    import java.util.Random;
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     *  消费者
     */
    public class Consumer implements WorkHandler<Order> {
        private String consumerId;
        private static AtomicInteger count=new AtomicInteger(0);
    
        private Random random=new Random();
        public Consumer(String consumerId) {
            this.consumerId = consumerId;
        }
    
        @Override
        public void onEvent(Order order) throws Exception {
    
            Thread.sleep(1 * random.nextInt(5));
            System.err.println("当前消费者: " + this.consumerId + ", 消费信息ID: " + order.getId());
            count.incrementAndGet();
        }
        public int getCount(){
            return count.get();
        }
    }
    
    
  • 并行代码分析(扩展)

    package com.tkn.disruptor;
    
    import java.util.concurrent.*;
    import java.util.concurrent.atomic.AtomicLong;
    import java.util.concurrent.locks.LockSupport;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     *  并行代码分析
     */
    public class Review {
        public static void main(String[] args) throws InterruptedException {
            ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
    
            // 读写分离
            CopyOnWriteArrayList<String> cowal = new CopyOnWriteArrayList<>();
    
            cowal.add("aaaa");
    
            // 乐观锁
    		AtomicLong count = new AtomicLong(1);
    		boolean flag = count.compareAndSet(0, 2);
    		System.err.println(flag);
    		System.err.println(count.get());
    
    
            /**
             *
             *  LockSupport :   可以先释放锁,因为是指定线程
             *    */
             Thread A = new Thread(new Runnable() {
    
            @Override
            public void run() {
            int sum = 0;
            for(int i =0; i < 10; i ++){
            sum += i;
            }
            try {
            Thread.sleep(4000);
            } catch (InterruptedException e) {
            e.printStackTrace();
            }
            LockSupport.park();	//后执行
            System.err.println("sum: " + sum);
            }
            });
    
             A.start();
    
             Thread.sleep(1000);
    
             LockSupport.unpark(A);	//先执行
    
    
            /**
             *  自定义线程池
             */
             Executors.newCachedThreadPool();
             Executors.newFixedThreadPool(10);
    
    
             ThreadPoolExecutor pool = new ThreadPoolExecutor(5,
             Runtime.getRuntime().availableProcessors() * 2,
             60,
             TimeUnit.SECONDS,
             new ArrayBlockingQueue<>(200),
             new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("order-thread");
            if(t.isDaemon()) {
            t.setDaemon(false);
            }
            if(Thread.NORM_PRIORITY != t.getPriority()) {
            t.setPriority(Thread.NORM_PRIORITY);
            }
            return t;
            }
            },
             new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.err.println("拒绝策略:" + r);
            }
            });
    
             // 重入锁
            ReentrantLock reentrantLock = new ReentrantLock(true);
    
    
    
    
        }
    }
    
    

你可能感兴趣的:(多线程,并发编程,disruptor,并行,disruptor,多生产,多消费者,多线程)