Giter VIP home page Giter VIP logo

simple-netty-threading-model's Introduction

模仿Netty的NIO实现

这个Demo旨在利用Java原生API模仿 Netty 的NIO线程模型的设计, 以及相关的类架构. 目前只有Acceptor, EventLoop的部分.

相关博客已更新, 访问请戳这里

结构讲解

概述

___Acceptor___是用于接收客户连接请求的线程. 在接收到客户端连接后, 将连接注册到单线程EventLoop的selector中. 然后在EventLoop中对于各种异步IO事件进行响应. 响应行为由EventLoop委托ConnectionWrapper执行. 对应关系:

Netty Demo
EventLoop EventLoop
(io.netty.channel)Channel ConnectionWrapper
AbstractUnsafe.outboundBuffer ConnectionWrapper.outbound

EventLoop

每个EventLoop都是单独的一个线程, 在这个线程中, EventLoop负责用Selector轮询可响应的IO事件. Acceptor获取到的客户端连接都由EventLoop管理, 并且一个连接只注册一个EventLoop. 这样这条连接的所有IO事件都会由注册的EventLoop单线程进行处理, 实现了每条连接的IO串行化, 一定程度上减小了编程难度.

ConnectionWrapper

连接包装器将IO操作屏蔽, 便于在EventLoop中调用. 主要是实现read, write等操作. 用专门的容器存储读入或要写出的数据, 在适当的时机进行操作.

inbound & outbound

读入或写出数据的缓存容器, 这里是简单用List存储ByteBuffer. 在Netty中, 借助Netty的ByteBuf, 会有更高效的实现.

simple-netty-threading-model's People

Contributors

gotz9 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.