161110-SharedMemoryConcurrency.pdf

In a sequential program another class could include

Info icon This preview shows pages 12–15. Sign up to view the full content.

In a sequential program, another class could include this correct method: void doubleBalance (BankAccount acct) { acct. setBalance (acct. getBalance ()* 2 ); } But this code is subject to a bad interleaving: the balance might be changed by another thread after the call to getBalance . If lk were instead public , then clients could use it — and the convenience of reentrant locks — to write a proper critical section: void doubleBalance (BankAccount acct) { synchronized (acct. lk ) { acct. setBalance (acct. getBalance ()* 2 ); } } There is a simpler and more idiomatic way, however: Why create a new Object and store it in a public field? Remember any object can serve as a lock, so it is more conve- nient, and the custom in Java, to use the instance of BankAccount itself (or in general, the object that has the fields relevant to the synchronization). So we would remove the lk field entirely like this (there is one more slightly shorter version coming): class BankAccount { private int balance = 0 ; int getBalance () { synchronized ( this ) { return balance; } } void setBalance ( int x) { synchronized ( this ) { balance = x; } } void withdraw ( int amount) { synchronized ( this ) { int b = getBalance (); CPEN 221 – Fall 2016
Image of page 12

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

Basic Shared-Memory Concurrency 13 if (amount > b) throw new WithdrawTooLargeException (); setBalance (b - amount); } } // deposit and other operations would also use synchronized(this) } All we are doing differently is using the object itself (referred to by the keyword this ) rather than a different object. Of course, all methods need to agree on what lock they are using. Then a client could write the doubleBalance method like this: void doubleBalance (BankAccount acct) { synchronized (acct) { acct. setBalance (acct. getBalance ()* 2 ); } } In this way, doubleBalance acquires the same lock that the methods of acct do, en- suring mutual exclusion. That is, doubleBalance will not be interleaved with other operations on the same account. Because this idiom is so common, Java has one more shortcut to save you a few keystrokes. When an entire method body should be synchronized on this , we can omit the synchronized statement and instead put the synchronized keyword in the method declaration before the return type. This is just a shorter way of saying the same thing. So our final version looks like this: class BankAccount { private int balance = 0 ; synchronized int getBalance () { return balance; } synchronized void setBalance ( int x) { balance = x; } synchronized void withdraw ( int amount) { int b = getBalance (); if (amount > b) CPEN 221 – Fall 2016
Image of page 13
Basic Shared-Memory Concurrency 14 throw new WithdrawTooLargeException (); setBalance (b - amount); } // deposit and other operations would also be declared synchronized } There is no change to the doubleBalance method. It must still use a synchronized statement to acquire acct . While we have built up to our final version in small steps, in practice it is common to have classes defining shared resources consist of all synchronized methods. This approach tends to work well provided that critical sections only access the state of a single object.
Image of page 14

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

Image of page 15
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish
  • Subroutine, Concurrent computing, Basic Shared-Memory Concurrency

{[ 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