我的代码如下所示:
public void handleRequests() {
ZMQ.Poller items = new ZMQ.Poller(1);
items.register(clientEndpoint, ZMQ.Poller.POLLIN);
while (!Thread.currentThread().isInterrupted()) {
byte[] message;
items.poll(); // this is the line that throws exception.
if (items.pollin(0)) {
message = clientEndpoint.recv(0);
}
}
}
当我直接调用它时它工作正常:
foo.handleRequests();
但如果它在新线程中运行,它会经常因断言错误而失败:
final Runnable listener = worldviewServer::handleRequests;
Executors.newSingleThreadExecutor().execute(listener);
我得到的堆栈跟踪如下所示:
Exception in thread "pool-6-thread-1" java.lang.AssertionError
at zmq.Mailbox.recv(Mailbox.java:113)
at zmq.SocketBase.process_commands(SocketBase.java:820)
at zmq.SocketBase.getsockopt(SocketBase.java:258)
at zmq.PollItem.readyOps(PollItem.java:107)
at zmq.ZMQ.zmq_poll(ZMQ.java:708)
at zmq.ZMQ.zmq_poll(ZMQ.java:600)
at org.zeromq.ZMQ$Poller.poll(ZMQ.java:1618)
at org.zeromq.ZMQ$Poller.poll(ZMQ.java:1592)
at com.tracelink.worldview.server.Head.handleRequests(Head.java:68)
at com.tracelink.worldview.server.WorldviewServer.handleRequests(WorldviewServer.java:236)
at com.tracelink.worldview.server.fsm.EnablingAction$$Lambda$12/404648734.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我正在使用带有 JeroMQ 0.3.5-SNAPSHOT 的 Java 8