<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://java.dzone.com"  xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dz="http://www.developerzone.com/modules/dz/1.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
 <title>Javalobby - Comments for &quot;ReentrantLock and the Dining Philosophers&quot;</title>
 <link>http://java.dzone.com/news/reentrantlock-and-dining-philo</link>
 <description>Comments for &quot;ReentrantLock and the Dining Philosophers&quot;</description>
 <language>en</language>
<item>
 <title>The point of this post is</title>
 <link>http://java.dzone.com/news/reentrantlock-and-dining-philo#comment-894</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;The point of this post is really about how ReentrantLock gives you options that don&#039;t exist with synchronized blocks and methods.  It&#039;s not an argument for or against the use of locks for protecting access to shared data.  &lt;/p&gt;&lt;p&gt;In general, having one entity tell others what to do sounds to me like a prescription for low concurrency because all of the entities then have to coordinate at the central point.  But it&#039;s hard to even talk about this without talking about real examples.  &lt;/p&gt;&lt;p&gt;I don&#039;t know about you, but I lock my house, my car, etc every day.  :)  Lock-based access to shared state is *one* style of concurrent programming and happens to be the most prevalent in Java (for better or worse and mean argue for worse).  Message-passing/actor-based or lock-free styles also exist and they all have their pros and cons.  &lt;/p&gt;&lt;p&gt;I&#039;ve written about &lt;a href=&quot;http://tech.puredanger.com/2007/10/03/skip-lists/&quot;&gt;ConcurrentSkipListMap&lt;/a&gt; in the past, which is an incredibly cool high-concurrency sorted map in Java 6 written with lock-free code.  Lock-free data structures and algorithms rock.  They&#039;re also incredibly tricky to get right (if you actually read the code for something like ConcurrentSkipListMap/Set or LinkedBlockingQueue).  ReentrantLock itself is implemented with lock-free code actually.  &lt;/p&gt;</description>
 <pubDate>Wed, 13 Feb 2008 11:41:48 -0500</pubDate>
 <dc:creator>puredanger</dc:creator>
 <guid isPermaLink="false">comment 894 at http://java.dzone.com</guid>
</item>
<item>
 <title>I think the Dining</title>
 <link>http://java.dzone.com/news/reentrantlock-and-dining-philo#comment-888</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;I think the Dining Philosophers is not such a great example of multithreading, because you can solve it simply by using one of the philosophers as an orchestrator directing other philosophers to eat.&lt;/p&gt;&lt;p&gt; This is how we solve this situations in real life anyway, using an arbitrator when people can&#039;t agree on their own.&lt;/p&gt;&lt;p&gt;If you can&#039;t show a problem in real life that uses locks, why do you think locks are such a great idea? I don&#039;t see deadlocks in real life, why should computer programs have it? Are we using the wrong abstractions?&lt;/p&gt;&lt;p&gt; A much better idea, IMHO, is to remove the use of locks by using lock free data structures and algorithms.&lt;/p&gt;&lt;p&gt;See: &lt;a href=&quot;http://www.ibm.com/developerworks/java/library/j-jtp11234/&quot;&gt;http://www.ibm.com/developerworks/java/library/j-jtp11234/&lt;/a&gt;&lt;/p&gt;</description>
 <pubDate>Wed, 13 Feb 2008 10:39:49 -0500</pubDate>
 <dc:creator>gschwarz</dc:creator>
 <guid isPermaLink="false">comment 888 at http://java.dzone.com</guid>
</item>
<item>
 <title>Yep, this is an excellent</title>
 <link>http://java.dzone.com/news/reentrantlock-and-dining-philo#comment-846</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;Yep, this is an excellent point.  tryLock() and any retry strategy can avoid deadlock but does not alleviate the potential for livelock where all threads are constantly retrying and never succeeding.  In that case, you don&#039;t deadlock, but make no further progress.&lt;/p&gt;&lt;p&gt;I believe TCP uses an &lt;a href=&quot;http://en.wikipedia.org/wiki/Exponential_backoff&quot;&gt;exponential backoff&lt;/a&gt; for stuff like this (although I haven&#039;t studied this in 10 years).  A fascinating topic in itself, but outside the scope of this article... :)&lt;/p&gt;</description>
 <pubDate>Tue, 12 Feb 2008 15:29:57 -0500</pubDate>
 <dc:creator>puredanger</dc:creator>
 <guid isPermaLink="false">comment 846 at http://java.dzone.com</guid>
</item>
<item>
 <title>Be wary of tryLock() usage.</title>
 <link>http://java.dzone.com/news/reentrantlock-and-dining-philo#comment-844</link>
 <description>&lt;!--paging_filter--&gt;&lt;p&gt;Be wary of tryLock() usage. You may end up in livelock if you use it in trivial way. Imagine two threads and two resources A and B. Thread 1 locks A and then B, thread 2 locks B and then A (clear possibility of deadlock). If you implement trivial idea of tryLock(), thread 1 will lock A and then tryLock B, releasing A if it fails. Unfortunately, at the very same time, thread 2 might have a lock on B, tryLocking A. Both will release their base locks and try entire operation again, possibly failing in same way over and over.&lt;/p&gt;&lt;p&gt;One possibility of avoiding is to wait random amount of time between retries - at least statistically it reduces the chances of livelock (ethernet is solving collisions this way).&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;If somebody wants small puzzle to think about here it is:&lt;/p&gt;&lt;p&gt;You have two classes, A and B. Each of them has a field pointing to other class (0..1-to-0..1 relationship), it&#039;s own lock and a bit of extra state. You have two different paths in code which have to update states of both objects in transaction (so they require locking both of them), but they have the pointer to instance of A, or instance of B (they have to retrieve the pointer to second object in pair from field of first one). And now complicated part - as part of this operation, relationship can change (so a1&amp;lt;-&amp;gt;b1 and null&amp;lt;-&amp;gt;b2 will become a1&amp;lt;-&amp;gt;b2 and null&amp;lt;-&amp;gt;b1). &lt;/p&gt;</description>
 <pubDate>Tue, 12 Feb 2008 14:51:33 -0500</pubDate>
 <dc:creator>abies</dc:creator>
 <guid isPermaLink="false">comment 844 at http://java.dzone.com</guid>
</item>
</channel>
</rss>
