我有一個Netty應用程序,我想讓多個線程寫入一個頻道。我只是想知道Channel.write是否是線程安全的?Netty Channel.write線程安全嗎?
回答
它是線程安全的,所以你不必擔心。
不,它是線程不安全的,因爲Channel.write
在其管道的HeadContext中調用ChannelOutboundBuffer.addMessage
,而ChannelOutboundBuffer.addMessage
絕對是線程不安全的。看看下面的代碼:
public void addMessage(Object msg, int size, ChannelPromise promise) {
Entry entry = Entry.newInstance(msg, size, total(msg), promise);
if (tailEntry == null) {
flushedEntry = null;
tailEntry = entry;
} else {
Entry tail = tailEntry;
tail.next = entry;
tailEntry = entry;
}
if (unflushedEntry == null) {
unflushedEntry = entry;
}
// increment pending bytes after adding message to the unflushed arrays.
// See https://github.com/netty/netty/issues/1619
incrementPendingOutboundBytes(size, false);
}
所以你應該在任何時候至多有一個線程調用Channel.write。代碼格式有問題,您可以在http://netty.io/4.0/xref/index.html – yuguoliang
上找到它。我無法確認您的聲明。我看到'AbstractChannel.write'調用了'Pipeline.write',它在獲取該通道的事件循環後最終調用'AbstractUnsafe.write'。在這個方法裏面有一個斷言,線程在事件循環內部,所以它一次只能被1個線程調用 – Ferrybig
正如你可以從代碼中看到,該ChannelOutboundBuffer.addMessage()
方法本身不是線程安全的。但是,寫入通道是「線程安全的」,因爲netty在單個I/O線程中執行寫入任務/方法。
- 1. 是Netty Channel.close()線程安全嗎?
- 2. 是Netty ChannelHandlerContext.set/getAttachment()線程安全嗎?
- 3. 我可以在非Netty線程中調用`Channel.write()`嗎?
- 4. java線程安全:線程安全嗎?
- 5. Netty的Channel.write不工作
- 6. Netty channel.write不寫信息
- 7. Spring mongoTemplate線程安全嗎?
- 8. Lparallel.queue線程安全嗎?
- 9. BoxClient線程安全嗎?
- 10. EventHubClient.SendBatchAsync - 線程安全嗎?
- 11. .NET:JsonMediaTypeFormatter線程安全嗎?
- 12. multiset equal_range線程安全嗎?
- 13. pip線程安全嗎?
- 14. com.google.cloud.datastore.Datastore線程安全嗎?
- 15. WNetGetResourceInformation線程安全嗎?
- 16. qsort線程安全嗎?
- 17. work_queue線程安全嗎?
- 18. .NET DateTime線程安全嗎
- 19. DynamoDBContext線程安全嗎?
- 20. 在Python線程安全嗎?
- 21. java.lang.reflect.Method線程安全嗎?
- 22. HttpContext.Current.Cache線程安全嗎?
- 23. UIFont線程安全嗎?
- 24. C#+ =線程安全嗎?
- 25. Go bytes.Buffer線程安全嗎?
- 26. SynchronizationContext.Post()線程安全嗎?
- 27. Guava Table線程安全嗎?
- 28. EPiServer線程安全嗎?
- 29. 是java.sql.Connection線程安全嗎?
- 30. 是CreateChildContainer()線程安全嗎?
http://stackoverflow.com/a/10702210/828625 –
我不知道,我可能是錯的。但是這個http://lists.jboss.org/pipermail/netty-users/2008-November/000045.html似乎與您引用的線索中的評論相沖突。 –
同樣,你所鏈接的是同樣的東西,並回答你的問題。 Channel.write是線程安全的。 –