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

你可能感兴趣的文章
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>