C# 多线程程序不能在一个核心处理器上正常工作
我正在编写多线程程序,以便以舒适的形式读取和查看Apache日志文件。它可以工作,但不能在单核处理器上正常工作。我假设错误在哪里,但我不知道我需要改变什么。假设错误已写入注释中C# 多线程程序不能在一个核心处理器上正常工作,c#,multithreading,C#,Multithreading,我正在编写多线程程序,以便以舒适的形式读取和查看Apache日志文件。它可以工作,但不能在单核处理器上正常工作。我假设错误在哪里,但我不知道我需要改变什么。假设错误已写入注释中 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Threading; usin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
namespace lab2Form
{
class LogStruct
{
public Dictionary<string, ulong> domainName;
public Dictionary<string, ulong> URL;
public Dictionary<string, ulong> domainData;
public Dictionary<string, ulong> errorCodes;
public LogStruct()
{
domainName = new Dictionary<string, ulong> { };
URL = new Dictionary<string, ulong> { };
domainData = new Dictionary<string, ulong> { };
errorCodes = new Dictionary<string, ulong> { };
}
}
class CLogParser
{
LogStruct m_logStruct;
public CLogParser()
{
m_logStruct = new LogStruct();
}
public void ThreadProc(object param)
{
string logName = (string)param;
StreamReader file;
try
{
file = new StreamReader(logName);
}
catch
{
return;
}
string line;
while ((line = file.ReadLine()) != null)//may be,something wrong here
{
var space_pos = line.IndexOf(' ');
if (space_pos > 0)
{
string[] parameters = line.Split(new Char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);
string domainName = parameters[0];
bool isMainPage = (parameters[4] == "\"-\"") ? true : false;
string relativePageAddress = (isMainPage) ? "/" : parameters[5];
Regex reg = new Regex(" \\d+");
MatchCollection matches = reg.Matches(line);
string errorCode = matches[1].Value;
ulong pageSize = (matches.Count > 2) ? Convert.ToUInt64(matches[2].Value) : 0;
string fullAdress = domainName + relativePageAddress;
string fullErrCode = domainName + errorCode;
if (m_logStruct.domainName.ContainsKey(domainName))
{
lock (m_logStruct.domainName)
{
m_logStruct.domainName[domainName]++;
}
lock (m_logStruct.domainData)
{
m_logStruct.domainData[domainName] += pageSize;
}
if (m_logStruct.URL.ContainsKey(fullAdress))
{
lock (m_logStruct.URL)
{
m_logStruct.URL[fullAdress]++;
}
}
else
{
lock (m_logStruct.URL)
{
m_logStruct.URL.Add(fullAdress, 1);
}
}
if (m_logStruct.errorCodes.ContainsKey(fullErrCode))
{
lock (m_logStruct.errorCodes)
{
m_logStruct.errorCodes[fullErrCode]++;
}
}
else
{
lock (m_logStruct.errorCodes)
{
m_logStruct.errorCodes.Add(fullErrCode, 1);
}
}
}
else
{
lock (m_logStruct.domainName)
{
m_logStruct.domainName.Add(domainName, 1);
}
lock (m_logStruct.URL)
{
m_logStruct.domainData.Add(domainName, pageSize);
}
lock (m_logStruct.domainData)
{
m_logStruct.URL.Add(fullAdress, 1);
}
lock (m_logStruct.errorCodes)
{
m_logStruct.errorCodes.Add(fullErrCode, 1);
}
}
}
}
}
public void ShowData(ref DataGridView dmRequests, ref DataGridView URL, ref DataGridView dmData, ref DataGridView errorCodes)
{
List<KeyValuePair<string, ulong>> dmReqList = new List<KeyValuePair<string, ulong>>();
List<KeyValuePair<string, ulong>> urlReqList = new List<KeyValuePair<string, ulong>>();
List<KeyValuePair<string, ulong>> dmDataList = new List<KeyValuePair<string, ulong>>();
List<KeyValuePair<string, ulong>> errCodesList = new List<KeyValuePair<string, ulong>>();
lock (m_logStruct.domainName)`enter code here`
{
dmReqList = m_logStruct.domainName.ToList();
}
lock(m_logStruct.URL)
{
urlReqList = m_logStruct.URL.ToList();
}
lock(m_logStruct.domainData)
{
dmDataList = m_logStruct.domainData.ToList();
}
lock(m_logStruct.errorCodes)
{
errCodesList = m_logStruct.errorCodes.ToList();
}
dmRequests.DataSource = dmReqList.OrderBy(x => x.Key).ToList();
URL.DataSource = urlReqList.OrderBy(x => x.Key).ToList();
dmData.DataSource = dmDataList.OrderBy(x => x.Key).ToList();
errorCodes.DataSource = errCodesList.OrderBy(x => x.Key).ToList();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
使用系统线程;
使用System.Windows.Forms;
使用系统数据;
使用System.Text.RegularExpressions;
名称空间lab2Form
{
类LogStruct
{
公共词典域名;
公共词典网址;
公共词典领域数据;
公共字典错误代码;
公共LogStruct()
{
域名=新字典{};
URL=新字典{};
domainData=新字典{};
errorCodes=新字典{};
}
}
类CLogParser
{
LogStruct m_LogStruct;
公共CLogParser()
{
m_logStruct=新的logStruct();
}
public void ThreadProc(对象参数)
{
字符串logName=(字符串)参数;
流阅读器文件;
尝试
{
文件=新的StreamReader(日志名);
}
抓住
{
返回;
}
弦线;
虽然((line=file.ReadLine())!=null)//可能是,但这里有问题
{
var space_pos=行索引(“”);
如果(空间位置>0)
{
string[]参数=line.Split(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);
字符串domainName=参数[0];
bool isMainPage=(参数[4]=“\”-\”)?true:false;
字符串RelativeAddress=(isMainPage)?“/”:参数[5];
正则表达式reg=新正则表达式(\\d+);
MatchCollection matches=注册匹配项(行);
字符串errorCode=匹配[1]。值;
ulong pageSize=(matches.Count>2)?Convert.ToUInt64(匹配[2]。值):0;
字符串fullAddress=域名+相对地址;
字符串FullerCode=domainName+errorCode;
if(m_logStruct.domainName.ContainsKey(domainName))
{
锁(m_logStruct.domainName)
{
m_logStruct.domainName[domainName]++;
}
锁(m_logStruct.domainData)
{
m_logStruct.domainData[domainName]+=pageSize;
}
if(m_logStruct.URL.ContainsKey(fullAddress))
{
锁(m_logStruct.URL)
{
m_logStruct.URL[fullAddress]++;
}
}
其他的
{
锁(m_logStruct.URL)
{
m_logStruct.URL.Add(FullAddress,1);
}
}
if(m_logStruct.errorCodes.ContainsKey(FullerCode))
{
锁(m_logStruct.errorCodes)
{
m_logStruct.errorCodes[FullerCode]++;
}
}
其他的
{
锁(m_logStruct.errorCodes)
{
m_logStruct.errorCodes.Add(FullerCode,1);
}
}
}
其他的
{
锁(m_logStruct.domainName)
{
m_logStruct.domainName.Add(domainName,1);
}
锁(m_logStruct.URL)
{
m_logStruct.domainData.Add(域名,页面大小);
}
锁(m_logStruct.domainData)
{
m_logStruct.URL.Add(FullAddress,1);
}
锁(m_logStruct.errorCodes)
{
m_logStruct.errorCodes.Add(FullerCode,1);
}
}
}
}
}
public void ShowData(ref DataGridView dmRequests、ref DataGridView URL、ref DataGridView dmData、ref DataGridView errorcode)
{
List dmReqList=新列表();
List urlReqList=新列表();
List dmDataList=新列表();
List errCodesList=新列表();
lock(m_logStruct.domainName)`在这里输入代码`
{
dmReqList=m_logStruct.domainName.ToList();
}
锁(m_logStruct.URL)
{
urlReqList=m_logStruct.URL.ToList();
}
锁(m_logStruct.domainData)
{
dmDataList=m_logStruct.domainData.ToList();
}
锁(m_logStruct.errorCodes)
{
errCodesList=m_logStruct.errorCodes.ToList();
}
dmRequests.DataSource=dmReqList.OrderBy(x=>x.Key).ToList();
URL.DataSource=urlReqList.OrderBy(x=>x.Key).ToList();
dmData.DataSource=dmDataList.OrderBy(x=>x.Key).ToList();
errorCodes.DataSource=errCodesList.OrderBy(x=>x.Key).ToList(
if (m_logStruct.URL.ContainsKey(fullAdress)) //unsynchronized read
class LogStruct
{
public ConcurrentDictionary<string, ulong> domainName;
public ConcurrentDictionary<string, ulong> URL;
public ConcurrentDictionary<string, ulong> domainData;
public ConcurrentDictionary<string, ulong> errorCodes;
public LogStruct()
{
domainName = new ConcurrentDictionary<string, ulong> { };
URL = new ConcurrentDictionary<string, ulong> { };
domainData = new ConcurrentDictionary<string, ulong> { };
errorCodes = new ConcurrentDictionary<string, ulong> { };
}
}
class CLogParser
{
LogStruct m_logStruct;
public CLogParser()
{
m_logStruct = new LogStruct();
}
public void ThreadProc(object param)
{
string logName = (string)param;
StreamReader file;
try
{
file = new StreamReader(logName);
}
catch
{
return;
}
string line;
while ((line = file.ReadLine()) != null)//may be,something wrong here
{
var space_pos = line.IndexOf(' ');
if (space_pos > 0)
{
string[] parameters = line.Split(new Char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);
string domainName = parameters[0];
bool isMainPage = (parameters[4] == "\"-\"") ? true : false;
string relativePageAddress = (isMainPage) ? "/" : parameters[5];
Regex reg = new Regex(" \\d+");
MatchCollection matches = reg.Matches(line);
string errorCode = matches[1].Value;
ulong pageSize = (matches.Count > 2) ? Convert.ToUInt64(matches[2].Value) : 0;
string fullAdress = domainName + relativePageAddress;
string fullErrCode = domainName + errorCode;
if (m_logStruct.domainName.ContainsKey(domainName))
{
m_logStruct.domainName[domainName]++;
m_logStruct.domainData[domainName] += pageSize;
m_logStruct.URL.AddOrUpdate(fullAdress, 1, (key, oldVal) =>
{
m_logStruct.URL[fullAdress]++;
return m_logStruct.URL[fullAdress];
});
m_logStruct.errorCodes.AddOrUpdate(fullErrCode, 1, (key, oldVal) =>
{
m_logStruct.errorCodes[fullErrCode]++;
return m_logStruct.errorCodes[fullErrCode];
});
}
else
{
m_logStruct.domainName.AddOrUpdate(domainName, 1, ShallNeverHappen);
m_logStruct.domainData.AddOrUpdate(domainName, pageSize, ShallNeverHappen);
m_logStruct.URL.AddOrUpdate(fullAdress, 1, ShallNeverHappen);
m_logStruct.errorCodes.AddOrUpdate(fullErrCode, 1, ShallNeverHappen);
}
}
}
}
public ulong ShallNeverHappen(String key, ulong existingVal)
{
throw new InvalidOperationException("This method is not expected to be called");
}
}