简体   繁体   English

如何处理代码中的竞争条件?

[英]How can I deal with the race condition in the code?

In the class below the function result() will display the value of the variable whatToDo accordingly the functions addition and multiplication are called.在下面的类中,函数 result() 将显示变量whatToDo的值,相应地调用函数additionmultiplication

Condition : Here, in the below code what if whatTodo is addition but function addition is only not called.条件:在这里,在下面的代码中,如果whatTodoaddition但只是不调用函数addition

Question : How can I deal this Condition ?问题:我该如何处理这种情况

class SomeClass{

let whatToDo: string;

public result(){
        if ( this.whatToDo == 'additon'){
             return this.whatToDo;
           }
        else if (this.whatToDo == 'multiplcation'){
              return this.whatToDo;
}

}
public addition(){
    this.whatToDo = 'addition';
     //do something
}

public multiplication(){
  this.whatToDo = 'multiplication';
     //do something
}
}

Use case - When whatTodo is assigned as addition before calling addition(), so it should tell that whatToDo is undefined so that it can be easily differentiated that addition() is not yet called.This what I want ot achieve.用例 - 当whatTodo在调用whatTodo () 之前被分配为addition ,它应该告诉whatToDoundefined以便可以轻松区分还没有调用 add()。这是我想要实现的。

It looks like just a typo, try:看起来只是一个错字,请尝试:

public result(){
        if ( this.whatToDo == 'addition'){
             return this.whatToDo;
           }
        else if (this.whatToDo == 'multiplication'){
              return this.whatToDo;
}

You misspelled the words "additon" and "multiplcation" in your code.您在代码中拼错了“additon”和“multiplcation”这两个词。

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

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