对着java并发包写.net并发包之原子类型实现

时间:2023-12-30 12:12:44

众所周知,java1.5并发包通过volatile+CAS原理提供了优雅的并发支持。今天仔细想想.net也有volatile关键字保证内存的可见性,同时也有Interlocked提供了CAS的API,因此突发奇想——对着java并发包写一下.net并发包。第一步就是原子类型的实现(.NET目前还没有原子类型)

项目地址:https://github.com/FanHuaRan/Dotnet.Concurrent

一.原子int

/// <summary>
/// 原子int 基于CAS+自旋无锁化编程实现,核心Interlocked
/// 2017/10/18 fhr
/// </summary>
public class AtomicInteger
{
/// <summary>
/// 缺省默认值
/// </summary>
private static readonly int DEFAULT_INITAL_VALUE=;
/// <summary>
/// 被包装的int值
/// </summary>
private volatile int value;
/// <summary>
/// 比较并设置新值 成功返回true 失败返回false
/// </summary>
/// <param name="expect"></param>
/// <param name="update"></param>
/// <returns></returns>
public bool CompareAndSet(int expect, int update)
{
return expect == Interlocked.CompareExchange(ref value, update, expect);
}
/// <summary>
/// 设置新值,返回旧值
/// </summary>
/// <param name="newValue"></param>
/// <returns></returns>
public int GetAndSet(int newValue)
{
return Interlocked.Exchange(ref value, newValue);
}
/// <summary>
/// 自增1,返回新值
/// </summary>
/// <returns></returns>
public int IncrementAndGet()
{
return Interlocked.Increment(ref value);
}
/// <summary>
/// 自增1,返回旧值
/// </summary>
/// <returns></returns>
public int GetAndIncrement()
{
return Interlocked.Increment(ref value)-;
}
/// <summary>
/// 自减一,返回新值
/// </summary>
/// <returns></returns>
public int DecrementAndGet()
{
return Interlocked.Decrement(ref value);
}
/// <summary>
/// 自减一,返回旧值
/// </summary>
/// <returns></returns>
public int GetAndDecrement()
{
return Interlocked.Decrement(ref value)+;
}
/// <summary>
/// 加上add,返回旧值
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public int GetAndAdd(int add)
{
for (; ; )
{
int current = value;
int next=current+add;
if (CompareAndSet(current,next))
{
return current;
}
}
}
/// <summary>
/// 加上add,返回新值
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public int AddAndGet(int add)
{
for (; ; )
{
int current = value;
int next = current + add;
if (CompareAndSet(current, next))
{
return current;
}
}
} public AtomicInteger(int inital)
{
this.value = inital;
} public AtomicInteger()
: this(DEFAULT_INITAL_VALUE)
{ }
/// <summary>
/// value getter&setter
/// </summary>
public int Value
{
get { return value; }
set { this.value = value; }
}
/// <summary>
/// 重写hashcode value相关
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return value;
}
/// <summary>
/// 重写equals value相关
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj is AtomicInteger&&obj!=null)
{
AtomicInteger atoObj = obj as AtomicInteger;
if (atoObj.Value == Value)
{
return true;
}
}
return false;
}
/// <summary>
/// toString
/// </summary>
/// <returns></returns>
public override string ToString()
{
return value.ToString();
}
}

二.原子long

/// <summary>
/// 原子long 基于CAS+自旋无锁化编程实现,核心Interlocked
/// 2017/10/18 fhr
/// </summary>
public class AtomicLong
{
/// <summary>
/// 缺省默认值
/// </summary>
private static readonly long DEFAULT_LONG_VALUE=;
/// <summary>
/// 被包装的long值
/// </summary>
private volatile long value;
/// <summary>
/// 比较并设置新值 成功返回true 失败返回false
/// </summary>
/// <param name="expect"></param>
/// <param name="update"></param>
/// <returns></returns>
public bool CompareAndSet(long expect, long update)
{
return expect == Interlocked.CompareExchange(ref value, update, expect);
}
/// <summary>
/// 设置新值,返回旧值
/// </summary>
/// <param name="newValue"></param>
/// <returns></returns>
public long GetAndSet(long newValue)
{
return Interlocked.Exchange(ref value, newValue);
}
/// <summary>
/// 自增1,返回新值
/// </summary>
/// <returns></returns>
public long IncrementAndGet()
{
return Interlocked.Increment(ref value);
}
/// <summary>
/// 自增1,返回旧值
/// </summary>
/// <returns></returns>
public long GetAndIncrement()
{
return Interlocked.Increment(ref value)-;
}
/// <summary>
/// 自减一,返回新值
/// </summary>
/// <returns></returns>
public long DecrementAndGet()
{
return Interlocked.Decrement(ref value);
}
/// <summary>
/// 自减一,返回旧值
/// </summary>
/// <returns></returns>
public long GetAndDecrement()
{
return Interlocked.Decrement(ref value)+;
}
/// <summary>
/// 加上add,返回旧值
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public long GetAndAdd(long add)
{
for (; ; )
{
long current = value;
long next=current+add;
if (CompareAndSet(current,next))
{
return current;
}
}
}
/// <summary>
/// 加上add,返回新值
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public long AddAndGet(long add)
{
for (; ; )
{
long current = value;
long next = current + add;
if (CompareAndSet(current, next))
{
return current;
}
}
} public AtomicLong(long inital)
{
this.value = inital;
} public AtomicLong()
: this(DEFAULT_LONG_VALUE)
{ }
/// <summary>
/// value getter&setter
/// </summary>
public long Value
{
get { return value; }
set { this.value = value; }
}
/// <summary>
/// 重写hashcode value相关
/// </summary>
/// <returns></returns>
public override long GetHashCode()
{
return value;
}
/// <summary>
/// 重写equals value相关
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj is AtomicLong&&obj!=null)
{
AtomicLong atoObj = obj as AtomicLong;
if (atoObj.Value == Value)
{
return true;
}
}
return false;
}
/// <summary>
/// toString
/// </summary>
/// <returns></returns>
public override string ToString()
{
return value.ToString();
}
}

三.原子bool

    /// <summary>
/// 原子Boolean 基于CAS+自旋无锁化编程实现,核心Interlocked
/// 2017/10/18 fhr
/// </summary>
public class AtomicBoolean
{
private static readonly int TRUE_INT = ; private static readonly int FALSE_INT = ; /// <summary>
/// 被包装的boolean值 int表示 0为false 1为真
/// </summary>
private volatile int value; /// <summary>
/// 比较并设置新值 成功返回true 失败返回false
/// </summary>
/// <param name="expect"></param>
/// <param name="update"></param>
/// <returns></returns>
public bool CompareAndSet(bool expect, bool update)
{
int e = expect ? TRUE_INT : FALSE_INT;
int u = update ? TRUE_INT : FALSE_INT;
return e == Interlocked.CompareExchange(ref value, u, e);
} /// <summary>
/// 设置新值,返回旧值
/// </summary>
/// <param name="newValue"></param>
/// <returns></returns>
public bool GetAndSet(bool newValue)
{
int n = newValue ? TRUE_INT : FALSE_INT;
return Interlocked.Exchange(ref value, n) == TRUE_INT;
} public AtomicBoolean(bool inital)
{
Value = inital;
} public AtomicBoolean()
: this(false)
{ }
/// <summary>
/// value getter&setter
/// </summary>
public bool Value
{
get { return value==TRUE_INT; }
set
{
this.value = value==true?TRUE_INT:FALSE_INT;
}
}
/// <summary>
/// 重写hashcode value相关
/// </summary>
/// <returns></returns>
public override long GetHashCode()
{
return value.GetHashCode();
}
/// <summary>
/// 重写equals value相关
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj is AtomicBoolean && obj != null)
{
AtomicBoolean atoObj = obj as AtomicBoolean;
if (atoObj.Value == Value)
{
return true;
}
}
return false;
}
/// <summary>
/// toString
/// </summary>
/// <returns></returns>
public override string ToString()
{
return value.ToString();
}
}

剩下完成原子引用+原子数组+AQS及同步器

有志同道合的朋友一起参与哦

github:https://github.com/FanHuaRan/Dotnet.Concurrent