近期会分享自己学习c#异步编程系列的文章,如果能帮助大家希望多多关注文章末尾的微信公众号和知乎三连。各位举手之劳是对我更新技术文章最大的支持。
示例代码如下:
static void Main(string[] args)
{
Thread t = new Thread(PrintValue);
t.Start();
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("main thread.");
}
}
static void PrintValue()
{
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("thread t.");
}
}
效果:
线程属性:
static void Main(string[] args)
{
Thread.CurrentThread.Name = "Main_Thread";
Thread t = new Thread(PrintValue);
t.Name = "T_Thread";
t.Start();
Console.WriteLine(Thread.CurrentThread.Name);
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("main thread.");
}
}
static void PrintValue()
{
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("thread t.");
}
}
Join();
示例代码如下:
static void Main(string[] args)
{
#region Join and Sleep
Thread t = new Thread(PrintValue);
t.Start();
t.Join();//等待PrintValue执行完成
Console.WriteLine("End.");
#endregion
}
static void PrintValue()
{
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("thread t.");
}
}
static Thread t1, t2;
static void Main(string[] args)
{
t1 = new Thread(ThreadTest);
t1.Name = "t1";
t1.Start();
t2 = new Thread(ThreadTest);
t2.Name = "t2";
t2.Start();
}
private static void ThreadTest()
{
Console.WriteLine("Current thread:{0}",Thread.CurrentThread.Name);
if (Thread.CurrentThread.Name == "t1" && t2.ThreadState != ThreadState.Unstarted)
{
if (t2.Join(1000))
{
Console.WriteLine("t2 join 2000ms.");
}
else
{
Console.WriteLine("the time out.");
}
}
Thread.Sleep(4000);
Console.WriteLine("thread 1:{0}", t1.ThreadState);
Console.WriteLine("thread 2:{0}", t2.ThreadState);
}
效果:
Sleep()
Thread.Sleep()方法会暂停当前的线程,并等一段时间。期间是不占用cpu资源的。
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 10000; j++)
{
Console.WriteLine("i");
}
Thread.Sleep(4000);
}
验证:在每次i循环的时候会占用CPU输出10000次,然后休息4秒再继续循环。这时候分析图如下:
前台线程:
Thread t1 = new Thread(PrintValue);
t1.IsBackground = true;
t1.Start();
static void PrintValue()
{
while (true)
{
Console.WriteLine("thread t.");
}
}
效果:
后台线程:
Thread t1 = new Thread(PrintValue);
t1.IsBackground = false;
t1.Start();
static void PrintValue()
{
while (true)
{
Console.WriteLine("thread t.");
}
}
线程优先级决定了相对于操作系统中其他活跃线程所占的执行时间,提升指定线程优先级如果该线程处理任务比较重则会降低其他线程优先级会导致其他抢占不到CPU处理时间片,。
Thread t1 = new Thread(() => { Console.ReadLine(); });
t1.Priority = ThreadPriority.Highest;
t1.Start();
ThreadPriority枚举如下:
//
// 摘要:
// Specifies the scheduling priority of a System.Threading.Thread.
public enum ThreadPriority
{
//
// 摘要:
// The System.Threading.Thread can be scheduled after threads with any other priority.
Lowest = 0,
//
// 摘要:
// The System.Threading.Thread can be scheduled after threads with Normal priority
// and before those with Lowest priority.
BelowNormal = 1,
//
// 摘要:
// The System.Threading.Thread can be scheduled after threads with AboveNormal priority
// and before those with BelowNormal priority. Threads have Normal priority by default.
Normal = 2,
//
// 摘要:
// The System.Threading.Thread can be scheduled after threads with Highest priority
// and before those with Normal priority.
AboveNormal = 3,
//
// 摘要:
// The System.Threading.Thread can be scheduled before threads with any other priority.
Highest = 4
}
如果想让某线程的优先级比其他进程中的线程高,那么就必须提升进程的优先级。