我想不出为什么记忆是不断增长的第二个,为什么我的CPU使用率是如此之高(90%)...
我建立一个多线程的数据库,这是实施生产者和消费者的设计模式。我拉着在股市的实时数据,我分析我的数据库中,然后更新表。下面是一个例子:我在做什么
唐纳德・艾伦:约翰・西蒙斯/取缔程序员:它采取的处理器时间,因为你的更新方法不会有任何延迟建在这之前的周期,并试图处理的下一个值。它只是不断循环。
List<threads> threads = new List<threads>();
object sync = new object();
EventWaitHandle ewh = AutoResetEvent(false);
Queue<string> q = new Queue<string>();
int numberOfThreads = Environment.ProcessorCount;
// constructor of class used for parsing data
public ParseCls()
{
MyDatabase databaseObj = new MyDatabase();
for(int i = 0; i < numberOfThreads; i++)
{
threads.Add(new Thread(prase));
threads[i].Start();
}
}
public GetData(string stockData)
{
lock(sync)
{
q.Enqueue(stockData);
}
ewh.Set();
}
private void parse()
{
string s = "";
lock(sync)
{
string s = q.Dequeue();
}
if(s.Length != "")
{
// parse string and create a new one then pass results to
// my database
string newS = s.Substring(0,1);
dataBaseObj.GetData(newS);
}
else
{
ewh.WaitOne();
s = "";
}
// here's my database class
class MyDatabase
{
int numberOfThreads = Enviorment.ProcessorCount;
List<threads> threads = new List<threads>();
EventWaitHandle ewh = AutoResetEvent(false);
object sync = new object();
Queue<string> q = new Queue<string>();
public MyDatabase()
{
for(int i = 0; i < numberOfThreads; i++)
{
threads.Add(new Thread(update));
threads[i].Start();
}
}
public void GetData(string data)
{
lock(sync)
{
q.Enqueue(data);
}
ewh.Set();
}
private void update()
{
SQLConnection conn = etc...
// other ado.net objects...
while(true)
{
lock(sync)
{
string s = q.Dequeue();
}
// update database here
// code...
if(etc...)
// blah, blah, blah
else
{
ewh.WaitOne();
}
}
}
}
它使吃,因为你永远不会出售/释放,因为他们无休止地运行创建的线程的内存