博客
关于我
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/

你可能感兴趣的文章
python+locust电商全流程性能测试
查看>>
Python函数运行的可执行文件的终端输出如何以一般方式静音?
查看>>
Python+Pytest+Allure+Git+Jenkins接口自动化框架
查看>>
python+pytest接口自动化 —— 参数关联
查看>>
python+pytest接口自动化 —— 参数关联
查看>>
Python+pytest接口自动化 —— 接口测试基础
查看>>
python+pytest接口自动化 —— 自动化用例编写思路 (使用pytest编写一个测试脚本)
查看>>
Python+pytest接口自动化之cookie绕过登录(保持登录状态)
查看>>
python+pytest接口自动化:接口测试
查看>>
Python+requests+unittest执行接口自动化测试详情
查看>>
Python+requests+unittest执行接口自动化测试详情
查看>>
python+requests+unittest执行自动化接口测试!
查看>>
Python+Requests编码识别Bug
查看>>
python函数编写_[零基础学python]传说中的函数编写条规
查看>>
Python+selenium —— UI自动化之鼠标操作
查看>>
python函数注释,参数后面加冒号:,函数后面的箭头→是什么?
查看>>
Python+Selenium+Threading进行兼容性测试
查看>>
Python+selenium+unittest的GUI自动化框架实现
查看>>
python函数拟合不规则曲线_Python计算&绘图——曲线拟合问题(转)
查看>>
python函数定义的基本格式_零基础学python-2.19 定义函数、调用函数与默认参数
查看>>