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

使用thread类的name属性来更改或获取线程的名称。下面我们来看一个例子,演示如何设置和获取线程的名称。

using system;
using system.threading;

public class mythread
{
    public void thread1()
    {
        for(int i=0; i<5; i++)
        {
            thread t = thread.currentthread;
            console.writeline(t.name + " is running at :"+i);
        }
    }
}
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));
        thread t3 = new thread(new threadstart(mt.thread1));
        t1.name = "thread1";
        t2.name = "thread2";
        t3.name = "thread3";
        t1.start();
        t2.start();
        t3.start();
    }
}

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

thread1 is running at :0
thread2 is running at :0
thread3 is running at :0
thread3 is running at :1
thread3 is running at :2
thread3 is running at :3
thread3 is running at :4
thread2 is running at :1
thread2 is running at :2
thread2 is running at :3
thread2 is running at :4
thread1 is running at :1
thread1 is running at :2
thread1 is running at :3
thread1 is running at :4

滚动到顶部 滚动到底部
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
QQ群:570078429
网站备案号:
粤ICP备17073857号