A block of code that requires atomic execution is called a critical section . But it is clumsy and inefficient on a multiprocessor because it forces all processors but one to stop execution for the duration of a critical section. A much better mechanism for preventing interference in concurrent programs that may be executed on multiprocessors is locking data objects. When a data object is locked by a thread, no other thread can access or modify the data object until the locking thread releases it. In essence, locking relaxes the concept of atomic execution so that it is relative to a particular object.
Threads can continue executing until they try to access a locked object.
Java relies on object locking to prevent interference. An object can be locked for the duration of a method invocation simply by prefixing the method declaration with the work synchronized . An unusual feature of Java's lock mechanism is the fact that locking an object only inhibits the execution of operations that are declared as synchronized . But it also invites subtle synchronization bugs if the synchronized modifier is inadvertently omitted from one method definition. Of course, concurrency only arises in Java when a program uses more than one thread.