MotywacjaCzy mogę wywołać XMPPConnection.sendPacket z równoczesnych wątków?
Chcę dodatkowe oczy, aby potwierdzić, że jestem w stanie wywołać tę metodę XMPPConnection.sendPacket ( Packet) jednocześnie. Dla mojego obecnego kodu, przywołuję Listę kalendarzy (maks. 3) w sposób seryjny. Każdy Callable wysyła/odbiera pakiety XMPP na jednym kawałku XMPPConnection. Planuję zrównoleglować te Callables poprzez wyrzucenie wielu wątków. & Każdy Callable będzie wywoływał sendPacket na wspólnym XMPPConnection bez synchronizacji.
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();
}
}
Co Kończę
Ponieważ PacketWriter stosuje BlockingQueue, nie ma żadnego problemu z moim zamiarem powołania se ndPacket z wielu wątków. Mam rację ?