views:

61

answers:

2

For the case of both Join() and lock() one thread can execute after the other.What is the main difference?

+8  A: 

Lock is a monitor which is used to guarantee that only 1 thread can execute at a time.

lock(myobj)
{
   // only 1 thread here
}

Join is used to wait for a thread to complete, before execution continues.

anotherThread.Join();
// execution here only when anotherThread is complete
Andrew Keith
+3  A: 

Thread.Join() waits for a thread to exit. Monitor.Enter(obj) (how the compiler expresses the entry to a lock statement) waits for no other thread to hold obj's object lock.

The former is used to help manage thread lifetimes, the latter to control concurrency.

Richard