首页 \ 问答 \ 从Netty Handler内部访问Netty服务器的实例(Accessing an instance of Netty server from inside a Netty Handler)

从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
更新时间:2022-03-21 06:03

最满意答案

我在你的问题中看不到任何问题,但总的来说是表达方式

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 expression

hp == w

will, if the vectors have the same length, return a vector of 0s and 1s, 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 write

hp == 1/(x.^2+1)

Note the use of the elementwise squaring operator .^, which returns a vector of the same length as x with each element the square of the corresponding element in x. Of course, the expression

hp - 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 ...
  • 根据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 ...
  • 这可以使用自定义函数使用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 错误 ...
  • 我假设你的意思是你使用内建的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的新手,我的解决方案对您来说看起来非常复杂(对我来说这看起来很复杂)。 可能有一个更简单的解决方案,我找不到.. ...

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)