Netty分布式FastThreadLocal的set方法怎么用
Netty分布式FastThreadLocal的set方法怎么用
本文小编为大家详细介绍“Netty分布式FastThreadLocal的set方法怎么用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Netty分布式FastThreadLocal的set方法怎么用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
FastThreadLocal的set方法实现
set方法, 其实就是修改线程共享对象, 作用域只是当前线程, 我们回顾根据上一小节demo中, 其中一个线程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方法怎么用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注恰卡编程网行业资讯频道。
推荐阅读
-
php大文件下载处理
set_time_limit(0);ini_set('max_execution_time',0);//getthe...
-
go语言中有set集合吗
go语言中有set集合吗本文小编为大家详细介绍“go语言中有set...
-
Netty的FastThreadLocal和Recycler实例分析
Netty的FastThreadLocal和Recycler实例分析...
-
Netty分布式高性能工具类recycler如何使用
Netty分布式高性能工具类recycler如何使用这篇文章主要介...
-
php如何实现Redis的Set操作
php如何实现Redis的Set操作这篇文章给大家分享的是有关ph...
-
python如何使用Set
python如何使用Set这篇文章主要为大家展示了“python如...
-
Java的IO模型和Netty框架是什么
Java的IO模型和Netty框架是什么这篇文章主要介绍“Java...
-
Python中的Set与dict实例分析
Python中的Set与dict实例分析这篇文章主要讲解了“Pyt...
-
如何实现Netty的服务端Channel不支持写操作
如何实现Netty的服务端Channel不支持写操作小编给大家分享...
-
Netty发送队列积压导致内存泄露怎么办
Netty发送队列积压导致内存泄露怎么办这篇文章主要为大家展示了“...