i have a method called display(string name, int timer). Inside this display i am creating an object to hold name and timer value and assigning the name to the thread.
from main method i am creating multiple threads to run display() logic simultaneously. My problem is even though seperate objects created for each thread, objects values are overwritten.
Attaching the source code here.. Can some please help me if you know...
Loading
VulpesPosted Jul 11, 2012, 2:15 PM
for (int i = 0; i < 5; i++)
Consequently, the value of i that is passed when the display() method is actually invoked will be the value it has reached then and may be different from the value it had when the Thread was created.
To avoid this behaviour, you need to create a temporary variable within the loop, assign the current value of i to it and then use the temporary variable within the anonymous methods. Each such method will then capture a different copy of the temporary variable: