简体   繁体   English

执行和完成方法

[英]Execute and finish of methods

This is a very naive question, please forgive my ignorance if I use the wrong terms. 这是一个非常天真的问题,如果我使用了错误的条款,请原谅我的无知。

If I have a series of instructions as in the snippet, 如果我在代码段中有一系列说明,

bool methodComplete = false;
methodComplete = doSomeMethod(someParam, etcParam); //long & complex method that returns true
if (methodComplete)
   doSomeOtherMethod();

will the method doSomeMethod() finish its execution before if (methodComplete) is evaluated? 在评估if (methodComplete)之前,方法doSomeMethod()完成执行?

Or is this a case for an asynchronous pattern if I want to guarantee it is completed? 或者,如果我想保证它已完成,这是异步模式的情况吗?

The language specifications define how a program will effectively behave from the point of the user/programmer. 语言规范定义了程序如何从用户/程序员的角度有效地运行。 So, yes, you can assume that the program behaves as that: 所以,是的,您可以假设程序的行为如下:

  • It computes doSomeMethod 它计算doSomeMethod
  • It stores the results in methodComplete 它将结果存储在methodComplete
  • It executes the if clauses 它执行if子句

That said, some optimizations might result in code executed ahead, see Speculative execution . 也就是说,一些优化可能会导致代码执行,请参阅推测执行

will the method doSomeMethod() finished executing before if (methodComplete) is evaluated? 在评估if (methodComplete)之前,方法doSomeMethod()完成执行?

Yes * . 是的*

or is this a case for an asynchronous pattern if I want to guarantee it has completed? 或者,如果我想保证它已经完成,这是异步模式的情况吗?

Only if you are doing parallel computing. 只有你在做并行计算。


*) It can get to be a no if your code is executing in parallel.. *) 如果您的代码并行执行,它可能会变为否。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM