Ho un'app Netty, dove voglio avere più di un thread per scrivere su un canale. Mi stavo chiedendo se Channel.write è thread-safe?Il thread Netty Channel.write è sicuro?
risposta
È thread-safe, quindi non devi preoccuparti.
No, è thread-safe, perché Channel.write
chiama ChannelOutboundBuffer.addMessage
nel suo HeadContext della pipeline e ChannelOutboundBuffer.addMessage
è sicuramente thread-safe. Date un'occhiata a questo codice:
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);
}
quindi dovresti avere al massimo un thread chiamando Channel.write in qualsiasi momento. c'è qualcosa di sbagliato nel formato del codice, puoi trovarlo su http://netty.io/4.0/xref/index.html – yuguoliang
Non riesco a confermare il tuo reclamo. Vedo che 'AbstractChannel.write' chiama' Pipeline.write', che alla fine chiama 'AbstractUnsafe.write', dopo aver acquisito il ciclo degli eventi per quel canale. All'interno di questo metodo si afferma che il thread si trova all'interno del ciclo degli eventi, quindi può essere chiamato solo da 1 thread alla volta – Ferrybig
Come si può vedere dal codice, il metodo ChannelOutboundBuffer.addMessage()
in sé non è thread-safe. Tuttavia, il canale Writing è "thread safe" perché netty esegue il task/metodo write nel singolo thread I/O.
- 1. Netty channel.write not writing message
- 2. Il thread NSPersistentStoreCoordinator è sicuro?
- 3. È sicuro il thread SecurityContextHolder?
- 4. Il thread SecureRandom è sicuro?
- 5. Il thread closesocket è sicuro?
- 6. Il thread libuv è sicuro?
- 7. È sicuro il thread unique_ptr?
- 8. Il thread java.util.Hashtable è sicuro?
- 9. Il thread WCF ClientBase è sicuro?
- 10. Il thread di console.writeline è sicuro?
- 11. È sicuro il thread Java Regex?
- 12. Il thread di Windows Win32 è sicuro?
- 13. Il thread java.util.Calendar è sicuro o no?
- 14. Il thread di GHashTable (glib2) è sicuro?
- 15. È il C# '??' thread operatore sicuro?
- 16. Il thread QueueSender di JMS è sicuro?
- 17. In Delphi, il thread OutputDebugString è sicuro?
- 18. Delphi - Il thread TClientDataset è sicuro?
- 19. Il thread armadillo solve() è sicuro?
- 20. è un thread di DatagramSocket.send sicuro?
- 21. Questo thread di codice grafica è sicuro?
- 22. è thread client JAX-RS sicuro
- 23. È sicuro per più thread impostare ServicePointManager.ServerCertificateValidationCallback?
- 24. È int sicuro leggere da più thread?
- 25. Questo codice è un thread sicuro?
- 26. DWScript è sicuro per i thread?
- 27. L'incremento automatico nel thread C è sicuro?
- 28. Il thread di utilizzo del blocco è sicuro?
- 29. Il thread PrintWriter di un socket Java è sicuro?
- 30. Il thread list.h del kernel linux è sicuro?
http://stackoverflow.com/a/10702210/828625 –
Non so, potrei sbagliarmi. Ma questo http://lists.jboss.org/pipermail/netty-users/2008-November/000045.html sembra in conflitto con le osservazioni nel thread che hai citato. –
Anche in questo caso, ciò che hai collegato dice la stessa cosa e risponde alla tua domanda. Channel.write è thread-safe. –