disruptor第三弹(手撕第一章)

前言:基于自己对disruptor的理解,实现一个简单的disruptor。上一篇文章是写了一个demo,现在完善他的功能

package com.wm;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2019-09-23 14:59
 * @Description:
 */
public class DisMain<T> {

    /**
     * 存放的环形队列
     */
    private Object[] objects;

    /**
     * 尾部指针
     */
    private int tail;

    /**
     * 头部指针
     */
    private int head;

    /**
     * 环形大小
     */
    public int ringBuffSie;

    public DisMain(int ringBuffSie,EventFactory eventFactory) {
        this.ringBuffSie = ringBuffSie;
        objects = new Object[ringBuffSie];
        for (int i = 0; i <ringBuffSie ; i++) {
            objects[i] =eventFactory.newInstance();
        }
    }

    /**
     * 判断环形是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        if (tail == head) {
            return true;
        }
        return false;
    }

    public boolean put(T obj){
        if(isFull()){
            return false;
        }else{
            objects[tail%ringBuffSie]=obj;
            tail++;
            return true;
        }
    }
    public T get(){
        if(isEmpty()){
            return null;
        }else{
           Object obj =  objects[head];
            head++;
            return (T)obj;
        }
    }

    /**
     * 判断环形是否填满
     *
     * @return
     */
    public boolean isFull() {
        //现在的位置
        int nowPro = tail - ringBuffSie;
        if ((nowPro + 1) == head) {
            return true;
        }
        return false;
    }

    /**
     * 获取所有未消费的对象列表
     *
     * @return
     */
    public List<T> getObjects() {
        List<T> list = new ArrayList<>();
        if (isEmpty()) {
            //环形为空
            return null;
        }
        //生产跑了多少圈
        int proRoundSize = tail % ringBuffSie;
        //消费跑了多少圈
        int consumerRoundSize = head % ringBuffSie;

//        消费者和生产者跑了同样的圈数
        if (proRoundSize == consumerRoundSize) {
            for (int i = head + 1; i < tail; i++) {
                list.add((T)objects[i]);
            }
        } else if (proRoundSize > consumerRoundSize) {
            for (int i = head + 1; i < consumerRoundSize * ringBuffSie; i++) {
                list.add((T)objects[i - consumerRoundSize * ringBuffSie]);
            }
            int cn = tail - proRoundSize * ringBuffSie;
            for (int i = 0; i < cn; i++) {
                list.add((T)objects[i]);
            }
        }
        return list;
    }

}

这个和上一章的区别是:

  1.  增加了一个构造函数
  2. 存在环形队列里的对象进行初始化
  3. 写了一个工厂去初始化环形队列里的每个对象
public interface EventFactory<T>
{
    T newInstance();
}

 

public class StudentFactory implements EventFactory<Student> {
    @Override
    public Student newInstance() {
        return new Student();
    }
}
public class Student {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

现在写一个Main函数泡起来,看看雏形

public class App 
{
    public static void main( String[] args )
    {

        DisMain<Student> disMain = new DisMain(16,new StudentFactory());
        Student stu = new Student();
        stu.setId(1);
        stu.setName("test");
        disMain.put(stu);

        Student stu1= disMain.get();
        System.out.println(stu1.getName());
        System.out.println(stu1==stu);
    }
}

由于这里是把对象new出来进行赋值,new一个对象也是一种开销,disruptor是把对象的属性进行覆盖。那我们下一章再进行一些功能添加和优化。

 

我为什么不把功能都写好贴出来,我想给大家做一种真正的手撕,一点点从源码中,把代码撕出来。做成功能,我也是对我自己的一种挑战。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值