• c# Thread.Join()方法讲解


    原文网址引用:https://blog.csdn.net/qq_39143594/article/details/109348514

    一 Thread.Join() 官网解释如下:
    Join 一个同步方法,该方法阻止调用线程 (即调用方法的线程) ,直到 Join 调用方法的线程完成。 使用此方法可以确保线程已终止。 如果线程未终止,调用方将无限期阻止。 在下面的示例中, Thread1 线程调用的 Join() 方法 Thread2 ,这会导致 Thread1 一直阻止到 Thread2 完成为止。

    个人感觉初学者很难弄清楚中间的专业名词,所以我将自己的使用体会记录下来,希望能给初学者一定的帮助。

    二 个人对官网解释的理解:
    1.一个线程在执行的过程中,可能调用另一个线程,前者可以称为调用线程,后者成为被调用线程。
    2.Thread.Join方法的使用场景:调用线程挂起,等待被调用线程执行完毕后,继续执行。
    3.被调用线程执行Join方法,告诉调用线程,你先暂停,我执行完了,你再执行。从而保证了先后关系。

    三 实例讲解

    using System;
    using System.Threading;

    public class Example
    {
    static Thread thread1, thread2;

    public static void Main()
    {
    thread1 = new Thread(ThreadProc);
    thread1.Name = "Thread1";
    thread1.Start();

    thread2 = new Thread(ThreadProc);
    thread2.Name = "Thread2";
    thread2.Start();
    }

    private static void ThreadProc()
    {
    // 这里具体是哪一个线程运行是不一定的,随机分配的
    Console.WriteLine("\nCurrent thread123: {0}", Thread.CurrentThread.Name);
    //这里的作用就是在当前是线程1,线程2的状态暂时还在等待状态时。线程1(调用线程)中间去调用线程2(被调用线程) 一直到线程2执行该方法完毕才继续回来执行线程1
    if (Thread.CurrentThread.Name == "Thread1" && thread2.ThreadState != ThreadState.Unstarted)
    thread2.Join();

    Thread.Sleep(6000);
    Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
    Console.WriteLine("Thread1: {0}", thread1.ThreadState);
    Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
    }
    }



    执行效果如下:
    // The example displays output like the following:
    // Current thread: Thread1
    //
    // Current thread: Thread2
    //
    // Current thread: Thread2
    // Thread1: WaitSleepJoin
    // Thread2: Running
    //
    //
    // Current thread: Thread1
    // Thread1: Running
    // Thread2: Stopped

    案例二:

    using System;
    using System.Net;
    using System.Threading;


    namespace ConsoleApp1thread_join
    {

    public class Apha
    {
    public void methThread()
    {
    for (int i = 0; i < 10; i++)
    {
    Console.WriteLine(Thread.CurrentThread.Name + ": i= " + i);
    }
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    Apha apha = new Apha();
    Thread.CurrentThread.Name = "Main Thread";
    Thread sub_thread = new Thread(new ThreadStart(apha.methThread));
    sub_thread.Name = "Sub Thread";
    for (int j = 0; j < 20; j++)
    {
    if (j == 10)
    {
    sub_thread.Start();
    //o_thread.Abort(); //如果此代码存在,sub_thread。因为并不是立即执行.Start()方法
    // 因为在主线程里面调用了sub_.join() 就一直要调用 sbthread 执行完毕以后才返回主线程继续执行
    sub_thread.Join();
    }
    else
    {
    Console.WriteLine(Thread.CurrentThread.Name + ": j = " + j);
    }
    }
    Console.ReadLine();
    }

    }
    }


    执行效果图如下:


    代码中的“sub_thread.Join();”要是删除的话,执行结果如下:即主线程和sub_thread会交替执行.
    执行效果如下:

    ————————————————
    版权声明:本文为CSDN博主「静宜程」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_39143594/article/details/109348514

  • 相关阅读:
    微微一笑很倾城(1)
    微微一笑很倾城(1)
    陈先生与程太太
    陈先生与程太太
    我在这
    我在这
    曾有一个人,爱我如生命(2)
    曾有一个人,爱我如生命(2)
    周末情妇
    [转载]黄泉嫁衣
  • 原文地址:https://www.cnblogs.com/bruce1992/p/16363141.html
Copyright © 2020-2023  润新知