Fix sendNumbers logic

This commit is contained in:
kxbmap 2013-07-16 06:00:49 +09:00 committed by Norman Maurer
parent b8200d975c
commit ed72925547

View File

@ -40,6 +40,7 @@ public class FactorialClientHandler extends SimpleChannelInboundHandler<BigInteg
private ChannelHandlerContext ctx;
private int receivedMessages;
private int next = 1;
private final int count;
final BlockingQueue<BigInteger> answer = new LinkedBlockingQueue<BigInteger>();
@ -94,16 +95,14 @@ public class FactorialClientHandler extends SimpleChannelInboundHandler<BigInteg
private void sendNumbers() {
// Do not send more than 4096 numbers.
for (int i = 0; i < 4096; i++) {
if (i <= count) {
ChannelFuture future = ctx.write(Integer.valueOf(i));
if (count == i) {
future.addListener(numberSender);
}
i ++;
} else {
break;
}
ChannelFuture future = null;
for (int i = 0; i < 4096 && next <= count; i++) {
future = ctx.write(Integer.valueOf(next));
next++;
}
if (next <= count) {
assert future != null;
future.addListener(numberSender);
}
ctx.flush();
}