我正在尝试使用线程使用API来提取一些数据,因为每个查询在Postman上完成时大约需要5秒。我试图提取大约500个查询。我有10个线程同时进行,但每个线程似乎需要大约20秒以上,我不知道为什么。它不是CPU密集型的,我无法看到瓶颈在哪里。
List<PullEncompassThread> puller = new List<PullEncompassThread>();
// class that stores two lists pipeLine is the original List loan list is what the result gets stored into
twoList lists = new twoList();
lists.guidList = pipeLine;
lists.loanList = new List<LoanInfo>();
List<Thread> pullerThreads = new List<Thread>();
int numBots = 10;
for (int i = 0; i < numBots; i++)
{
puller.Add(new PullEncompassThread(currToken, i));
}
for (int i = 0; i < numBots; i++)
{
pullerThreads.Add(new Thread(new
ParameterizedThreadStart(puller[i].ThreadProc)));
}
for (int i = 0; i < numBots; i++)
{
pullerThreads[i].Start(lists);
Console.WriteLine("Bot number " + i + " started");
}
for (int i = 0; i < numBots; i++)
{
pullerThreads[i].Join();
}
以下是该主题的代码。由于它们都共享相同的列表,因此我在访问它时锁定它只是为了防止竞争条件。是什么让一切都变慢了?
public class PullEncompassThread
{
public Token access;
public int botNum;
public PullEncompassThread(Token key, int num)
{
access = key;
botNum = num;
}
public void ThreadProc(object lists)
{
while (((twoList)lists).guidList.Count != 0)
{
GUID tempGUID;
lock (((twoList)lists).guidList)
{
tempGUID = ((twoList)lists).guidList[0];
((twoList)lists).guidList.Remove(((twoList)lists).guidList[0]);
}
//api get and store
var client = new RestClient("https://api.elliemae.com/encompass/v1/loans/" + tempGUID.loanGuid);
var request = new RestRequest(Method.GET);
request.AddHeader("postman-token", //removed for security);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "//removed for security");
IRestResponse response = client.Execute(request);
LoanInfo temp = JsonConvert.DeserializeObject<LoanInfo>(response.Content);
lock (((twoList)lists).loanList)
{
((twoList)lists).loanList.Add(temp);
}
Console.WriteLine("Bot " + botNum + " Got loan " + temp.loanIdNumber);
}
}
}