Động lựcTôi có thể gọi XMPPConnection.sendPacket từ các chủ đề đồng thời không?
Tôi muốn đôi mắt thêm để xác nhận rằng tôi có thể gọi phương thức này XMPPConnection.sendPacket ( Packet) đồng thời. Đối với mã hiện tại của tôi, tôi đang gọi một danh sách các Callables (tối đa 3) trong một thời trang nối tiếp. Mỗi Callable gửi/nhận các gói XMPP trên một phần của XMPPConnection. Tôi có kế hoạch song song các Callables này bằng cách quay ra nhiều chủ đề & mỗi Callable sẽ gọi sendPacket trên XMPPConnection chia sẻ mà không đồng bộ hóa.
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();
}
}
gì tôi kết luận
Kể từ khi PacketWriter đang sử dụng một BlockingQueue, không có vấn đề với ý định của tôi để gọi se ndPacket từ nhiều chủ đề. Tôi có đúng không?