161110-SharedMemoryConcurrency.pdf

4 locks in java the java language has built in

Info icon This preview shows pages 10–13. Sign up to view the full content.

View Full Document Right Arrow Icon
4 Locks in Java The Java language has built-in support for locks that is different than the pseudocode in the previous section, but easy to understand in terms of what we have already discussed. The main addition to the language is a synchronized statement, which looks like this: synchronized (expression) { statements } Here, synchronized is a keyword. Basically, the syntax is just like a while-loop using a different keyword, but this is not a loop. Instead it works as follows: 1. The expression is evaluated. It must produce (a reference to an object — not null or a number. This object is treated as a lock. In Java, every object is a lock that any thread can acquire or release. This decision is a bit strange, but is convenient in an object-oriented language as we will see. 2. The synchronized statement acquires the lock, i.e., the object that is the result of step (1). Naturally, this may block until the lock is available. Locks are reentrant in Java, so the statement will not block if the executing thread already holds it. 3. After the lock is successfully acquired, the statements are executed. 4. When control leaves the statements , the lock is released. This happens either when the final } is reached or when the program “jumps out of the statement” via an exception, a return , a break , or a continue . Step (4) is the other unusual thing about Java locks, but it is quite convenient. Instead of having an explicit release statement, it happens implicitly at the ending } . More CPEN 221 – Fall 2016
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Basic Shared-Memory Concurrency 11 importantly, the lock is still released if an exception causes the thread to not finish the statements . Naturally, statements can contain arbitrary Java code: method calls, loops, other synchronized statements, etc. The only shortcoming is that there is no way to release the lock before reaching the ending } , but it is rare that you want to do so. Here is an implementation of our bank-account class using synchronized statements as you might expect. However, this example is unusual in style and is not encouraged . The key thing to notice is that any object can serve as a lock, so for the lock we simply create an instance of Object , the built-in class that is a superclass of every other class. class BankAccount { private int balance = 0 ; private Object lk = new Object(); int getBalance () { synchronized (lk) { return balance; } } void setBalance ( int x) { synchronized (lk) { balance = x; } } void withdraw ( int amount) { synchronized (lk) { int b = getBalance (); if (amount > b) throw new WithdrawTooLargeException (); setBalance (b - amount); } } // deposit and other operations would also use synchronized(lk) } Because locks are reentrant, it is no problem for withdraw to call setBalance and getBalance . Because locks are automatically released, the exception in withdraw is not a problem. While it may seem naturally good style to make lk a private field since it is an imple- mentation detail (clients need not care how instances of BankAccount provide mutual CPEN 221 – Fall 2016
Image of page 11
Basic Shared-Memory Concurrency 12 exclusion), this choice prevents clients from writing their own critical sections involv- ing bank accounts. For example, suppose a client wants to double an account’s balance.
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern