Netty分布式FastThreadLocal的set方法怎么用

Netty分布式FastThreadLocal的set方法怎么用

本文小编为大家详细介绍“Netty分布式FastThreadLocal的set方法怎么用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Netty分布式FastThreadLocal的set方法怎么用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

FastThreadLocal的set方法实现

set方法, 其实就是修改线程共享对象, 作用域只是当前线程, 我们回顾根据上一小节demo中, 其中一个线程set对象的过程:

Netty分布式FastThreadLocal的set方法怎么用

线程set对象

newThread(newRunnable(){@Overridepublicvoidrun(){Objectobj=fastThreadLocalDemo.fastThreadLocalTest.get();try{for(inti=0;i<10;i++){fastThreadLocalDemo.fastThreadLocalTest.set(newObject());Thread.sleep(1000);}}catch(Exceptione){e.printStackTrace();}}}).start();

我们跟到set方法中:

publicfinalvoidset(Vvalue){if(value!=InternalThreadLocalMap.UNSET){set(InternalThreadLocalMap.get(),value);}else{remove();}}

这里首先判断我们当前设置的对象是不是UNSET, 因为这里不是UNSET, 所以进到if块中

if块调用了重载的set方法, 参数仍然为InternalThreadLocalMap, 有关InternalThreadLocalMap的get操作, 上一小节已经进行过分析, 这里不再赘述, 同时, 参数也传入了set的value值

我们跟到重载的set方法中:

publicfinalvoidset(InternalThreadLocalMapthreadLocalMap,Vvalue){if(value!=InternalThreadLocalMap.UNSET){if(threadLocalMap.setIndexedVariable(index,value)){addToVariablesToRemove(threadLocalMap,this);}}else{remove(threadLocalMap);}}

这里我们重点关注if(threadLocalMap.setIndexedVariable(index, value))这部分, 这里通过threadLocalMap调用setIndexedVariable方法进行对象的设置, 传入了当前FastThreadLocal的下标和value

我们跟到setIndexedVariable中

publicbooleansetIndexedVariable(intindex,Objectvalue){Object[]lookup=indexedVariables;if(index<lookup.length){ObjectoldValue=lookup[index];lookup[index]=value;returnoldValue==UNSET;}else{expandIndexedVariableTableAndSet(index,value);returntrue;}}

这里的逻辑其实和get非常类型, 都是直接通过索引操作的, 这里根据索引值, 直接通过数组下标的方式对元素进行设置, 熟悉上一小节内容的同学对此应该不会陌生

回到FastThreadLocal的Set方法中:

publicfinalvoidset(Vvalue){if(value!=InternalThreadLocalMap.UNSET){set(InternalThreadLocalMap.get(),value);}else{remove();}}

刚才我们分析了如果修改的对象不是UNSET对象的操作, 如果修改的对象是UNSET对象, 则会调用remove方法

跟进remove方法:

publicfinalvoidremove(InternalThreadLocalMapthreadLocalMap){if(threadLocalMap==null){return;}Objectv=threadLocalMap.removeIndexedVariable(index);removeFromVariablesToRemove(threadLocalMap,this);if(v!=InternalThreadLocalMap.UNSET){try{onRemoval((V)v);}catch(Exceptione){PlatformDependent.throwException(e);}}}

Object v = threadLocalMap.removeIndexedVariable(index)

这一步是根据索引index, 将值设置成UNSET

我们跟进removeIndexedVariable方法

publicObjectremoveIndexedVariable(intindex){Object[]lookup=indexedVariables;if(index<lookup.length){Objectv=lookup[index];lookup[index]=UNSET;returnv;}else{returnUNSET;}}

这里的逻辑也比较简单, 根据index通过数组下标的方式将元素设置成UNSET对象

回到remove方法中:

if(v != InternalThreadLocalMap.UNSET)

这里判断如果我们设置的值不是UNSET对象, 则会调用onRemoval方法

跟进onRemoval方法:

protectedvoidonRemoval(@SuppressWarnings("UnusedParameters")Vvalue)throwsException{}

这里是个空实现, 用于交给子类去完成

读到这里,这篇“Netty分布式FastThreadLocal的set方法怎么用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注恰卡编程网行业资讯频道。

发布于 2022-03-29 22:33:34
收藏
分享
海报
0 条评论
26
上一篇:javascript怎么实现简易的计算器功能 下一篇:Netty的FastThreadLocal和Recycler实例分析
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码