I was just doing some random testing on locking in multi-threading this morning and strangely I found that locking a private "string" in two separate instance actually blocks the other thread from executing. Please find the code below for reference.
The thing that confuses me is that "string" in two objects are really two separate object, so why locking on one blocks the other? ( Note, if you replace string with other reference type object like List, it will not block the other thread from executing, which is really what we expected... )
class Program {
static void Main(string[] args) {
Thread th = new Thread(DoWork);
th.Start();
Thread th2 = new Thread(DoWork);
th2.Start();
}
static void DoWork() {
Test importer = new Test();
importer.SyncTest();
}
}
public class Test {
public void SyncTest() {
string find = "test";
lock(find) {
Console.WriteLine("thread starting...");
Thread.Sleep(4000);
}
}
}