首页

关于ehcache-core源码中通过java.util.concurrent.atomic.AtomicLong实现线程计数器Counter分析说明

标签:counter,线程计数器,AtomicLong,ehcache-core     发布时间:2018-03-20   

一、前言

关于ehcache-core源码包中net.sf.ehcache.util.counter.Counter、net.sf.ehcache.util.counter.CounterImpl基于java.util.concurrent.atomic.AtomicLong实现并发线程计数器,详情参考源码说明部分。

二、源码说明

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@}
<<热门下载>>