【发布时间】:2016-06-30 13:49:02
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication58
{
class MultiThreading
{
static void Main(string[] args)
{
MultiThreading mul = new MultiThreading();
Thread t1 = new Thread(new ThreadStart(mul.WriteX));
Thread t2 = new Thread(new ThreadStart(mul.WriteO));
t1.Priority = ThreadPriority.Lowest;
t2.Priority = ThreadPriority.Highest;
t1.Start();
t2.Start();
}
private void WriteX()
{
for (int i = 0; i < 300; i++)
{
Console.Write("X");
}
}
private void WriteO()
{
for (int i = 0; i < 300; i++)
{
Console.Write("O");
}
}
}
}
当我执行上面的代码时,我希望 X 结束打印作业,因为我给该方法的优先级最低,但有时我会在最后得到 O。我的意思是不给第二个线程高优先级保证它会更快完成吗?
【问题讨论】:
-
也许只有一个核心的 cpu,但祝你在现代计算机中找到一个核心
标签: c# multithreading thread-priority