本文介绍了什么应该是最好的方法来最小化CPU负载,其上升到70%至80%。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我已创建了示例应用程序,其中我创建了3个线程,负责将一些数据发送到端口..我创建了两个函数,一个是StartSending1和StartSending2但是如果我使用其中任何一个仍然是我的CPU最高可达70%至80%
请建议在没有Thread.Sleep()的情况下最小化CPU负载的更好方法
请检查样本
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading;
使用 System.Threading.Tasks;
使用 System.Runtime.CompilerServices;
命名空间 ConsoleApplication1
{
class 计划
{
static System.Threading.Thread sender;
静态 DataSender dataSender;
静态 void Main( string [] args)
{
dataSender = new DataSender();
var dataSender2 = new DataSender();
var dataSender3 = new DataSender();
var dataSender4 = new DataSender();
sender = new System.Threading.Thread(dataSender.StartSending2);
sender.IsBackground = true ;
sender.Start();
sender = new System.Threading.Thread(dataSender2.StartSending2);
sender.IsBackground = true ;
sender.Start();
sender = new System.Threading.Thread(dataSender3.StartSending2);
sender.IsBackground = true ;
sender.Start();
Console.ReadKey();
}
}
class SnderEvrgs:EventArgs
{
public SnderEvrgs()
{
}
}
class DataSender
{
private static string IP = 127.0.0.1;
public static int 端口= 20100 ;
static bool issending = true ;
public static event EventHandler< ; SnderEvrgs> DataSent;
public void StartSending1()
{
while (isrending)
{
System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP,Port);
byte [] arr_bData = new byte [] { 1 , 1 , 1 , 1 , 1 };
Console.WriteLine(DateTime.Now);
for ( int i = 0 ; i < 100000000 ; i ++)
{
}
}
}
静态 AutoResetEvent abc = new AutoResetEvent( false );
线程新线程;
public void StartSending2()
{
while (issending)
{
newthread = new Thread(sendData);
newthread.IsBackground = true ;
newthread.Priority = ThreadPriority.Lowest;
newthread.Start();
abc.WaitOne();
}
}
对象 abca = 新 object ();
[MethodImplAttribute(MethodImplOptions.Synchronized)]
public void sendData()
{
lock (abca)
{
System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP,Port);
byte [] arr_bData = new byte [] { 1 , 1 , 1 , 1 , 1 };
Console.WriteLine(DateTime.Now);
if (_sockMain.Send(arr_bData,arr_bData.Length)== arr_bData.Length);
{
for ( int i = 0 ; i < 1000000000 ; i ++)
{
}
abc.Set();
}
}
}
}
}
解决方案
Hello, I have created sample application in which i have created 3 threads which will be responsible to send some data to Port.. i have created two functions one is StartSending1 and StartSending2 but if I use any one of these still my CPU goes upto 70 to 80%
Please suggest what should be better way to minimize load on CPU without Thread.Sleep()
Please check sample
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Runtime.CompilerServices; namespace ConsoleApplication1 { class Program { static System.Threading.Thread sender; static DataSender dataSender; static void Main(string[] args) { dataSender = new DataSender(); var dataSender2 = new DataSender(); var dataSender3 = new DataSender(); var dataSender4 = new DataSender(); sender = new System.Threading.Thread(dataSender.StartSending2); sender.IsBackground = true; sender.Start(); sender = new System.Threading.Thread(dataSender2.StartSending2); sender.IsBackground = true; sender.Start(); sender = new System.Threading.Thread(dataSender3.StartSending2); sender.IsBackground = true; sender.Start(); Console.ReadKey(); } } class SnderEvrgs : EventArgs { public SnderEvrgs() { } } class DataSender { private static string IP = "127.0.0.1"; public static int Port = 20100; static bool issending = true; public static event EventHandler<SnderEvrgs> DataSent; public void StartSending1() { while (issending) { System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP, Port); byte[] arr_bData = new byte[] { 1, 1, 1, 1, 1 }; Console.WriteLine(DateTime.Now); for (int i = 0; i < 100000000; i++) { } } } static AutoResetEvent abc = new AutoResetEvent(false); Thread newthread; public void StartSending2() { while (issending) { newthread = new Thread(sendData); newthread.IsBackground = true; newthread.Priority = ThreadPriority.Lowest; newthread.Start(); abc.WaitOne(); } } object abca = new object(); [MethodImplAttribute(MethodImplOptions.Synchronized)] public void sendData() { lock (abca) { System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP, Port); byte[] arr_bData = new byte[] { 1, 1, 1, 1, 1 }; Console.WriteLine(DateTime.Now); if (_sockMain.Send(arr_bData, arr_bData.Length) == arr_bData.Length) ; { for (int i = 0; i < 1000000000; i++) { } abc.Set(); } } } } }
解决方案
这篇关于什么应该是最好的方法来最小化CPU负载,其上升到70%至80%。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!