多线程同时异步 [英] Multi thread and async at same time

查看:92
本文介绍了多线程同时异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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();

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

As far as I understand, this code will create 2 new threads, run the specified methods, pause the main thread until both these threads complete, and then continue execution.

假设我说的是正确的,到目前为止一切都很好.

Assuming what I say is correct, all fine so far.

下一步,我想尝试一下:

Next I want to try this:

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


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

基本上向每个线程添加异步并等待.

Basically adding async and await to each thread.

编译器接受此更改,并且似乎在本地运行,但是此代码还可以,并且是否有可能导致我进一步遇到任何问题,例如,线程会变得混乱,无法等待,混淆结果等

The compiler accepts this change and it seems to run locally, but is this code ok, and is it likely to cause me any problems further down the line, for example will the threads get confused, fail to wait, mix up results etc.

我对异步有相当不错的理解,对多线程也有基本的了解,我无法想到为什么它不起作用.

I have a reasonably good understanding of async and a basic understanding of multi threading, and I cannot think of any reason why this would not work.

代码在本地运行,但是我担心的是,在服务器上负载很重的情况下,可能会出现本地版本中不存在的问题....

The code runs locally, but my worry is that under heavy load on the server issues may appear that were not present in a local version....

推荐答案

编译器接受此更改,并且似乎在本地运行,但是此代码还可以,并且是否有可能导致我进一步遇到任何问题,例如,线程会变得混乱,无法等待,混淆结果等

The compiler accepts this change and it seems to run locally, but is this code ok, and is it likely to cause me any problems further down the line, for example will the threads get confused, fail to wait, mix up results etc.

我对异步有相当不错的理解,对多线程也有基本的了解,我无法想到为什么它不起作用.

I have a reasonably good understanding of async and a basic understanding of multi threading, and I cannot think of any reason why this would not work.

是的,此代码会给您带来麻烦.线程没有按您期望的那样等待.您正在将 async void lambda 传递给Thread构造函数,并且该线程在到达该lambda中的await时将退出,在之前它将设置object1/object2变量.因此,这些变量完全有可能在Join之后保留在null.

Yes, this code will cause you problems. The threads are not waiting as you expect. You're passing an async void lambda to the Thread constructor, and that thread will exit as soon as it hits the await in that lambda, before it sets the object1/object2 variable. So it's entirely possible those variables remain null after the Join.

FCin 所述,正确的解决方案是使用异步并发. (为了避免与Parallel类型和任务并行库混淆,我在这里避免使用并行"一词).异步并发使用Task.WhenAll:

The proper solution, as FCin posted, is to use asynchronous concurrency. (I avoid the term "parallel" here to reduce confusion with the Parallel type and the Task Parallel Library). Asynchronous concurrency uses Task.WhenAll:

// Start both methods concurrently
var task1 = _myService.GetMethod(variable1, variable2);
var task2 = _myService.GetMethod2(variable3, variable4);

// (Asynchronously) wait for them both to complete.
await Task.WhenAll(task1, task2);

// Retrieve results.
myObject object1 = await task1;
myObject object2 = await task2;

这篇关于多线程同时异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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