Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why don't just use simple striped locking, like:</p> <pre><code>/** * Striped locks holder, contains array of {@link java.util.concurrent.locks.ReentrantLock}, on which lock/unlock * operations are performed. Purpose of this is to decrease lock contention. * &lt;p&gt;When client requests lock, it gives an integer argument, from which target lock is derived as follows: * index of lock in array equals to &lt;code&gt;id &amp; (locks.length - 1)&lt;/code&gt;. * Since &lt;code&gt;locks.length&lt;/code&gt; is the power of 2, &lt;code&gt;locks.length - 1&lt;/code&gt; is string of '1' bits, * and this means that all lower bits of argument are taken into account. * &lt;p&gt;Number of locks it can hold is bounded: it can be from set {2, 4, 8, 16, 32, 64}. */ public class StripedLock { private final ReentrantLock[] locks; /** * Default ctor, creates 16 locks */ public StripedLock() { this(4); } /** * Creates array of locks, size of array may be any from set {2, 4, 8, 16, 32, 64} * @param storagePower size of array will be equal to &lt;code&gt;Math.pow(2, storagePower)&lt;/code&gt; */ public StripedLock(int storagePower) { if (!(storagePower &gt;= 1 &amp;&amp; storagePower &lt;= 6)) { throw new IllegalArgumentException("storage power must be in [1..6]"); } int lockSize = (int) Math.pow(2, storagePower); locks = new ReentrantLock[lockSize]; for (int i = 0; i &lt; locks.length; i++) locks[i] = new ReentrantLock(); } /** * Locks lock associated with given id. * @param id value, from which lock is derived */ public void lock(int id) { getLock(id).lock(); } /** * Unlocks lock associated with given id. * @param id value, from which lock is derived */ public void unlock(int id) { getLock(id).unlock(); } /** * Map function between integer and lock from locks array * @param id argument * @return lock which is result of function */ private ReentrantLock getLock(int id) { return locks[id &amp; (locks.length - 1)]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload