线程操作之线程休眠

线程的休眠是通过Thread类的Sleep方法实现的(休眠的时间是以毫秒为单位的),而Thread类的实例的IsAlive属性可以判断线程是否执行完毕,Sleep方法的使用格式为:

自流井ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!


实例代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace 线程休眠
{
class Program
{
static void Main(string[] args)
{
ThreadStart ts = new ThreadStart(method);
Thread t = new Thread(ts);
t.Start();
while (t.IsAlive)
{
Console.WriteLine("线程开始执行!");
Thread.Sleep(1);//设置休眠时间为1毫秒
}
Console.ReadKey();
}
public static void method()
{
string state;
for (int i = 1; i < 5001; i++)
{
state = Thread.CurrentThread.ThreadState.ToString();
if (i % 5 == 0)
{
Console.WriteLine("当前线程状态为:{0}", state);
}
}
}
}
}
新闻名称:线程操作之线程休眠
文章路径:http://pwwzsj.com/article/sjdpdo.html