Unformatted text preview: am, known as critical
• The critical region in the Account example is the entire deposit method.
• You can use the synchronized keyword to synchronize the method so that
only one thread can access the method at a time. o
• One approach is to make Account thread-safe by adding the synchronized
keyword in the deposit method in Line 45 as follows:
public synchronized void deposit(double amount) 33 Cri4cal Regions and Locks • Used to be a topic of systems programming. Now a common topic • The concept of cri4cal regions and locks are everywhere around us – Share road outside of our academic building • Lock Traﬃc light; CR Road – Shared washroom of a coﬀee shop • Lock Physical lock; CR The toilet • In Java, every object is also a lock 34 Synchronizing Instance Methods and Static
• A synchronized method acquires...
View Full Document