首页

关于ehcache基于java.util.concurrent.atomic.AtomicLong实现的Counter计数器源码说明

标签:计数器,ehcache,AtomicLong     发布时间:2018-04-20   

一、前言

关于ehcache源码包中net.sf.ehcache.util.counter.Counter、net.sf.ehcache.util.counterCounterImpl线程计数器定义实现类,具体参考如下说明。

二、源码说明

1.Counter计数器接口

package net.sf.ehcache.util.counter;@b@@b@public abstract interface Counter@b@{@b@  public abstract long increment();@b@@b@  public abstract long decrement();@b@@b@  public abstract long getAndSet(long paramLong);@b@@b@  public abstract long getValue();@b@@b@  public abstract long increment(long paramLong);@b@@b@  public abstract long decrement(long paramLong);@b@@b@  public abstract void setValue(long paramLong);@b@}

2.CounterImpl计数器实现类

package net.sf.ehcache.util.counter;@b@@b@import java.io.Serializable;@b@import java.util.concurrent.atomic.AtomicLong;@b@@b@public class CounterImpl@b@  implements Counter, Serializable@b@{@b@  private AtomicLong value;@b@@b@  public CounterImpl()@b@  {@b@    this(0L);@b@  }@b@@b@  public CounterImpl(long initialValue)@b@  {@b@    this.value = new AtomicLong(initialValue);@b@  }@b@@b@  public long increment()@b@  {@b@    return this.value.incrementAndGet();@b@  }@b@@b@  public long decrement()@b@  {@b@    return this.value.decrementAndGet();@b@  }@b@@b@  public long getAndSet(long newValue)@b@  {@b@    return this.value.getAndSet(newValue);@b@  }@b@@b@  public long getValue()@b@  {@b@    return this.value.get();@b@  }@b@@b@  public long increment(long amount)@b@  {@b@    return this.value.addAndGet(amount);@b@  }@b@@b@  public long decrement(long amount)@b@  {@b@    return this.value.addAndGet(amount * -1L);@b@  }@b@@b@  public void setValue(long newValue)@b@  {@b@    this.value.set(newValue);@b@  }@b@}