Java 6上的AtomicInteger getAndUpdate为零

Java 6上的AtomicInteger getAndUpdate为零,java,java-6,atomicinteger,Java,Java 6,Atomicinteger,我试图使用计数器来检测通过HTTP方法发送的文本中的唯一单词数。由于需要确保此计数器值的并发性,我将其更改为AtomicInteger 在某些情况下,我希望得到计数器的当前值并将其重置为零。如果我理解正确,我不能像这样单独使用get()和set(0)方法,而是使用getAndUpdate()方法,但是!我不知道如何实现IntUnaryOperator进行此重置,以及是否可以在Java 6服务器上实现此重置。 多谢各位 public class Server { static AtomicI

我试图使用计数器来检测通过HTTP方法发送的文本中的唯一单词数。由于需要确保此计数器值的并发性,我将其更改为
AtomicInteger

在某些情况下,我希望得到计数器的当前值并将其重置为零。如果我理解正确,我不能像这样单独使用
get()
set(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(???); 
    }
  }
}

您可以使用
getAndSet(intnewvalue)
方法

/**
     * Atomically sets to the given value and returns the old value.
     *
     * @param newValue the new value
     * @return the previous value
     */
    public final int getAndSet(int newValue) {
        for (;;) {
            int current = get();
            if (compareAndSet(current, newValue))
                return current;
        }
    }
或者,您可以使用与初始值相关的
compareAndSet(int expect,int update)
,并希望更新新值

/**
     * Atomically sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     *
     * @param expect the expected value
     * @param update the new value
     * @return true if successful. False return indicates that
     * the actual value was not equal to the expected value.
     */
    public final boolean compareAndSet(int expect, int update) {
        return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
    }`

您可以使用
getAndSet(intnewvalue)
方法

/**
     * Atomically sets to the given value and returns the old value.
     *
     * @param newValue the new value
     * @return the previous value
     */
    public final int getAndSet(int newValue) {
        for (;;) {
            int current = get();
            if (compareAndSet(current, newValue))
                return current;
        }
    }
或者,您可以使用与初始值相关的
compareAndSet(int expect,int update)
,并希望更新新值

/**
     * Atomically sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     *
     * @param expect the expected value
     * @param update the new value
     * @return true if successful. False return indicates that
     * the actual value was not equal to the expected value.
     */
    public final boolean compareAndSet(int expect, int update) {
        return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
    }`
getAndUpdate()
基本上适用于基于涉及上一个值的某些操作(例如,将值加倍)设置值的情况。如果要更新的值始终为零,则更明显的选择是
getAndSet()
。Java 6中提供了
getAndSet()
操作,因此可以在这里解决您的问题

由于Java 8之前没有基于lamba的原子更新操作,如果您想在那里实现类似的功能,您需要处理自己的同步或使用
compareAndSet()
方法,可能会重试直到成功。

getAndUpdate()
基本上适用于根据涉及上一个值的某些操作(例如,将值加倍)设置值的情况。如果要更新的值始终为零,则更明显的选择是
getAndSet()
。Java 6中提供了
getAndSet()
操作,因此可以在这里解决您的问题


由于Java 8之前没有基于lamba的原子更新操作,如果您想在Java 8之前实现类似的操作,您需要处理自己的同步,或者使用
compareAndSet()
方法,可能会重试直到成功。

只需使用(而不是getAndUpdate)。(但为了更直接地回答您的问题:lambda应该是
oldValue->0
。也就是说,“一个接受名为oldValue的int的lambda,忽略它,只返回0。”)要以原子方式重置值,您可以使用AtomicInteger
getAndSet(重置值)
另外,getAndUpdate在1.8中是新的,所以我不知道你为什么特别标记了
java-6
?@yshavit你读了整个问题了吗?:)啊,好吧,这很公平。作为将来的参考,Javadocs包含一个“since”标记,JDK类能够很好地保持它的最新状态。在本例中,如果您查看文档,您会看到它写着“自1.8”。一个不太优雅的技巧(对于文档不太严谨的第三方库)是只查看旧版本的文档。在本例中,AtomicInteger的Java 7文档没有updateAndGet。只需使用(而不是getAndUpdate)。(但为了更直接地回答您的问题:lambda应该是
oldValue->0
。也就是说,“一个接受名为oldValue的int的lambda,忽略它,只返回0。”)要以原子方式重置值,您可以使用AtomicInteger
getAndSet(重置值)
另外,getAndUpdate在1.8中是新的,所以我不知道你为什么特别标记了
java-6
?@yshavit你读了整个问题了吗?:)啊,好吧,这很公平。作为将来的参考,Javadocs包含一个“since”标记,JDK类能够很好地保持它的最新状态。在本例中,如果您查看文档,您会看到它写着“自1.8”。一个不太优雅的技巧(对于文档不太严谨的第三方库)是只查看旧版本的文档。在本例中,AtomicInteger的Java 7文档没有updateAndGet。