-1

for example--

Object obj = new Object();

then I lock one section with lock(obj)

and I lock in other section also lock(obj)

if one thread is in the first section, does that mean that the other section is also locked?

Onic Team
  • 1,620
  • 5
  • 26
  • 37

1 Answers1

2

Locking is done on an object, not on a section or location of the code. If one thread takes the lock in the first location, the second thread can't take it anywhere - neither in the same location and nor in any other location.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • So if let's say I write in other location: while(true) { lock(obj) ....} for the other thread will try to take the lock until the first thread will unlock? Is that like wait/pulse? – user8387501 Jul 30 '17 at 06:31
  • You don't need and shouldn't use a while loop – H H Jul 30 '17 at 12:00