Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - What is a indebted semaphore
    primarykey
    data
    text
    <p>In a Code example it is said that a thread re-synchronization is based on signaling, using an indebted semaphore. </p> <pre><code>final Semaphore indebtedSemaphore = new Semaphore(1 - PROCESSOR_COUNT); </code></pre> <p>What is the purpose of this negative semaphore, with my notebook it would be initialized with -3</p> <pre><code>/** * Sums two vectors, distributing the work load into as many new child threads as there * are processor cores within a given system. Note that the added cost of thread * construction and destruction is higher than the gain of distributing the work for * practically any vector size. * @param leftOperand the first operand * @param rightOperand the second operand * @return the resulting vector * @throws NullPointerException if one of the given parameters is null * @throws IllegalArgumentException if the given parameters do not share the same length */ public static double[] add(final double[] leftOperand, final double[] rightOperand) { if (leftOperand.length != rightOperand.length) throw new IllegalArgumentException(); final double[] result = new double[leftOperand.length]; final int sectorWidth = leftOperand.length / PROCESSOR_COUNT; final int sectorThreshold = leftOperand.length % PROCESSOR_COUNT; final Semaphore indebtedSemaphore = new Semaphore(1 - PROCESSOR_COUNT); for (int threadIndex = 0; threadIndex &lt; PROCESSOR_COUNT; ++threadIndex) { final int startIndex = threadIndex * sectorWidth + (threadIndex &lt; sectorThreshold ? threadIndex : sectorThreshold); final int stopIndex = startIndex + sectorWidth + (threadIndex &lt; sectorThreshold ? 1 : 0); final Runnable runnable = new Runnable() { public void run() { try { for (int index = startIndex; index &lt; stopIndex; ++index) { result[index] = leftOperand[index] + rightOperand[index]; } } finally { indebtedSemaphore.release(); } } }; // EXECUTOR_SERVICE.execute(runnable); // uncomment for managed thread alternative! new Thread(runnable).start(); // comment for managed thread alternative! } indebtedSemaphore.acquireUninterruptibly(); return result; } </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.
 

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