From what I've read on other SO posts, my guess is that neither CreateTry1 nor CreateTry2 are thread safe, but even after reading so much, I'm still not 100% sure this is correct.
public ClassA
{
private string MyString;
private ClassA(string myString)
{
MyString = myString;
}
public static CreateTry1(string aString)
{
return new ClassA(aString);
}
public static CreateTry2(string aString)
{
ClassA a = new ClassA();
a.MyString = aString;
return a;
}
// The Below is not the main focus of the question
static public void Main(String[] args)
{
Thread t1 = new Thread(p =>
{
int i = 0;
while(i < 1000000)
{
ClassA newA = ClassA.CreateTry2("Hello");
i++;
}
});
Thread t2 = new Thread(p =>
{
int i = 0;
while(i < 1000000)
{
ClassA newA = ClassA.CreateTry2("Hello");
i++;
}
});
t1.Start();
t2.Start();
}
More Information 1: Clarification
I edited the code
This question was specifically pertaining to Selenium Grid. I thought if I got a better understanding of what was and was not thread safe (in general) then I would know what to avoid unsafe code when refactoring code to run in Selenium Grid (in parallel). However, I think I jumped into the deep end of the pool, and don't fully know how to swim here, so apologies for that.
I thought part of the answer would be something as simple as, "Non-Static variables (fields and properties) within a non-static class are Always thread safe. Static variables (fields and properties) inside a non-static or static class are not safe in general".
More Information 2: Related SO posts
I read a lot of posts. Some statements cleared things up while others made things more confusing.
This comment made most sense. However, part of the statement says, "When your data in separate instances still gets mixed up, it's most likely because the data is not really separate." It seems hard to not see that one of your variables (fields) was static, so wondering how else this cross contamination of data between threads could have occurred withOut him knowing.
Any conversation related to the following MSDN statement never fully made sense to me:
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
2. About above quote. This was a good question, but the answers at first seemed overly complex. I eventually remembered that 'members' refers to fields, properties and methods within a class (not just fields). A statement this broad would make the answers complex.
The following post, "Is this extension method thread safe", was a great question but I couldn't figure out which answer was correct. I believe both reposes are correct, but Avner's is a very general answer and Ondrej's specifically answers the question.
A few posts, like this one, brought up the term 'shared state' which, after reading the following, seemed to be nothing more than a static field within a class (but I could easily be wrong).