我希望这个程序中的线程在锁步中工作。但是,发送者在等待接收者赶上之前发送了大约 60000 条消息。我对 HWM 有什么误解?
如果我不启动接收者线程,则发送者在尝试发送第二条消息时会阻塞。
public static void main(String[] args) throws Exception {
new Thread() {
@Override
public void run() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.XREP);
socket.setHWM(1);
socket.bind("tcp://127.0.0.1:8080");
while (true) {
try {
socket.recv(0);
byte[] msg = socket.recv(0);
System.out.println("Received: " + new String(msg, "UTF-8"));
Thread.sleep(1000);
} catch (Exception ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}.start();
new Thread() {
@Override
public void run() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.XREQ);
socket.setHWM(1);
socket.connect("tcp://127.0.0.1:8080");
Integer i = 1;
while (true) {
System.out.println("Sending: " + i);
socket.send(i.toString().getBytes(), 0);
i++;
}
}
}.start();
}