博客
关于我
Thread的join方法使用解析
阅读量:582 次
发布时间:2019-03-11

本文共 2559 字,大约阅读时间需要 8 分钟。

C#中的Thread在Windows Forms应用中应用广泛,常常引发界面和线程交互的挑战。关于线程操作,开发者通常会遇到数据共享、线程异步等问题,这些确实需要一定的技巧才能处理。

在讨论Thread.Join方法时,需要明确其作用。Join方法的主要目的在于阻塞当前线程,等待子线程完成后再继续执行。例如,在主线程启动了子线程后,若希望主线程等待子线程完成,可以使用Join方法实现线程合并。

以下代码示例可以帮助理解Join方法的实际应用:

using System;using System.Windows.Forms;using System.Threading;namespace WindowsFormsApplication1{    public partial class Form2 : Form    {        public Form2()        {            InitializeComponent();        }        private void Form2_Load(object sender, EventArgs e)        {            Thread t = new Thread(new ThreadStart(ShowInfo));            for (int j = 0; j < 20; j++)            {                if (j == 10)                {                    t.Start();                    t.Join(); // 阻塞当前线程,等待子线程完成                }                else                {                    Console.WriteLine("j=" + j);                }            }        }        private void ShowInfo()        {            for (int i = 0; i < 10; i++)            {                Console.WriteLine("i=" + i);            }        }    }}

通过上述代码可以观察到输出结果:j从0到9依次输出,随后由于Join方法的阻塞,主线程会等待子线程完成后再继续输出j从11到20。这样可以有效地观察到Join方法的实际效果。

如果去掉Join方法,主线程和子线程将同时执行,输出结果会变得不规则,可能出现乱序输出的情况。

另一个简化的示例:

using System;using System.Threading;namespace ThreadJonCAPTwo{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        public Thread tMain; // 主线程        private void Form1_Load(object sender, EventArgs e)        {            CreateMainThread();        }        private void CreateMainThread()        {            tMain = new Thread(new ThreadStart(MainThreadDoing));            tMain.Start();        }        private void MainThreadDoing()        {            int totalNum = 20;            for (int i = 1; i <= totalNum; i++)            {                Console.WriteLine("我是店主,我卖了" + i + "条鱼");                if (i == 12)                {                    CreateChildThread();                }            }        }        private void CreateChildThread()        {            Thread tChild = new Thread(new ThreadStart(ChildThreadDoing));            tChild.Start(); // 开启线程            // tChild.Join(); // 阻塞主线程        }        private void ChildThreadDoing()        {            int childNum = 20;            for (int i = 1; i <= childNum; i++)            {                Console.WriteLine("顾客" + i + "来买鱼啦,呵呵....");            }        }    }}

在上述代码中,若保留Join方法,主线程会在子线程完成后继续执行,结果会呈现出“店主卖鱼”和“顾客买鱼”的明确顺序。而若移除Join方法,则可能出现乱序输出。

通过以上例子可以清晰地看出Join方法的作用,即阻塞主线程,等待子线程完成后再继续执行。这种机制对于多线程应用的有效管理至关重要。

转载地址:http://uflvz.baihongyu.com/

你可能感兴趣的文章
ossfs常见配置错误
查看>>
Ossim4系统故障处理
查看>>
Spring赌上未来:响应式的 WebFlux 框架更优雅,性能更强!
查看>>
oss报UnknownHost,k8s设置hostAliases参数
查看>>
OSS报错The difference between the request time and the current time is too large
查看>>
OSS直传与UXCore-Uploader实践
查看>>
Spring详解Bean的生命周期
查看>>
OS模块
查看>>
OS第1章
查看>>
OS第2章 —— 进程
查看>>
OS第3章 —— 进程调度和死锁
查看>>
OS第5章
查看>>
OS第6章 —— 设备管理
查看>>
OTA测试
查看>>
Other User's Bloh Links
查看>>
others
查看>>
Oulipo
查看>>
Outlook 2010 Inside Out
查看>>
outlook 2016 接收发送无法及时收下邮件,如何更改接收时间?
查看>>
Outlook Express could not be started
查看>>