Thread.join() 阻塞主线程直到完成 [英] Thread.join() blocking main thread until complete

查看:69
本文介绍了Thread.join() 阻塞主线程直到完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我从这个问题中收到的一些评论有关:

This question relates to some comments I received from this question:

多线程和异步同时

我有以下代码:

myObject object1 = null;
Thread obj1Thread = new Thread(() => { object1 = _myService.GetMethod(variable1, variable2); });
obj1Thread.Start();
obj1Thread.Join();


myObject object2 = null;
Thread obj2Thread = new Thread(() => { object2 = _myService.GetMethod2(variable3, variable4); });
obj2Thread.Start();
obj2Thread.Join();

我对这段代码的理解如下:

My understanding of this code was as follows:

此代码将创建 2 个新线程,并发运行指定的方法,暂停主线程直到这两个线程都完成,然后继续执行.

This code will create 2 new threads, run the specified methods concurrently, pausing the main thread until both these threads complete, and then continue execution.

但是,根据上面问题中的一些注释,obj1Thread.Join();下面的代码在obj1Thread完成之前不会执行.

However, according to some of the comments in the above question, the code below obj1Thread.Join(); will not execute until obj1Thread has completed.

谁能确认这是否正确?

如果是这样,这基本上意味着代码根本不是多线程的,而是顺序运行的.

If so, this basically means the code is not multithreaded at all, but runs sequentially.

另外 2 点/问题.

-如何让这段代码并发运行,同时在主线程继续之前在指定点等待所有线程完成

-How can I make this code run concurrently, but also wait at a specified point for all threads to complete before the main thread continues

-我运行此方法 10 次,使用 .join() 平均快 0.5 秒-如果代码基本上按顺序运行,为什么我看到性能改进?

-I ran this method 10 times, and with .join() it was .5 seconds faster on average - why am I seeing a performance improvement if the code is basically running sequentially?

推荐答案

Join() 的调用会阻塞当前线程,这意味着您的第二个线程在第一次调用之前没有创建或启动GetMethod 已完成.

A call to Join() blocks the current thread which means that your second thread isn't created nor started before the first call to GetMethod has completed.

从 .NET Framework 4.0 开始,并行执行两个操作的推荐方法是使用 任务并行库 (TPL).

Starting with .NET Framework 4.0, the recommended way to execute two operations in parallel is to use the task parallel library (TPL).

您可以使用 Parallel.Invoke 方法:

myObject object1 = null;
myObject object2 = null;
Parallel.Invoke(
    () => object1 = _myService.GetMethod(variable1, variable2),
    () => object2 = _myService.GetMethod2(variable3, variable4)
);

它需要任意数量的动作并执行它们,可能是并行的.该方法在所有操作完成后返回.

It takes an arbitrary number of actions and executes them, possibly in parallel. The method returns once all actions have completed.

如果您想等待并行操作异步完成,您可以启动两个单独的任务并使用 Task.WhenAll 方法等待它们:

If you want to wait for the parallel operations to complete asynchronously, you could start two individual tasks and await them using the Task.WhenAll method:

myObject object1 = null;
myObject object2 = null;
Task t1 = Task.Run(() => object1 = _myService.GetMethod(variable1, variable2));
Task t2 = Task.Run(() => object2 = _myService.GetMethod(variable3, variable4));
await Task.WhenAll(t1, t2);

这篇关于Thread.join() 阻塞主线程直到完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆