我正在尝试使用计数器来检测通过 HTTP 方法发送的文本中唯一单词的数量。由于我需要确保此计数器值的并发性,我已将其更改为AtomicInteger
.
在某些情况下,我想获取计数器的当前值并将其重置为零。如果我理解正确的话,我一定不能像这样单独使用get()
andset(0)
方法,而是使用 getAndUpdate() 方法,但是!我不知道如何为此重置实现 IntUnaryOperator,甚至不知道是否可以在 Java 6 服务器上执行此操作。
谢谢你。
public class Server {
static AtomicInteger counter = new AtomicInteger(0);
static class implements HttpHandler {
@Override
public void handle(HttpExchange spojeni) throws IOException {
counter.getAndUpdate(???);
}
}
}