简体   繁体   English

从一个线程写入socketchannel并从另一线程选择

[英]write to socketchannel from one thread and select from other thread

Can I write to SocketChannel from one thread, while same SocketChannel may be being selected by Selector#select() for IO operations in other thread? 我可以从一个线程写入SocketChannel ,而Selector#select()为另一个线程的IO操作Selector#select()相同的SocketChannel吗? Is it thread-safe? 它是线程安全的吗? Or do I have to delegate ByteBuffer s to other thread (which do Selector#select() indefinitely), to be written to SocketChannel every after Selector#select() calls. 还是我必须将ByteBuffer委托给其他线程(无限期执行Selector#select() ),以便在Selector#select()调用之后每次都将ByteBuffer写入SocketChannel

Also, is SelectionKey#interestOps(int) to set interest operations thread-safe? 另外, SelectionKey#interestOps(int)可以设置兴趣操作线程安全吗? Or do I have to delegate same to other thread (which do Selector#select() indefinitely). 还是我必须将其委托给其他线程(无限期执行Selector#select() )。

Yes you can,in general. 是的,总的来说可以。 The thread that's blocked in select() isn't using the ByteBuffer while it's blocked. select()被阻塞的线程在被阻塞时未使用ByteBuffer If it does so elsewhere, of course you need to synchronize, or take some other measure. 如果在其他地方这样做,则当然需要同步或采取其他措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM