發(fā)文章
發(fā)文工具
撰寫
網(wǎng)文摘手
文檔
視頻
思維導(dǎo)圖
隨筆
相冊(cè)
原創(chuàng)同步助手
其他工具
圖片轉(zhuǎn)文字
文件清理
AI助手
留言交流
看如下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace _05線程的優(yōu)先級(jí) { 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("當(dāng)前線程為:{0},線程級(jí)別是{1}",Thread.CurrentThread.Name,Thread.CurrentThread.Priority); } } }
運(yùn)行結(jié)果之一:
首先,解釋一下線程的優(yōu)先級(jí):
線程的優(yōu)先級(jí)并不是你想象的先執(zhí)行哪個(gè)后執(zhí)行哪個(gè)而是所有的線程不論優(yōu)先級(jí)高低都會(huì)執(zhí)行,優(yōu)先級(jí)越高表示CPU分配給該線程的時(shí)間片越多,執(zhí)行時(shí)間就多優(yōu)先級(jí)越低表示CPU分配給該線程的時(shí)間片越少,執(zhí)行時(shí)間就少就這個(gè)問題我們可以做一些測(cè)試,看如下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace _05線程的優(yōu)先級(jí)_測(cè)試線程優(yōu)先級(jí)發(fā)生概率 { 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(); } } }
將注釋代碼取消注釋,運(yùn)行結(jié)果之一:
總結(jié)一句話就是:我們給線程分配優(yōu)先級(jí)增加或減少的是該線程被優(yōu)先執(zhí)行的概率:
來自: 昵稱10504424 > 《C#》
0條評(píng)論
發(fā)表
請(qǐng)遵守用戶 評(píng)論公約
C#教程:線程的優(yōu)先權(quán)
C#教程:線程的優(yōu)先權(quán)。如果具有較高優(yōu)先級(jí)的線程可以運(yùn)行,則具有較低優(yōu)先級(jí)的線程將被搶先,并允許具有較高優(yōu)先級(jí)的線程再次執(zhí)行。Name = "線程1";Thread threadTwo = new Thread(new Thre...
VB.NET多線程開發(fā)實(shí)例
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 安排在具有任何其他優(yōu)先級(jí)的線程之后。// 可以將 System.Threading.Thread 安排在具有 Normal ...
多線程之旅(Thread)
/// <summary> /// 使用Thread 線程的優(yōu)先級(jí)(但是執(zhí)行還是看CPU,可以做優(yōu)先級(jí),但是不是絕對(duì)優(yōu)先) /// </summary> publi...
C#綜合揭秘——細(xì)說多線程(上)
C# 線程
C# 線程。開始一個(gè)線程System.Threading 名字空間的線程類描述了一個(gè)線程對(duì)象,通過使用類對(duì)象,你可以創(chuàng)建、刪除、停止及恢復(fù)一個(gè)線程。創(chuàng)建一個(gè)新線程通過new 操作,并可以通過start()方法啟動(dòng)線程th...
C#WinForm實(shí)踐開發(fā)教程》5.多線程編程技術(shù).ppt
Threading命名空間5.2System.Threading命名空間在.NET程序設(shè)計(jì)中,線程是使用Thread類(或Timer類(線程計(jì)數(shù)器)、ThreadPool類(線程池))來處理的,這些類在System.Threading命名空間中:usingSyste...
深入解讀VB.NET多線程代碼示例 - 51CTO.COM
深入解讀VB.NET多線程代碼示例 - 51CTO.COM.我們今天先來了解一下有關(guān)VB.NET多線程的相關(guān)概念,希望大家可以從中獲得一些幫助,從另一角度來詳細(xì)的解讀VB.NET中的一些基礎(chǔ)概念,掌握應(yīng)用技巧,提高我們...
C#多線程參數(shù)傳遞 | Intel? Developer Zone
C#多線程參數(shù)傳遞 | Intel? Developer ZoneC#多線程參數(shù)傳遞。方法一:在VS2003中,也不能直接訪問,參看一般來說,直接在子線程中對(duì)窗體上的控件操作是會(huì)出現(xiàn)異常,這是由于子線程和運(yùn)行窗體的線程是...
微信掃碼,在手機(jī)上查看選中內(nèi)容