CSharp基础 专题
专题目录
您的位置:csharp > CSharp基础 专题 > C#线程实例:Join()方法
C#线程实例:Join()方法
作者:--    发布时间:2019-11-20

join()方法使所有的调用线程等待直到当前线程(已连接线程)终止或完成其任务。

参考以下示例 -

using system;
using system.threading;
public class mythread
{
    public void thread1()
    {
        for (int i = 0; i < 5; i++)
        {
            console.writeline("thread1 : "+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));
        thread t3 = new thread(new threadstart(mt.thread1));
        t1.start();
        t1.join();
        t2.start();
        t3.start();
    }
}

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

thread1 : 0
thread1 : 1
thread1 : 2
thread1 : 3
thread1 : 4
thread1 : 0
thread1 : 0
thread1 : 1
thread1 : 1
thread1 : 2
thread1 : 2
thread1 : 3
thread1 : 3
thread1 : 4
thread1 : 4

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册