java CyclicBarrier“ count”字段未使用final定义。如何确保计数字段的安全发布

问题描述 投票:0回答:1

我最近正在学习Java并发编程。我知道final关键字可以保证安全的发布。但是,当我阅读“ CyclicBarrier”源代码时,发现“ count”字段未使用final关键字。因此,当第一次调用“ dowait”时,“ count”是否有可能是0而不是初始值

public class CyclicBarrier {
...
private int count;
...
public CyclicBarrier(int parties, Runnable barrierAction) {
        if (parties <= 0) throw new IllegalArgumentException();
        this.parties = parties;
        this.count = parties;
        this.barrierCommand = barrierAction;
}
public int await() throws InterruptedException, BrokenBarrierException {
        try {
            return dowait(false, 0L);
        } catch (TimeoutException toe) {
            throw new Error(toe); // cannot happen
        }
    }
private int dowait(boolean timed, long nanos)
        throws InterruptedException, BrokenBarrierException,
               TimeoutException {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            final Generation g = generation;

            if (g.broken)
                throw new BrokenBarrierException();

            if (Thread.interrupted()) {
                breakBarrier();
                throw new InterruptedException();
            }

            int index = --count;
            if (index == 0) {  // tripped
                boolean ranAction = false;
                try {
                    final Runnable command = barrierCommand;
                    if (command != null)
                        command.run();
                    ranAction = true;
                    nextGeneration();
                    return 0;
                } finally {
                    if (!ranAction)
                        breakBarrier();
                }
            }

            // loop until tripped, broken, interrupted, or timed out
            for (;;) {
                try {
                    if (!timed)
                        trip.await();
                    else if (nanos > 0L)
                        nanos = trip.awaitNanos(nanos);
                } catch (InterruptedException ie) {
                    if (g == generation && ! g.broken) {
                        breakBarrier();
                        throw ie;
                    } else {
                        // We're about to finish waiting even if we had not
                        // been interrupted, so this interrupt is deemed to
                        // "belong" to subsequent execution.
                        Thread.currentThread().interrupt();
                    }
                }

                if (g.broken)
                    throw new BrokenBarrierException();

                if (g != generation)
                    return index;

                if (timed && nanos <= 0L) {
                    breakBarrier();
                    throw new TimeoutException();
                }
            }
        } finally {
            lock.unlock();
        }
    }
}

有人可以帮我吗?

java count final cyclicbarrier
1个回答
0
投票

““安全发布”与并发,锁或类似的东西无关。

mutability有关。如果类发布(即从方法返回)引用可变对象,则调用者可以对其进行突变(更改其字段的值)。这是“不安全的发布”。

但是,您可以通过返回可变对象的深层副本来安全地发布可变对象,也可以使用不可变的API封装该可变对象,该API委托getter并防止调用setter。

int是原始类型,因此本质上是不可变的,因此不需要特殊处理即可安全地发布。

© www.soinside.com 2019 - 2024. All rights reserved.