I have multiple threads that send the same packet and want all of them to finish before continuing to send the next packet.
I tested out CyclicBarrier with the following code: Test Code (was too long/messy to embed) This worked as expected printing a combination of 0 - 4 then done in a loop
For the packet sender, the code is equivalent except the for loop is:
for(RSocket sendSocket : sendSocketList){
new Thread(new Send(sendSocket , toServer)).start();
}
barrier.await();
And Send is:
private class Send implements Runnable{
private RSocket sendSocket;
private MPacket packet;
public Send(RSocket sendSocket, MPacket packet){
this.sendSocket = sendSocket;
this.packet = packet;
}
public void run(){
sendSocket.send(packet);
try {
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
RSocket.send(packet); functions the same way as a regular socket's writeObject() function, except it blocks until completion (when it receives an ACK). Assuming the networking aspects are correct, am I using CyclicBarrier correctly?
P.S. I should point out if I use a for loop without starting new threads it works as intended i.e.:
for(RSocket sendSocket : sendSocketList){
sendSocket.send(toServer);
}