All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP技巧:正确理解PHP程序编译时的错误信息_php资料_编程技术
PHP入门:初学来看配置文件PHP.INI的中文注释_[PHP教程]
PHP进阶教程:实现网站的无限分类_php资料_编程技术
SQL,Server自带的用户自定义函数_mssql学习_编程技术
.NET内置对象之Request对象_[Asp.Net教程]
用PHP程序实现从数组里筛选出重复的数据_[PHP教程]
SQL,Server非正常删除日志文件(ldf)恢复方法_[SQL,Server教程]
一个使用C#自写的SPLIT函数的代码_.net资料_编程技术
HTML 和 XHTML 区别_[Html教程]
你必须知道的.NET之特性和属性_[Asp.Net教程]
Reading number is top 10 pictures
The sixties of the last century, China is such a kill pig
Lewd,it is too lewd.
西游四格漫画(四)
战场废物2
大四女生借债隆胸成功
A man's favorite things8
NeedWallpaper3
这只猪到底犯了什么错?
我国房地产真相
Household design comfortable contracted
Download software ranking
XML+Web+Service开发教程
C++教程第四版
Unix video tutorial7
Unix video tutorial12
Boxer's Top ten classic battle2
jdk1.6 for windows
Popkart Cracked versions Mobile phone games
Tram sex maniac 2 (H) rar bag18
Boxer's Top ten classic battle5
豪门浪荡史
qq published in(发表于) 2014/7/11 9:28:11 Edit(编辑)
C#教程:线程的优先权

C#教程:线程的优先权

C#教程:线程的优先权

线程的优先权

每个线程都具有分配给它的线程优先级。为在公共语言运行库中创建的线程最初分配的优先级为ThreadPriority.Normal。在运行库外创建的线程会保留它们在进入托管环境之前所具有的优先级。用户可以使用Thread.Priority属性获取或设置任何线程的优先级。

线程是根据其优先级而调度执行的。即使线程正在运行库中执行,所有线程都是由操作系统分配处理器时间片的。用于确定线程执行顺序的调度算法的详细情况随每个操作系统的不同而不同。在某些操作系统下,具有最高优先级(相对于可执行线程而言)的线程经过调度后总是首先运行。如果具有相同优先级的多个线程都可用,则计划程序将遍历处于该优先级的线程,并为每个线程提供一个固定的时间片来执行。如果在给定的优先级上不再有可运行的线程,则计划程序将移到下一个较低的优先级并在该优先级上调度线程以执行。如果具有较高优先级的线程可以运行,则具有较低优先级的线程将被抢先,并允许具有较高优先级的线程再次执行。除此之外,当应用程序的用户界面在前台和后台之间移动时,操作系统还可以动态调整线程优先级。其他操作系统可以选择使用不同的调度算法。

可以为线程分配以下任何一个优先级值,如下所示。

Highest

AboveNormal

Normal

BelowNormal

Lowest

示例

线程的优先级

下面的示例展示了如何使用优先级。

using System;

using System.Threading;

class Test

{

static void Main()

{

PriorityTest priorityTest = new PriorityTest();

Thread threadOne = new Thread(new ThreadStart(priorityTest.ThreadMethod));

threadOne.Name = "线程1";

Thread threadTwo = new Thread(new ThreadStart(priorityTest.ThreadMethod));

threadTwo.Name = "线程2";

threadTwo.Priority = ThreadPriority.BelowNormal;

threadOne.Start();

threadTwo.Start();

// 线程休眠20s

Thread.Sleep(20000);

priorityTest.LoopSwitch = False;

}

}

class PriorityTest

{

bool loopSwitch;

public PriorityTest()

{

loopSwitch = True;

}

public bool LoopSwitch

{

set{ loopSwitch = value; }

}

public void ThreadMethod()

{

long threadCount = 0;

while(loopSwitch)

{

threadCount++;

}

Console.WriteLine("{0} with {1,11} priority " +

"has a count = {2,13}", Thread.CurrentThread.Name,

Thread.CurrentThread.Priority.ToString(),

threadCount.ToString("N0"));

}

}

完整程序代码如下:

★ ★★★★Program.cs主程序文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading;

namespace _8_03

{

class Test

{

static void Main(string[] args)

{

PriorityTest priorityTest = new PriorityTest();

Thread threadOne = new Thread(new ThreadStart(priorityTest.ThreadMethod));

threadOne.Name = "线程1";

Thread threadTwo = new Thread(new ThreadStart(priorityTest.ThreadMethod));

threadTwo.Name = "线程2";

threadTwo.Priority = ThreadPriority.BelowNormal;

threadOne.Start();

threadTwo.Start();

// 线程休眠20秒

Thread.Sleep(20000);

priorityTest.LoopSwitch = false;

}

}

class PriorityTest

{

bool loopSwitch;

public PriorityTest()

{

loopSwitch = true;

}

public bool LoopSwitch

{

set { loopSwitch = value; }

}

public void ThreadMethod()

{

long threadCount = 0;

while (loopSwitch)



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.