C# 属性

C# 抽象

C# 字符串

C# 泛型

C# 杂项

C# 新特性

C# Threading Example: Join() method 线程示例:Join() 方法

它使所有调用线程等待,直到当前线程(加入线程)终止或完成其任务。

例子 (Example)

using System;  
using System.Threading;  
public class MyThread  
{  
    public void Thread1()  
    {  
        for (int i = 0; i < 5; i++)  
        {  
            Console.WriteLine(i);  
            Thread.Sleep(200);  
        }  
    }  
}  
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();  
    }  
}

Output:

0
1
2
3
4
0
0
1
1
2
2
3
3
4
4


  • 使用社交账号登录,本站支持
全部评论(0)