CPEN
161110-SharedMemoryConcurrency.pdf

Remember that each java object represents a different

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

Remember that each Java object represents a different lock. If two threads use synchro- nized statements to acquire different locks, the bodies of the synchronized statements can still execute simultaneously. Here is a subtle bug emphasizing this point, assuming arr is an array of objects and i is an int : void someMethod () { synchronized (arr[i]) { if ( someCondition ()) { arr[i] = new Foo (); } // some more statements using arr[i] } } If one thread executes this method and someCondition() evaluates to true , then it updates arr[i] to hold a different object, so another thread executing someMethod() could acquire this different lock and also execute the synchronized statement at the same time as the first thread. Finally, note that the synchronized statement is far from the only support for concur- rent programming in Java. Later, we will discuss Java’s support for condition variables. The package java.util.concurrent also has many library classes useful for differ- ent tasks. For example, you almost surely should not implement your own concurrent hashtable or queue since carefully optimized correct implementations are already pro- vided. In addition to concurrent data structures, the standard library also has many features for controlling threads. A standard reference is the book Java Concurrency in Practice (Brian Goetz et al.). CPEN 221 – Fall 2016
Image of page 14

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

Basic Shared-Memory Concurrency 15 5 Summary In this note, we have focused on shared-memory concurrency. We considered one of the challenges of concurrent programming: data races . We then examined a mechanism to avoid data races: locks . Java supports locking through the use of synchronized statements. These ideas connect to our three key properties of good software mostly in bad ways. Concurrency is necessary but it causes serious problems for correctness. We’ll work on fixing those problems in the next few readings. Safe from bugs. Concurrency bugs are some of the hardest bugs to find and fix, and require careful design to avoid. Easy to understand. Predicting how concurrent code might interleave with other concurrent code is very hard for programmers to do. It is best to design in such a way that programmers do not have to think about that. Ready for change. Not particularly relevant here. CPEN 221 – Fall 2016
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