发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
看如下代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace _05线程的优先级 { class Program { static void Main(string[] args) { Thread[]t = new Thread[5]; t[0] =new Thread( new ThreadStart(Tell)); t[0].Name = "Lowest"; t[0].Priority = ThreadPriority.Lowest; t[1] = new Thread(new ThreadStart(Tell)); t[1].Name = "BelowNormal"; t[1].Priority = ThreadPriority.BelowNormal; t[2] = new Thread(new ThreadStart(Tell)); t[2].Name = "Normal"; t[3] = new Thread(new ThreadStart(Tell)); t[3].Name = "AboveNormal"; t[3].Priority = ThreadPriority.AboveNormal; t[4] = new Thread(new ThreadStart(Tell)); t[4].Name = "Highest"; t[4].Priority = ThreadPriority.Highest; foreach (Thread itemt in t) { itemt.Start(); } Console.ReadLine(); } public static void Tell() { Console.WriteLine("当前线程为:{0},线程级别是{1}",Thread.CurrentThread.Name,Thread.CurrentThread.Priority); } } }
运行结果之一:
首先,解释一下线程的优先级:
线程的优先级并不是你想象的先执行哪个后执行哪个而是所有的线程不论优先级高低都会执行,优先级越高表示CPU分配给该线程的时间片越多,执行时间就多优先级越低表示CPU分配给该线程的时间片越少,执行时间就少就这个问题我们可以做一些测试,看如下代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace _05线程的优先级_测试线程优先级发生概率 { class Program { static void Main(string[] args) { Thread th1 = new Thread(delegate() { for (int i = 0; i < 100; i++) { Console.Write("H"); } }); // th1.Priority = ThreadPriority.Highest; Thread th2 = new Thread(delegate() { for (int i = 0; i < 100; i++) { Console.Write("A"); } } ); // th2.Priority = ThreadPriority.AboveNormal; Thread th3 = new Thread(delegate() { for (int i = 0; i < 100; i++) { Console.Write("N"); } } ); // th3.Priority = ThreadPriority.Normal; Thread th4 = new Thread(delegate() { for (int i = 0; i < 100; i++) { Console.Write("B"); } } ); // th4.Priority = ThreadPriority.BelowNormal; Thread th5 = new Thread(delegate() { for (int i = 0; i < 100; i++) { Console.Write("L"); } } ); // th5.Priority = ThreadPriority.Lowest; th1.Start(); th2.Start(); th3.Start(); th4.Start(); th5.Start(); Console.ReadKey(); } } }
将注释代码取消注释,运行结果之一:
总结一句话就是:我们给线程分配优先级增加或减少的是该线程被优先执行的概率:
来自: 昵称10504424 > 《C#》
0条评论
发表
请遵守用户 评论公约
C#教程:线程的优先权
C#教程:线程的优先权。如果具有较高优先级的线程可以运行,则具有较低优先级的线程将被抢先,并允许具有较高优先级的线程再次执行。Name = "线程1";Thread threadTwo = new Thread(new Thre...
VB.NET多线程开发实例
end sub end class public class ThreadTest public shared sub Main() dim obj as new aclass dim th1,th2 as thread th1=new Thread(new ThreadSt...
多线程的常见用法详解
// 可以将 System.Threading.Thread 安排在具有任何其他优先级的线程之后。// 可以将 System.Threading.Thread 安排在具有 Normal ...
多线程之旅(Thread)
/// <summary> /// 使用Thread 线程的优先级(但是执行还是看CPU,可以做优先级,但是不是绝对优先) /// </summary> publi...
C#综合揭秘——细说多线程(上)
C# 线程
C# 线程。开始一个线程System.Threading 名字空间的线程类描述了一个线程对象,通过使用类对象,你可以创建、删除、停止及恢复一个线程。创建一个新线程通过new 操作,并可以通过start()方法启动线程th...
C#WinForm实践开发教程》5.多线程编程技术.ppt
Threading命名空间5.2System.Threading命名空间在.NET程序设计中,线程是使用Thread类(或Timer类(线程计数器)、ThreadPool类(线程池))来处理的,这些类在System.Threading命名空间中:usingSyste...
深入解读VB.NET多线程代码示例 - 51CTO.COM
深入解读VB.NET多线程代码示例 - 51CTO.COM.我们今天先来了解一下有关VB.NET多线程的相关概念,希望大家可以从中获得一些帮助,从另一角度来详细的解读VB.NET中的一些基础概念,掌握应用技巧,提高我们...
多线程1
这段代码产生以下输出: Thread - working. Main - aborting my thread. Thread - caught ThreadAbortException - resetting. Exception message: Thread was being aborted. Thread - still alive an...
微信扫码,在手机上查看选中内容