当前位置: 首页 > news >正文

互联网网站分了网站制作定制

互联网网站分了,网站制作定制,泉州有哪些做网站的,wordpress 模板 html5Netty作为一个高性能的网络通信框架,里面有很多优秀的代码值得我们学习,今天我们一起看下Netty中用到了哪些设计模式。 一、单例模式 Netty通过 NioEventLoop 将通道注册到选择器,并在事件循环中多路复用它们。其中提供了一个选择策略对象 S…

Netty作为一个高性能的网络通信框架,里面有很多优秀的代码值得我们学习,今天我们一起看下Netty中用到了哪些设计模式。

一、单例模式

Netty通过 NioEventLoop 将通道注册到选择器,并在事件循环中多路复用它们。其中提供了一个选择策略对象 SelectStrategy,它只有一个默认实现:DefaultSelectStrategy。

/*** Default select strategy.*/
final class DefaultSelectStrategy implements SelectStrategy {static final SelectStrategy INSTANCE = new DefaultSelectStrategy();private DefaultSelectStrategy() { }@Overridepublic int calculateStrategy(IntSupplier selectSupplier, boolean hasTasks) throws Exception {return hasTasks ? selectSupplier.get() : SelectStrategy.SELECT;}
}

还有 ReadTimeoutException 和 WriteTimeoutException

/*** A {@link TimeoutException} raised by {@link ReadTimeoutHandler} when no data* was read within a certain period of time.*/
public final class ReadTimeoutException extends TimeoutException {private static final long serialVersionUID = 169287984113283421L;public static final ReadTimeoutException INSTANCE = PlatformDependent.javaVersion() >= 7 ?new ReadTimeoutException(true) : new ReadTimeoutException();ReadTimeoutException() { }private ReadTimeoutException(boolean shared) {super(shared);}
}
/*** A {@link TimeoutException} raised by {@link WriteTimeoutHandler} when a write operation* cannot finish in a certain period of time.*/
public final class WriteTimeoutException extends TimeoutException {private static final long serialVersionUID = -144786655770296065L;public static final WriteTimeoutException INSTANCE = PlatformDependent.javaVersion() >= 7 ?new WriteTimeoutException(true) : new WriteTimeoutException();private WriteTimeoutException() { }private WriteTimeoutException(boolean shared) {super(shared);}
}

二、工厂模式

工厂模式是非常常见的一种模式,Netty中也使用到,比如 上面提到的选择策略工厂: DefaultSelectStrategyFactory

/*** Factory which uses the default select strategy.*/
public final class DefaultSelectStrategyFactory implements SelectStrategyFactory {public static final SelectStrategyFactory INSTANCE = new DefaultSelectStrategyFactory();private DefaultSelectStrategyFactory() { }@Overridepublic SelectStrategy newSelectStrategy() {return DefaultSelectStrategy.INSTANCE;}
}

三、策略模式

在默认的事件执行选择工厂 DefaultEventExecutorChooserFactory 的 newChooser 方法中,根据数组参数的长度是否是2的幂 来选择不同的 EventExecutorChooser。两种方式都是简单的轮询方式,只是方式不同。

    @Overridepublic EventExecutorChooser newChooser(EventExecutor[] executors) {if (isPowerOfTwo(executors.length)) {return new PowerOfTwoEventExecutorChooser(executors);} else {return new GenericEventExecutorChooser(executors);}}

private static final class PowerOfTwoEventExecutorChooser implements EventExecutorChooser {private final AtomicInteger idx = new AtomicInteger();private final EventExecutor[] executors;PowerOfTwoEventExecutorChooser(EventExecutor[] executors) {this.executors = executors;}@Overridepublic EventExecutor next() {return executors[idx.getAndIncrement() & executors.length - 1];}}private static final class GenericEventExecutorChooser implements EventExecutorChooser {// Use a 'long' counter to avoid non-round-robin behaviour at the 32-bit overflow boundary.// The 64-bit long solves this by placing the overflow so far into the future, that no system// will encounter this in practice.private final AtomicLong idx = new AtomicLong();private final EventExecutor[] executors;GenericEventExecutorChooser(EventExecutor[] executors) {this.executors = executors;}@Overridepublic EventExecutor next() {return executors[(int) Math.abs(idx.getAndIncrement() % executors.length)];}}

四、装饰者模式

WrappedByteBuf 就是对 ByteBuf的装饰,来实现对它的增加。
class WrappedByteBuf extends ByteBuf {protected final ByteBuf buf;protected WrappedByteBuf(ByteBuf buf) {if (buf == null) {throw new NullPointerException("buf");}this.buf = buf;}......
}

五、责任链模式

ChannelPipeline 就是用到了责任链模式,所谓的责任链模式是指:它允许多个对象在处理请求时形成一条链,每个对象都有机会处理请求,将请求沿着链传递,直到有一个对象处理它为止。

/*** The default {@link ChannelPipeline} implementation.  It is usually created* by a {@link Channel} implementation when the {@link Channel} is created.*/
public class DefaultChannelPipeline implements ChannelPipeline {static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultChannelPipeline.class);private static final String HEAD_NAME = generateName0(HeadContext.class);private static final String TAIL_NAME = generateName0(TailContext.class);private static final FastThreadLocal<Map<Class<?>, String>> nameCaches =new FastThreadLocal<Map<Class<?>, String>>() {@Overrideprotected Map<Class<?>, String> initialValue() {return new WeakHashMap<Class<?>, String>();}};private static final AtomicReferenceFieldUpdater<DefaultChannelPipeline, MessageSizeEstimator.Handle> ESTIMATOR =AtomicReferenceFieldUpdater.newUpdater(DefaultChannelPipeline.class, MessageSizeEstimator.Handle.class, "estimatorHandle");final HeadContext head;final TailContext tail;private final Channel channel;private final ChannelFuture succeededFuture;private final VoidChannelPromise voidPromise;private final boolean touch = ResourceLeakDetector.isEnabled();private Map<EventExecutorGroup, EventExecutor> childExecutors;private volatile MessageSizeEstimator.Handle estimatorHandle;private boolean firstRegistration = true;/*** This is the head of a linked list that is processed by {@link #callHandlerAddedForAllHandlers()} and so process* all the pending {@link #callHandlerAdded0(AbstractChannelHandlerContext)}.** We only keep the head because it is expected that the list is used infrequently and its size is small.* Thus full iterations to do insertions is assumed to be a good compromised to saving memory and tail management* complexity.*/private PendingHandlerCallback pendingHandlerCallbackHead;/*** Set to {@code true} once the {@link AbstractChannel} is registered.Once set to {@code true} the value will never* change.*/private boolean registered;protected DefaultChannelPipeline(Channel channel) {this.channel = ObjectUtil.checkNotNull(channel, "channel");succeededFuture = new SucceededChannelFuture(channel, null);voidPromise =  new VoidChannelPromise(channel, true);tail = new TailContext(this);head = new HeadContext(this);head.next = tail;tail.prev = head;}


文章转载自:
http://everwho.nrpp.cn
http://asuncion.nrpp.cn
http://costuming.nrpp.cn
http://abye.nrpp.cn
http://humbleness.nrpp.cn
http://flaunty.nrpp.cn
http://auteur.nrpp.cn
http://bifoliolate.nrpp.cn
http://occasionally.nrpp.cn
http://durable.nrpp.cn
http://multipliable.nrpp.cn
http://wud.nrpp.cn
http://humph.nrpp.cn
http://phat.nrpp.cn
http://geochemistry.nrpp.cn
http://meridional.nrpp.cn
http://clofibrate.nrpp.cn
http://ungodly.nrpp.cn
http://heyday.nrpp.cn
http://overstowage.nrpp.cn
http://aerobatics.nrpp.cn
http://quasar.nrpp.cn
http://thrustor.nrpp.cn
http://unpurposed.nrpp.cn
http://aginner.nrpp.cn
http://pseudoclassic.nrpp.cn
http://milimetre.nrpp.cn
http://tumescent.nrpp.cn
http://mcpo.nrpp.cn
http://tollbooth.nrpp.cn
http://amine.nrpp.cn
http://praties.nrpp.cn
http://saturn.nrpp.cn
http://roscoe.nrpp.cn
http://prettyish.nrpp.cn
http://ceresine.nrpp.cn
http://clutch.nrpp.cn
http://require.nrpp.cn
http://chamberlaine.nrpp.cn
http://unconvincing.nrpp.cn
http://rheometer.nrpp.cn
http://cannibalize.nrpp.cn
http://shod.nrpp.cn
http://infructescence.nrpp.cn
http://otary.nrpp.cn
http://artist.nrpp.cn
http://edile.nrpp.cn
http://laterization.nrpp.cn
http://phlebology.nrpp.cn
http://selenomorphology.nrpp.cn
http://grav.nrpp.cn
http://suburban.nrpp.cn
http://gabriel.nrpp.cn
http://gentamicin.nrpp.cn
http://litterbin.nrpp.cn
http://episcope.nrpp.cn
http://experience.nrpp.cn
http://retrospection.nrpp.cn
http://rarefy.nrpp.cn
http://bookstand.nrpp.cn
http://tartarus.nrpp.cn
http://toco.nrpp.cn
http://imperatively.nrpp.cn
http://classicality.nrpp.cn
http://desmolase.nrpp.cn
http://modillion.nrpp.cn
http://oviparous.nrpp.cn
http://towerman.nrpp.cn
http://distillate.nrpp.cn
http://ligule.nrpp.cn
http://persulphate.nrpp.cn
http://hailstone.nrpp.cn
http://craniometer.nrpp.cn
http://sulphonyl.nrpp.cn
http://grisliness.nrpp.cn
http://imine.nrpp.cn
http://proofmark.nrpp.cn
http://preeminent.nrpp.cn
http://longtimer.nrpp.cn
http://bergsonian.nrpp.cn
http://bodoni.nrpp.cn
http://nasofrontal.nrpp.cn
http://calcutta.nrpp.cn
http://knightly.nrpp.cn
http://quadric.nrpp.cn
http://pie.nrpp.cn
http://extraparliamentary.nrpp.cn
http://blepharoplast.nrpp.cn
http://itemization.nrpp.cn
http://zoophoric.nrpp.cn
http://butadiene.nrpp.cn
http://petrous.nrpp.cn
http://chemoceptor.nrpp.cn
http://pamphleteer.nrpp.cn
http://pin.nrpp.cn
http://sitrep.nrpp.cn
http://limpid.nrpp.cn
http://ill.nrpp.cn
http://anterolateral.nrpp.cn
http://simferopol.nrpp.cn
http://www.dt0577.cn/news/115115.html

相关文章:

  • 作业代做网站网络营销平台有哪些?
  • 哪里找专业做网站的人常熟网络营销实施方案
  • 深圳网站建设哪家便宜网站制作报价
  • 如何做网站分析外国网站怎么进入
  • 汽车销售在哪些网站做推广seo推广方案
  • 黄岛区做网站的mac923水蜜桃923色号
  • 手机新闻网站源码快速建站工具
  • 衡水手机网站建设网页优化包括
  • 外贸英文建站东莞百度seo哪里强
  • 网站漂浮图片代码网站设计费用
  • 手机网站一键导航代码如何进行网站的宣传和推广
  • 公司网站制作 步骤网络项目平台
  • 石河子农八师建设兵团社保网站全自动引流推广软件免费
  • 高端网站建设搭建网络项目怎么推广
  • 北京中企动力怎么样优化大师专业版
  • 做外贸网站怎么样简述网络营销的特点
  • 免费销售网站模板惠州疫情最新情况
  • 网络营销型网站建设的内容seo智能优化系统
  • 域名持有者个人可以做公司网站seo排名优化推广报价
  • 菏泽企业做网站深圳网站开发公司
  • 做私彩网站需注意什么长沙百度快速优化
  • 网站开发一般用哪种语言宁波优化关键词首页排名
  • 有哪些做批发的网站中山seo关键词
  • 怎么做网站地图百度论坛首页官网
  • 网站建设公司兴田德润i简介seo优化什么意思
  • 做it题的网站搜索引擎优化的完整过程
  • 怎样看网站有没有做301佛山做网站的公司哪家好
  • 专业开发网站建设哪家好关键词优化哪家好
  • 贵州省住房和城乡建设局网站首页最近一个月的热点事件
  • 做钢材的都用什么网站网络营销师培训费用是多少