Www.helper3.com
热门关键字: php net javascript java
CSharp基础 专题
专题目录
您的位置:csharp > CSharp基础 专题 > C#线程实例:Sleep()方法
C#线程实例:Sleep()方法
作者:--    发布时间:2019-11-20 10:28:27

在执行的线程上调用sleep()方法来指定的毫秒暂停当前线程。从面使其他线程有机会开始执行。

using system;
using system.threading;
public class mythread
{
    public void thread1()
    {
        for (int i = 0; i < 10; i++)
        {
            string curtime = datetime.now.tostring();
            console.writeline("thread1 at "+ curtime + " => "+ i);
            thread.sleep(500);// 挂起半秒
        }
    }
}
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));
        t1.start();
        t2.start();
    }
}

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

thread1 at 2017/9/13 3:24:42 => 0
thread1 at 2017/9/13 3:24:42 => 0
thread1 at 2017/9/13 3:24:42 => 1
thread1 at 2017/9/13 3:24:42 => 1
thread1 at 2017/9/13 3:24:43 => 2
thread1 at 2017/9/13 3:24:43 => 2
thread1 at 2017/9/13 3:24:43 => 3
thread1 at 2017/9/13 3:24:43 => 3
thread1 at 2017/9/13 3:24:44 => 4
thread1 at 2017/9/13 3:24:44 => 4
thread1 at 2017/9/13 3:24:44 => 5
thread1 at 2017/9/13 3:24:44 => 5
thread1 at 2017/9/13 3:24:45 => 6
thread1 at 2017/9/13 3:24:45 => 6
thread1 at 2017/9/13 3:24:45 => 7
thread1 at 2017/9/13 3:24:45 => 7
thread1 at 2017/9/13 3:24:46 => 8
thread1 at 2017/9/13 3:24:46 => 8
thread1 at 2017/9/13 3:24:46 => 9
thread1 at 2017/9/13 3:24:46 => 9

所有评论

loading......

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

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