13_Review Lecture

An object becomes a monitor once a thread locks it

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: a lock before it executes. •  For instance method, the lock is on the object for which the method was invoked. •  For static method, the lock is on the class. •  If one thread invokes a synchronized instance method (respectively, static method) on an object, the lock of that object (respectively, class) is acquired first, then the method is executed, and finally the lock is released. Another thread invoking any synchronized method of that object (respectively, class) is blocked until the lock is released. 35 Synchronizing Instance Methods and Static Methods With the deposit method synchronized, the preceding scenario cannot happen. If Task 2 starts to enter the method, and Task 1 is already in the method, Task 2 is blocked until Task 1 finishes the method. 36 Synchronizing Statements •  A synchronized statement can be used to acquire a lock on any object, not just this object, when executing a block of the code in a method. •  This block is referred to as a synchronized block. The general form of a synchronized statement is as follows: synchronized (expr) { statements; } •  The expression expr mu...
View Full Document

Ask a homework question - tutors are online