【问题标题】:Java - What is a indebted semaphoreJava - 什么是负债信号量
【发布时间】:2013-04-24 14:56:57
【问题描述】:

在代码示例中,据说线程重新同步是基于信号量,使用负债信号量。

final Semaphore indebtedSemaphore = new Semaphore(1 - PROCESSOR_COUNT);

这个负信号量的目的是什么,用我的笔记本它会被初始化为-3

/**
 * 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 < PROCESSOR_COUNT; ++threadIndex) {
        final int startIndex = threadIndex * sectorWidth + (threadIndex < sectorThreshold ? threadIndex : sectorThreshold);
        final int stopIndex  = startIndex  + sectorWidth + (threadIndex < sectorThreshold ? 1 : 0);
        final Runnable runnable = new Runnable() {
            public void run() {
                try {
                    for (int index = startIndex; index < 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;
}

【问题讨论】:

  • 我假设只是开发人员承认信号量可能以负值开头?不过不确定。
  • 负信号量表示可用资源数量为负数。换句话说,此时,应用程序持有所有资源,并且必须在任何其他单元获取它们之前释放它们。 stackoverflow.com/questions/1221322/how-does-semaphore-work
  • @SotiriosDelimanolis 为什么是 -3 ?
  • 你写的,信号量用于重新同步线程。因此每个线程“终止”(意味着它释放其处理资源)都会增加信号量。只要剩下 1 个线程,同步就完成了(如果最后一个线程(在您的示例中是产生其他所有内容的控制器)也会停止,则整个过程将终止。
  • 您的主线程将在这一行 indebtedSemaphore.acquireUninterruptibly(); 等待,直到所有其他线程完成其信号量份额。我猜它被称为indebted,因为它需要偿还债务(计数)才能继续进行。

标签: java multithreading semaphore


【解决方案1】:

源代码中的cmets->

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];
    //--------------------------------------------------------------
    //EXAMPLE: 3 Cores, vector length 10 
    //--------------------------------------------------------------

    final int sectorWidth = leftOperand.length / PROCESSOR_COUNT;
    final int sectorThreshold = leftOperand.length % PROCESSOR_COUNT;
    //indebtedSemaphore (-2) ..  to wait for a unindebted semaphore
    final Semaphore indebtedSemaphore = new Semaphore(1 - PROCESSOR_COUNT);

    //for each core a thread
    for (int threadIndex = 0; threadIndex < PROCESSOR_COUNT; ++threadIndex) {
        //ranges 0-4 , 4-7, 7-10  
        final int startIndex = threadIndex * sectorWidth + (threadIndex < sectorThreshold ? threadIndex : sectorThreshold);
        final int stopIndex = startIndex + sectorWidth + (threadIndex < sectorThreshold ? 1 : 0);
        final Runnable runnable = new Runnable() {
            public void run () {
                try {
                    for (int index = startIndex; index < stopIndex; ++index) {
                        result[index] = leftOperand[index] + rightOperand[index];
                    }
                } finally {
                    //semaphore dept:-2,-1,0,
                    indebtedSemaphore.release();
                }
            }
        };
        // EXECUTOR_SERVICE.execute(runnable);                          // uncomment for managed thread alternative!
        new Thread(runnable).start();                                   // comment for managed thread alternative!
    }

    //wait for unindebted semaphore 
    indebtedSemaphore.acquireUninterruptibly();
    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2010-09-19
    • 2022-11-13
    相关资源
    最近更新 更多