Wee Bubba Posted October 26, 2005 Posted October 26, 2005 if i have a static method like this: public static int CalculateNewInt(int myInt) { myInt += 10; myInt = (myInt * 5) / 10 myInt += 100; return myInt; } would I have to worry about concurrency here. what i mean is this. lets say my thread gets to line 3 of the code ready to increment the input parameter variable by a 100. but before this happens another thread executes this method with a different input parameter. would this much the calculation up? i really dont understand this. thanks Quote
bri189a Posted October 26, 2005 Posted October 26, 2005 Well this isn't a great example of static thread issues. In this case you'd have none because int is a value type and therefore the caller would pass in the value, but the method would do the calculation on a copy of that value and return the value... (the caller would actually recieve a copy of the value). A more realistic example of a static function causing issues in multi-threading is: public class Demo { private static int _instanceInt = 0; public static int CaclulateNewInt() { _instanceInt += 10; _instanceInt = (_instanceInt * 5)/10; _instanceInt += 100; return _instanceInt; } } Now you have the possibility (remote) of _instanceInt being modified halfway through executing because of another thread that has started the routine. To avoid this you lock the variable at the beginning of the sub and unlock it at the end of sub. Notice this would be an issue whether the method was static or not. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.