Java常见源码解析
环境搭建 克隆jdk项目:git clone https://github.com/openjdk/jdk.git / https://gitee.com/zhijiantianya/jdk 编译 数组ArrayListArrayList ,基于 [] 数组实现的,支持自动扩容的动态数组。 实现了 4 个接口,分别是: java.util.List 接口,提供数组的添加、删除、修改、迭代遍历等操作。 java.util.RandomAccess 接口,表示 ArrayList 支持快速的随机访问。 java.io.Serializable 接口,表示 ArrayList 支持序列化的功能。 java.lang.Cloneable 接口,表示 ArrayList 支持克隆。 继承了 java.util.AbstractList 抽象类,而 AbstractList 提供了 List 接口的骨架实现,大幅度的减少了实现迭代遍历相关操作的代码。可能这样表述有点抽象,点到 java.util.AbstractList 抽象类中看看,例如说 #iterator()、#indexOf( ...
阿里蚂蚁金服技术栈
阿里蚂蚁金服技术栈一、SOFARPC服务发布、引用以及调用的简单流程图如下: 当一个 SOFARPC 的应用启动时,如果发现当前应用需要发布 RPC 服务,那么 SOFARPC 会将该服务注册到配置中心,就是图中蓝色实线所示的过程。 当引用这个服务的 SOFA 应用启动时,会从配置中心订阅对应服务的地址,当配置中心收到订阅请求后,会将发布方的地址列表推送给订阅方,就是图中绿色实线所示的过程。 当引用服务的一方拿到地址以后,就可以调用服务了,就是图中蓝色虚线所示的过程。 使用步骤依赖 1234<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>rpc-enterprise-sofa-boot-starter</artifactId></dependency> 服务发布 1、设计服务接口类 12345678/** * 服务接口类 */public interface SampleService { public S ...
什么是Dubbo与Zookeeper
简介ZooKeeper 是一个开源的分布式协调服务 ZooKeeper 为我们提供了高可用、高性能、稳定的分布式数据一致性解决方案,通常被用于实现诸如数据发布/订阅、负载均衡、命名服务、分布式协调/通知、集群管理、Master 选举、分布式锁和分布式队列等功能。 顺序一致性: 从同一客户端发起的事务请求,最终将会严格地按照顺序被应用到 ZooKeeper 中去。 原子性: 所有事务请求的处理结果在整个集群中所有机器上的应用情况是一致的,也就是说,要么整个集群中所有的机器都成功应用了某一个事务,要么都没有应用。 单一系统映像 : 无论客户端连到哪一个 ZooKeeper 服务器上,其看到的服务端数据模型都是一致的。 可靠性: 一旦一次更改请求被应用,更改的结果就会被持久化,直到被下一次更改覆盖。 典型的应用场景: 分布式锁 : 通过创建唯一节点获得分布式锁,当获得锁的一方执行完相关代码或者是挂掉之后就释放锁。 命名服务 :可以通过 ZooKeeper 的顺序节点生成全局唯一 ID 数据发布/订阅 :通过 Watcher 机制 可以很方便地实现数据发布/订阅。当你将数据发布到 Zoo ...
如何手写一个LRU算法
LRU—–最近最少使用 缓存机制实现LRUCache类 LRUCache(int capacity )以正整数作为容量capacity初始化LRU缓存 int get( int key)如果关键字key存在缓存中,则返回关键字的值,否则返回-1 void put(int key,int value)如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组。 当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新数据值流出空间。 使用现成双向链表—–LinkedHashMap123456789101112131415161718192021class LRUCache extends LinkedHashMap<Integer, Integer>{ private int capacity; public LRUCache(int capacity) { super(capacity, 0.75F, true); this.capacity = capacity; ...
springmvc源码解析
架构 Spring MVC 一共有九大核心组件,分别是: MultipartResolver LocaleResolver ThemeResolver HandlerMapping HandlerAdapter HandlerExceptionResolver RequestToViewNameTranslator ViewResolver FlashMapManager ① 发送请求 用户向服务器发送 HTTP 请求,请求被 Spring MVC 的调度控制器 DispatcherServlet 捕获。 ② 映射处理器 DispatcherServlet 根据请求 URL ,调用 HandlerMapping 获得该 Handler 配置的所有相关的对象(包括 Handler 对象以及 Handler 对象对应的拦截器),最后以 HandlerExecutionChain 对象的形式返回。 ③ 处理器适配 DispatcherServlet 根据获得的 Handler,选择一个合适的HandlerAdapter 。(附注:如果成功获得 HandlerAdapter 后,此时将开始 ...
mybatis源码解析
整体架构 基础支持层反射、类型、日志、io、解析器、数据源、事务、缓存、Binding、注解、异常 以后细看 核心处理层配置解析mybatis-config.xml 解析~Mapper.xml 配置文件解析~SQL解析~ SQL执行~ 插件、 接口层SqlSession 接口~ MyBatis 编程步骤 创建 SqlSessionFactory 对象。 通过 SqlSessionFactory 获取 SqlSession 对象。 通过 SqlSession 获得 Mapper 代理对象。 通过 Mapper 代理对象,执行数据库操作。 执行成功,则使用 SqlSession 提交事务。 执行失败,则使用 SqlSession 回滚事务。 最终,关闭会话。 动态sql其执行原理为,使用 OGNL 的表达式,从 SQL 参数对象中计算表达式的值,根据表达式的值动态拼接 SQL ,以此来完成动态 SQL 的功能。 Mapper 接口的实现类,通过 MyBatis 使用 JDK Proxy 自动生成其代理对象 Proxy ,而代理对象 Proxy 会拦截接口方法,从而“调用”对应的 Mapp ...
spring源码解析
架构Spring Framework Spring 核心容器 Core Container Spring Core Spring Bean Spring Context SpEL (Spring Expression Language) 数据访问 Data Access JDBC ORM OXM Transaction Web WebMVC WebFlux WebSocket AOP AOP Aspects 其他 JMS (Java Messaging Service) Test Messaging 使用 Spring 框架带来的主要好处: DI :**Dependency Injection(DI)** 方法,使得构造器和 JavaBean、properties 文件中的依赖关系一目了然。 轻量级:与 EJB 容器相比较,IoC 容器更加趋向于轻量级。这样一来 IoC 容器在有限的内存和 CPU 资源的情况下,进行应用程序的开发和发布就变得十分有利。 面向切面编程(AOP): Spring 支持面 ...
什么是spring循环依赖?源码分析
是什么?循环依赖,其实就是循环引用,就是两个或者两个以上的 bean 互相引用对方,最终形成一个闭环,如 A 依赖 B,B 依赖 C,C 依赖 A。 循环依赖,其实就是一个死循环的过程,在初始化 A 的时候发现引用了 B,这时就会去初始化 B,然后又发现 B 引用 C,跑去初始化 C,初始化 C 的时候发现引用了 A,则又会去初始化 A,依次循环永不退出,除非有终结条件。 spring在singleton模式下,用三级缓存解决此问题,在原型(prototype)模式下无法解决。 参见官网 源码解析DefaultSingletonBeanRegistry类 第一层singletonObjects存放的是已经初始化好了的Bean,第二层earlySingletonObjects存放的是实例化了,但是未初始化的Bean,第三层singletonFactories存放的是FactoryBean。假如A类实现了FactoryBean,那么依赖注入的时候不是A类而是A类产生的Bean。 1A创建过程中需要B,于是A将自己放到三级缓里面,去实例化B2B实例化的时候发现需要A,于是B先查一级缓 ...
什么是AQS?源码分析
AQS简介java.util.concurrent.locks.AbstractQueuedSynchronizer 抽象类,简称 AQS ,是一个用于构建锁和同步容器的同步器。concurrent` 包内许多类都是基于 AQS 构建。如 ReentrantLock,Semaphore,CountDownLatch,ReentrantReadWriteLock,等。 AQS 使用一个 FIFO 的队列表示排队等待锁的线程,队列头节点称作“哨兵节点”,它不与任何线程关联。其他的节点与等待线程关联,每个节点维护一个等待状态 waitStatus 。 它是 J.U.C 并发包中的核心基础组件。 优势AQS 解决了在实现同步器时涉及当的大量细节问题,例如获取同步状态、FIFO 同步队列。基于 AQS 来构建同步器可以带来很多好处。它不仅能够极大地减少实现工作,而且也不必处理在多个位置上发生的竞争问题。 在基于 AQS 构建的同步器中,只能在一个时刻发生阻塞,从而降低上下文切换的开销,提高了吞吐量。同时在设计 AQS 时充分考虑了可伸缩性,因此 J.U.C 中,所有基于 AQS 构建的同步器均可 ...
Github骚操作
inseckill in:name —–查项目名含有seckill的项目 in:desciption 描述里包含 in:readme 组合使用 seckill in:name,readme starsspringboot stars:>=5000 —–查找stars数大于等于5000的项目 forks:>=500 awesome加强搜索 awesome redis ——查学习,教程, 文档, 书籍,pdf等 #L指出关键代码行号 地址#L77 地址#L77-L90 一段代码 https://github.com/codingXiaxw/seckill/blob/master/src/main/java/cn/codingxiaxw/service/SeckillService.java#L47 t搜索全局显示所有类 搜索同志location:shanghai language:java —–查上海的java大佬 https://files ...