Www.helper3.com
热门关键字: php net javascript java
CSharp基础 专题
专题目录
您的位置:csharp > CSharp基础 专题 > C#线程示例:优先级
C#线程示例:优先级
作者:--    发布时间:2019-11-20 10:28:34

我们来看下面的一个例子,演示如何改变线程的优先级。高优先级线程优先执行。但是不能完全保证,因为线程是高度依赖系统的。它只提高了优先级较高的线程在低优先级线程之前执行的机会。

using system;
using system.threading;
public class mythread
{
    public void thread1()
    {
        for(int i=0; i<3; i++)
        {
            thread t = thread.currentthread;
            console.writeline(t.name + " is running");
        }


    }
}
public class threadexample
{
    public static void main()
    {
        mythread mt = new mythread();
        thread t1 = new thread(new threadstart(mt.thread1));
        thread t2 = new thread(new threadstart(mt.thread1));
        thread t3 = new thread(new threadstart(mt.thread1));
        t1.name = "highest-thread ";
        t2.name = "normal-thread ";
        t3.name = "lowest-thread ";
        t1.priority = threadpriority.highest;
        t2.priority = threadpriority.normal;
        t3.priority = threadpriority.lowest;

        t1.start();
        t2.start();
        t3.start();
    }
}

输出是不可预测的,因为线程系统依赖性高。它可以遵循任何算法抢先或非抢占式来执行。

执行上面示例代码,得到以下结果 -

highest-thread  is running
normal-thread  is running
normal-thread  is running
normal-thread  is running
lowest-thread  is running
lowest-thread  is running
lowest-thread  is running
highest-thread  is running
highest-thread  is running

所有评论

loading......

滚动到顶部 滚动到底部
合作联系(微信):
www.helper3.com
关于本站:
宗旨
网站备案号:
粤ICP备17073857号-1

声明:
本站部分内容来自网络,如您发现本站内容侵害到您的利益,请联系本站管理员处理。