using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace YouXianJi
{
class SubThread
{
public bool over = false;
public void Working()
{
long number = 0;
Console.WriteLine("------{0}子线程开始执行",Thread.CurrentThread.Name);
Console.WriteLine("优先级:{0}",Thread.CurrentThread.Priority);
while (!over)
{
number++;
}
Console.WriteLine("{0}子线程,现累计数值到{1}",Thread.CurrentThread.Name,number);
Console.WriteLine("------{0}子线程结束------",Thread.CurrentThread.Name);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("主线程开始执行");
SubThread subThread = new SubThread();
ThreadStart start = new ThreadStart(subThread.Working);
Thread thread = new Thread(start);
thread.Name = "SubThread";
thread.Priority = ThreadPriority.AboveNormal;
Thread thread2 = new Thread(start);
thread2.Name = "SubThread2";
thread2.Priority = ThreadPriority.BelowNormal;
Console.WriteLine("请等待三秒,再查询输出结果......");
thread.Start();
thread2.Start();
Thread.Sleep(3000);
subThread.over = true;
thread.Join();
Console.WriteLine("主线程结束");
Console.ReadLine();
}
}
}
多线程高级编程 线程优先级及调度
最新推荐文章于 2021-04-16 22:06:40 发布