从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的最新版本中,有containers.Map数据结构。 查看更多的MATLAB Map容器 。 这在使用STRUCTs时删除了一些限制。 例如 c = containers.Map c('foo') = 1 c(' not a var name ') = 2 keys(c) values(c) In recent versions of MATLAB, there's the containers.Map data structure. See MATLAB Map containers f ...
-
更快的Matlab表创建(Faster Matlab Table Creation)[2022-07-24]
根据MathWorks员工的回复 ,如果不改变核心Matlab文件,就无法做到这一点。 Based on this reply from a MathWorks employee, you can't do it without altering the core Matlab files. -
我在你的问题中看不到任何问题,但总的来说是表达方式 hp == w 如果向量具有相同的长度,则返回该长度的0s和1s的向量,表示两个向量匹配( 1 )或不匹配( 0 )的情况。 请注意,浮点数相等的比较充满了“问题”,您可能会更好地评估: abs(hp-w) < 10^-6 用你喜欢的公差替换10^-6 。 根据你对w的定义,你应该能够写出来 hp == 1/(x.^2+1) 注意使用元素平方运算符.^ ,它返回一个与x长度相同的向量,每个元素都是x中相应元素的平方。 当然,表达 hp - 1./(x ...
-
重塑Matlab表(Reshape Matlab table)[2022-05-07]
这可以使用自定义函数使用accumarray完成。 第一步是将T的name列转换为数字向量; 然后可以应用accumarray 。 这种方法要求T按照第1列进行排序,因为只有在这种情况下accumarray确保accumarray保持顺序(如文档中所示)。 因此,如果T可能没有排序(尽管在您的示例中),请首先使用sortrows对其进行sortrows 。 T = sortrows(T, 1); %// you can remove this line if T is guaranteed to be so ... -
我的Matlab给你的代码提供了不同的错误信息。 >> rows_attack = strcmp(T(:,2),'attack') rows_attack = 0 >> T(rows_attack,2) = 1 Right hand side of an assignment into a table must be another table or a cell array. >> T(rows_attack,2) ans = empty 0-by-1 table 错误 ...
-
MATLAB:增长哈希表(MATLAB: Growing hash table)[2023-07-12]
我假设你的意思是你使用内建的containers.Map对象作为散列表。 尽管没有直接的方法来预先分配这样一个对象,但我建议你使用一个简单的两列单元数组或一个java.util.Hashtable对象,它们通常比containers.Map快得多containers.Map 。 参考: https : //undocumentedmatlab.com/blog/using-java-collections-in-matlab I assume you mean that you're using the b ... -
spdiags是要走的路, A = sparse(Nx); A = spdiags(b*ones(Nx-1,1), -1, A); A = spdiags(a*ones(Nx,1), 0, A); A = spdiags(b*ones(Nx-1,1), 1, A); A(1, 1:2) = [1,1]; A(N, N-1:N) = [1,1]; spdiags is the way to go, A = sparse(Nx); A = spdiags(b*ones(Nx-1,1), -1, A); A = ...
-
我同意这个问题不是很清楚,显示一些代码会有所帮助。 无论如何我会尝试。 为了使LUT有意义,必须将由tx获得的值集限制为例如整数。 假设指数可以是-1000到1000之间的任何整数,你可以像这样创建一个LUT: LUT = exp(-1000:1000); 然后创建索引(假设t是一维数组,x是二维数组) indexArray = bsxfun(@minus,reshape(t,[1,1,3]), x) + 1001; %# -1000 turns into 1 最后,您创建结果 output = LUT ...
-
如果你不介意全部完成,那么[~, ans] = sort(a)就可以了。 然后,您可以获取实际需要的ans的前几个元素。 sort的构建非常快,尽管找到了所有的分钟而不仅仅是你需要的分钟,但这应该足够高效。 If you don't mind doing them all, then [~, ans] = sort(a) will do the trick. You can then take the first few elements of ans that you actually need. The ...
-
以下解决方案解决了以下问题: 将最后一个表列转换为单元格数组(保持不同长度的字符串需要单元格数组)。 从单元格数组中删除所有NaN元素 (NaN元素会破坏下一节)。 查找单元格数组中最重复的字符串 。 在stringColumn中查找NaN元素的所有indeces(我使用cellfun基于上一节的cellfun )。 找到的indeces中的Rplace元素,最常见的字符串。 由于您是Matlab的新手,我的解决方案对您来说看起来非常复杂(对我来说这看起来很复杂)。 可能有一个更简单的解决方案,我找不到.. ...