static的不恰当应用
最近总是和static闹别扭,看下面这个例子(取自我客户的一个例子)
using System;
using System.Threading;
![](/Images/OutliningIndicators/None.gif)
namespace StaticTest
{
class Program
{
static void Main(string[] args)
{
(new Thread(new ThreadStart(Database.RunProcedure))).Start();
(new Thread(new ThreadStart(Database.RunProcedure2))).Start();
}
}
public class Database
{
private static int conn = 0;
![](/Images/OutliningIndicators/InBlock.gif)
private static void Open()
{
conn = 1;
}
![](/Images/OutliningIndicators/InBlock.gif)
private static void Close()
{
conn = 2;
}
![](/Images/OutliningIndicators/InBlock.gif)
public static void RunProcedure()
{
while (true)
{
Open();
Console.WriteLine("Proc1 Open:" + conn);
Close();
Console.WriteLine("Proc1 Close:" + conn);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public static void RunProcedure2()
{
while (true)
{
Open();
Console.WriteLine("Proc2 Open:" + conn);
Close();
Console.WriteLine("Proc2 Close:" + conn);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
在命令行下面跑这个程序,假如叫做test.exe,那么你用test.exe>1.txt,等10-20秒,用一个好用的editor打开1.txt,查找Close:1或者Open:2。大部分时候很幸运,能找到这个本不应该出现的字符串,为什么?
设想conn是IDBConnection, comm是IDBCommand,那么你也许会想起来,在偶然的情况下,客户那里的代码
1. 数据库链接明明是Open的,但是偶然的情况下,会提示已经Close
2. 相同sp,不同的参数,偶然的情况下,会让客户的数据乱掉。如制单人明明是张三,偶尔的情况下会变成李四。
这个代码不难,按照singleton模式,加一个lock,两层的null判断就可以了。但是针对这个database,我的经验更倾向于使用instance,而不是static。static如果不小心,就会产生莫名其妙的错误。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
在命令行下面跑这个程序,假如叫做test.exe,那么你用test.exe>1.txt,等10-20秒,用一个好用的editor打开1.txt,查找Close:1或者Open:2。大部分时候很幸运,能找到这个本不应该出现的字符串,为什么?
设想conn是IDBConnection, comm是IDBCommand,那么你也许会想起来,在偶然的情况下,客户那里的代码
1. 数据库链接明明是Open的,但是偶然的情况下,会提示已经Close
2. 相同sp,不同的参数,偶然的情况下,会让客户的数据乱掉。如制单人明明是张三,偶尔的情况下会变成李四。
这个代码不难,按照singleton模式,加一个lock,两层的null判断就可以了。但是针对这个database,我的经验更倾向于使用instance,而不是static。static如果不小心,就会产生莫名其妙的错误。