動機我可以從併發線程調用XMPPConnection.sendPacket嗎?
我想額外的眼睛,以確認我能夠調用此方法XMPPConnection.sendPacket( 包)兼任。對於我當前的代碼,我以串行方式調用可調參數列表(最多3個)。每個Callable在一個XMPPConnection上發送/接收XMPP數據包。我打算通過分離多個線程來並行化這些Callables,每個Callable將在沒有同步的情況下調用共享XMPPConnection上的sendPacket。
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();
}
}
我能得出什麼結論
由於PacketWriter使用的BlockingQueue,還有把我的意圖來調用沒問題SE ndPacket來自多個線程。我對麼 ?