我正在编写多线程程序,以舒适的格式读取和查看Apache日志文件。它可以工作,但它在单核处理器上无法正常工作。我认为错误在哪里,但我不知道我需要改变什么。假设在评论中写入了错误。多线程程序在单核处理器上无法正常工作
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();
}
}
}
你有什么证据表明它不起作用在单核处理器上? – Nilzor
'while((line = file.ReadLine())!= null)'是一个本地文件,所以绝对不是问题。 –