motivazionePosso richiamare XMPPConnection.sendPacket da thread simultanei?
voglio occhi in più per confermare che sono in grado di chiamare questo metodo XMPPConnection.sendPacket ( Packet) contemporaneamente. Per il mio codice attuale, sto invocando una lista di Callables (max 3) in modo seriale. Ogni Callable invia/riceve pacchetti XMPP su un pezzo di XMPPConnection. Ho intenzione di parallelizzare questi Callables ruotando più thread & ogni Callable invocherà sendPacket sul XMPPConnection condiviso senza sincronizzazione.
XMPPConnection
class XMPPConnection
{
private boolean connected = false;
public boolean isConnected()
{
return connected;
}
PacketWriter packetWriter;
public void sendPacket(Packet packet)
{
if (!isConnected())
throw new IllegalStateException("Not connected to server.");
if (packet == null)
throw new NullPointerException("Packet is null.");
packetWriter.sendPacket(packet);
}
}
PacketWriter
class PacketWriter
{
public void sendPacket(Packet packet)
{
if (!done) {
// Invoke interceptors for the new packet
// that is about to be sent. Interceptors
// may modify the content of the packet.
processInterceptors(packet);
try {
queue.put(packet);
}
catch (InterruptedException ie) {
ie.printStackTrace();
return;
}
synchronized (queue) {
queue.notifyAll();
}
// Process packet writer listeners. Note that we're
// using the sending thread so it's expected that
// listeners are fast.
processListeners(packet);
}
protected PacketWriter(XMPPConnection connection)
{
this.queue = new ArrayBlockingQueue<Packet>(500, true);
this.connection = connection;
init();
}
}
Cosa concludo
Dal momento che la PacketWriter sta usando una BlockingQueue, non c'è nessun problema con la mia intenzione di invocare SE ndPacket da più thread. Ho ragione ?