个性化阅读
专注于IT技术分析

C#线程加入join

点击下载

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

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();
    }
}

输出:

0
1
2
3
4
0
0
1
1
2
2
3
3
4
4
赞(0)
未经允许不得转载:srcmini » C#线程加入join

评论 抢沙发

评论前必须登录!