我需要測試客戶端服務器應用程序。我有服務器運行在某個端口上,需要創建大約2000個連接服務器的客戶端。對於這個我嘗試使用下面的代碼創建線程時系統內存不足
class Program
{
/// <summary>
/// Sample client code that makes gRPC calls to the server.
/// </summary>
public static void Main(string[] args)
{
for (int i = 0; i <= 2000; i++)
{
CalThread cThread = new CalThread(i);
} // Exception Raised Here
}
}
class CalThread
{
public CalThread(int clientID)
{
Thread thread = new Thread(() => getDataFromServer(clientID));
thread.Start();
}
public void getDataFromServer(int clientID)
{
try
{
//Channel channel = new Channel("192.168.0.123:50051", ChannelCredentials.Insecure);
while (true)
{
//Some code to connect to server and fetch data
Thread.Sleep(15000);
}
}
catch (Exception ex)
{
Thread.Sleep(15000);
Console.WriteLine(ex.Message);
}
}
}
這裏例外發生在System.OutOfmemory
for loop of Main method
但是我已經檢查應用程序消耗時此異常只籌集到110 MB
內存中創建在C#應用程序2000多個線程?
爲什麼c#不讓我在數字中創建線程..? 我也試過Thread Pool
但不能正常工作...
我可以使用'ThreadPool' –