从Netty Handler内部访问Netty服务器的实例(Accessing an instance of Netty server from inside a Netty Handler)
我正在为多人游戏编写Netty服务器,我不确定是否需要以某种方式同步服务器中但由ChannelHandler访问的变量。
在服务器级别,我使用ArrayList来存储服务器将要服务的不同匹配。
每个匹配将引用2个通道(我为每个匹配存储ChannelHandlerContetx)。
当我创建从SimpleChannelInboundHandler扩展的ChannelHandler时,我将服务器的实例传递给构造函数,并将服务器作为实例变量存储在处理程序中。
当触发channelActive时,ChannerlHandler将在“等待”状态下搜索ArrayList(它位于服务器实例中)中的匹配项。 如果找到一个绑定它并改变匹配状态。 如果不是,则创建新的匹配,然后通道绑定到它,使其处于等待状态。
我知道频道是线程安全的。 但是这里不同的通道正在访问同一服务器的ArrayList实例。
在这种情况下,我应该注意同步对ArrayList的访问吗?
注意如果它添加到我的问题:因为我将在后端有一个数据库,我在创建处理程序时在.addLast()方法中传递一个DefaultEventExecutor。
I am writting a Netty Server for a multiplayer game and I am not sure if I need to synchronize in some way a variable that lives in the server but is accessed by the ChannelHandler.
At the server level I am using an ArrayList to store the different matches the server will be serving.
Each match will be referencing 2 channels (I store for the match the ChannelHandlerContetx for each one).
When I create the ChannelHandler that extends from SimpleChannelInboundHandler I pass an instance of the server to the constructor and I store the server as an instance variable in the handler.
When channelActive is fired, the ChannerlHandler will search for a match in the ArrayList (that lives in the server instance) in "Waiting" state. If it finds one it bounds to it and changes the match status. If not a new Match is created and then the channel bounds to it leaving it in Waiting status.
I know that channels are Thread Safe. But here the different channels are accessing the same server's ArrayList instance.
In this case should I take care of synchronizing the access to the ArrayList?
Note in case it adds to my question: As I am going to have a database on the backend, I am passing a DefaultEventExecutor in the .addLast() method when the handler is being created.
原文:https://stackoverflow.com/questions/23840097
满意答案
我在你的问题中看不到任何问题,但总的来说是表达方式
hp == w
如果向量具有相同的长度,则返回该长度的0s和1s的向量,表示两个向量匹配(
1
)或不匹配(0
)的情况。 请注意,浮点数相等的比较充满了“问题”,您可能会更好地评估:abs(hp-w) < 10^-6
用你喜欢的公差替换
10^-6
。根据你对
w
的定义,你应该能够写出来hp == 1/(x.^2+1)
注意使用元素平方运算符
.^
,它返回一个与x
长度相同的向量,每个元素都是x
中相应元素的平方。 当然,表达hp - 1./(x.^2+1)
将返回差异的向量,这可能是你想要的。
I see no
w
in your question, but in general the expressionhp == w
will, if the vectors have the same length, return a vector of
0
s and1
s, of that length, representing the cases where the two vectors match (1
) or don't match (0
). In passing, note that comparison for equality of floating-point numbers is fraught with 'issues' and you might be better evaluating:abs(hp-w) < 10^-6
replacing
10^-6
by your preferred tolerance.Given your definition for
w
you should be able to writehp == 1/(x.^2+1)
Note the use of the elementwise squaring operator
.^
, which returns a vector of the same length asx
with each element the square of the corresponding element inx
. Of course, the expressionhp - 1./(x.^2+1)
will return a vector of the differences, which might be what you want.
相关问答
更多如何在MATLAB中使用哈希表(字典)?(How to use Hash Tables (dictionaries) in MATLAB?)
更快的Matlab表创建(Faster Matlab Table Creation)
如何在matlab中使用以下值构建表?(How I can build a table in matlab with the following values?)
重塑Matlab表(Reshape Matlab table)
在Matlab表中将字符串值更改为数字(Change string values to number in Matlab table)
MATLAB:增长哈希表(MATLAB: Growing hash table)
如何在matlab中生成具有下一个值的矩阵?(How to generate a matrix in matlab with the next values?)
如何在MATLAB中使用查找表(How to use a look up table in MATLAB)
Matlab在列中查找x min值的索引(Matlab find indexes of x min values in column)
Matlab的。(Matlab. Replace missed values with an avg)
相关文章
更多[Netty 1] 初识Netty
Netty源码分析
Netty入门实例-时间服务器
Netty入门实例-编写服务器端程序
Netty基于流的传输处理
Netty开发环境配置
Netty环境配置
storm学习之Netty代替ZMQ
在Twitter,Netty 4 GC开销降为五分之一
Netty入门实例-使用POJO代替ByteBuf
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!