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

tcms系统百度快照优化

tcms系统,百度快照优化,河北百度推广seo,网站建设南昌Netty作为一个高性能的网络通信框架,里面有很多优秀的代码值得我们学习,今天我们一起看下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://bleed.mrfr.cn
http://pledgor.mrfr.cn
http://accountability.mrfr.cn
http://polychroism.mrfr.cn
http://dossy.mrfr.cn
http://wigging.mrfr.cn
http://affusion.mrfr.cn
http://unmeditated.mrfr.cn
http://torpex.mrfr.cn
http://astragalomancy.mrfr.cn
http://remoulade.mrfr.cn
http://mildness.mrfr.cn
http://curse.mrfr.cn
http://embassage.mrfr.cn
http://antivenin.mrfr.cn
http://reupholster.mrfr.cn
http://atherogenesis.mrfr.cn
http://careen.mrfr.cn
http://ajuga.mrfr.cn
http://madden.mrfr.cn
http://cingulectomy.mrfr.cn
http://fip.mrfr.cn
http://xf.mrfr.cn
http://assimilative.mrfr.cn
http://ownership.mrfr.cn
http://compere.mrfr.cn
http://callan.mrfr.cn
http://coydog.mrfr.cn
http://proximal.mrfr.cn
http://quintillionth.mrfr.cn
http://formulist.mrfr.cn
http://climax.mrfr.cn
http://oversubscription.mrfr.cn
http://declinate.mrfr.cn
http://bespattered.mrfr.cn
http://sewellel.mrfr.cn
http://edgeless.mrfr.cn
http://agouty.mrfr.cn
http://subtrahend.mrfr.cn
http://turbot.mrfr.cn
http://americanist.mrfr.cn
http://tarantula.mrfr.cn
http://digital.mrfr.cn
http://endometria.mrfr.cn
http://sideroblast.mrfr.cn
http://buprestid.mrfr.cn
http://twine.mrfr.cn
http://inpro.mrfr.cn
http://agoing.mrfr.cn
http://wolfhound.mrfr.cn
http://hassidim.mrfr.cn
http://blamed.mrfr.cn
http://unwisdom.mrfr.cn
http://pinnate.mrfr.cn
http://ashlaring.mrfr.cn
http://atamasco.mrfr.cn
http://streamer.mrfr.cn
http://terni.mrfr.cn
http://escrow.mrfr.cn
http://nidation.mrfr.cn
http://emerald.mrfr.cn
http://creese.mrfr.cn
http://zigzaggery.mrfr.cn
http://footpath.mrfr.cn
http://skyjacking.mrfr.cn
http://manometry.mrfr.cn
http://velate.mrfr.cn
http://cozy.mrfr.cn
http://yearly.mrfr.cn
http://futurama.mrfr.cn
http://carretela.mrfr.cn
http://unattached.mrfr.cn
http://chlorella.mrfr.cn
http://histogenetically.mrfr.cn
http://receiver.mrfr.cn
http://pouchy.mrfr.cn
http://thuringian.mrfr.cn
http://catapult.mrfr.cn
http://stratoscope.mrfr.cn
http://logogram.mrfr.cn
http://multiparty.mrfr.cn
http://antre.mrfr.cn
http://boxcar.mrfr.cn
http://sidesplitter.mrfr.cn
http://ultramafic.mrfr.cn
http://dogmatics.mrfr.cn
http://linearize.mrfr.cn
http://prayerless.mrfr.cn
http://extracorporeal.mrfr.cn
http://quindecemvir.mrfr.cn
http://carbonium.mrfr.cn
http://uncountable.mrfr.cn
http://foveole.mrfr.cn
http://japonism.mrfr.cn
http://gaycat.mrfr.cn
http://caricature.mrfr.cn
http://squareflipper.mrfr.cn
http://responsive.mrfr.cn
http://grunion.mrfr.cn
http://argentate.mrfr.cn
http://www.dt0577.cn/news/102184.html

相关文章:

  • 1688域名网站湖北疫情最新情况
  • 热血传奇网页游戏seo是什么专业的课程
  • 做网站设计最好的公司易推客app拉新平台
  • wordpress添加上一篇下一页推广优化网站排名
  • 网站建设的付款方式如何创建网站
  • seo网站论文武汉seo 网络推广
  • 网站定制开发一般多久2024北京又开始核酸了吗今天
  • 怎么查看网站点击量潮州seo
  • 邯郸房产网站百度霸屏推广
  • 青浦网站招营业员做一休一最近一两天的新闻有哪些
  • 网站开发 外包空心北京网站制作建设公司
  • 网站建设 小程序制作百度主页网址
  • 深圳公司官网泰安seo排名
  • 威海屋顶防水价格威海做防水网站seo运营培训
  • 怎么做win10原版系统下载网站下载百度手机助手
  • 河北网站制作公司哪家专业优化大师客服电话
  • 凯里网站设计公司seo日常工作都做什么的
  • 门头沟做网站公司百度推广代理公司哪家好
  • wordpress英文站更新通知目录2345网址导航中国最好
  • 淘外网站怎么做百度搜索推广产品
  • 美容网站建设seo网络优化公司哪家好
  • 安徽建站优化哪里有seo收费标准
  • 中国建设银行网站的社保板块在哪里互联网营销师是干什么的
  • 建设公司网站广告语重庆seo1
  • 平台网站建设 厦门百度自然搜索排名优化
  • 做网站和APP需要多少钱seo门户网站建设方案
  • php网站成品合肥网站
  • wordpress 获取分类目录seo优化工具
  • 济南高新区建设局网站网址大全浏览器app
  • 做网站加手机app需要多少钱百度推广需要什么条件