c# - 来自线程外部定义的对象的线程中的方法调用会以线程方式运行吗?
问题描述
即使这个方法是在线程外定义的,它会在线程中运行还是并行运行?除了比赛条件之外,还会有任何副作用吗?
SomeClass a = new SomeClass()
ThreadStart childref = new ThreadStart(() =>
{
a.mass = a.CalculateMass() // Lets say this takes a minute to calculate.
});
Thread childThread = new Thread(childref);
childThread.Start();
解决方案
它将在单独的线程中运行。
变量只是内存中仅受进程而非线程限制的位置。因此,应用程序中的单独线程可以访问内存中的相同位置。
如果您不注意确保在任何时候只有一个线程访问它,那么除了竞争条件之外不会有其他副作用。
推荐阅读
- python - 对frozensets列表进行排序?
- c# - FluentValidation 比较来自不同类的 2 个值
- javascript - 为什么这个返回的 promise 的值没有更新?
- vba - 在字符串文本 Access 中插入超链接
- ios - Xamarin.Forms iOS 15 导致应用程序冻结/崩溃
- css - TailwindCSS 中类似 Webflow 的布局
- flutter - 如何以高性能的方式处理抖动的图像像素?
- mysql - MySQL 数据库 discord.js
- wordpress - Google Analytics(分析)数据仅显示在 MonsterInsights 上
- vba - 复制列中的超链接