我这个简单类是否是简单线程安全的,在多线程访问时是否有阻塞?
public sealed class ReceiverQueue
{
private static int userId;
private static int minimum;
private static int maximum;
private static readonly object padlock = new object();
public ReceiverQueue()
{
//
}
public static int Minimum
{
set
{
minimum = value;
}
get
{
return minimum;
}
}
public static int Maximun
{
set
{
maximum = value;
}
get
{
return maximum;
}
}
public static int UserId
{
set
{
userId = value;
}
get
{
lock ( padlock )
{
if ( userId == minimum )
{
return userId;
}
else
{
if ( userId > maximum )
{
return 0;
}
else
{
userId ++;
return userId;
}
}
}
}
}
多线程部分会调用ReceiverQueue.UserId取得UserId,直到发现UserId = 0,谢谢。
------解决方案--------------------好像是没有问题~
------解决方案--------------------用了lock ,好像不会有问题了。