Deadlocks 5 it follows the strategy described above

Info icon This preview shows page 5. Sign up to view the full content.

View Full Document Right Arrow Icon
Deadlocks 5 it follows the strategy described above. And any code following this strategy will not deadlock because we cannot create a cycle if we acquire locks according to the agreed- upon partial order. In practice, methods like transferTo where we have two instances of the same class are a difficult case to handle. Even in the Java standard library it does not always work well: The StringBuffer class has an append method that is subject to race conditions (bad interleavings) because the only way to avoid it would be to create a possible dead- lock. With no clear, efficient solution (buffers do not have unique identifiers like bank accounts), the documentation for StringBuffer indicates that clients of the library must use their own synchronization to prevent calls to append from interleaving with other methods that change the contents or size of a StringBuffer . Fortunately, there are other situations where preventing deadlock amounts to fairly simple rules about the order that locks are acquired. Here are two examples: 1. If you have two different types of objects, you can order the locks by the types of data they protect. For example, the documentation could state, “When moving an item from the hashtable to the work queue, never try to acquire the queue’s lock while holding the hashtable’s lock (the other order is acceptable).” 2. If you have an acyclic data structure like a tree, you can use the references in the data structure to define the locking order. For example, the documentation could state, “If holding a lock for a node in the tree, do not acquire other nodes’ locks unless they are descendants in the tree.” 3 Summary One of the potential dangers of concurrent programming is the possibility of deadlocks, wherein multiple threads are prevented from making progress because of conflicting resource requirements. Deadlocks are bugs and we need to use synchronization approaches such as locks care- fully to avoid this pitfall in concurrent systems. CPEN 221 – Fall 2016
Image of page 5
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