0

我需要确保每个实例生命周期只执行一次特定的启动和停止代码,并且不能“重新启动”实例。对于多个线程可能作用于实例的场景,以下代码是否足够?

public final class MyRunnable {
    private final AtomicBoolean active = new AtomicBoolean(false);
    private final AtomicBoolean closed = new AtomicBoolean(false);

    public void start() {
      if (closed.get()) {
        throw new IllegalStateException("Already closed!");
      }
      if (active.get()) {
        throw new IllegalStateException("Already running!");
      }

      active.set(true);

      // My one-time start code.

      // My runnable code.
    }

    public void stop() {
      if (closed.get()) {
        throw new IllegalStateException("Already stopped!");
      }
      if (!active.get()) {
        throw new IllegalStateException("Stopping or already stopped!");
      }

      active.set(false);

      // My one-time stop code.

      closed.set(true);
    }
}
4

2 回答 2

5

出于两个原因,我会选择单一的 3 值状态。

首先,在“元组”的 4 个可能值中,active,closed只有 3 个有意义,将两者都设置为true导致(可能是良性的,但仍然是)无效状态。您可能会将其视为纯粹的迂腐,但清晰的设计通常会带来其他好处。

这将我们巧妙地引向第二个更可怕的原因:

 active.set(false);
 // <-- what if someone calls start() here?
 closed.set(true); //I assume you wanted to set it to true

正如您从我的评论中看到的那样,您在那里有一个脆弱的地方,可以想象有人可能会start()在您设置active为之后false但在您设置closed为之前调用true

现在你可能只是说“好吧,让我们交换这两个然后closed先设置”,但是你必须解释为什么这两个肯定不会被 JVM 重新排序。您最终可能会将两个标志都设置为true,从而导致上述“无效状态”。

这里还有另一个单独的问题:您遵循的模式是调用get()以检查该值,然后再将set()其用于其他内容。正如PetrosP 指出的那样,这不是原子操作,您可以调用start()a 1000 次,所有这些操作都active视为false. 您需要compareAndSet改用它,它原子的(这是Atomic*类的全部要点),从而保证只有一个线程可以推进状态标志。

因此,让我们将两者结合起来,使用单个 3 值状态(为了简单起见,我使用 AtomicInteger过,但您可以使用AtomicReference和 true enum)和compareAndSet()

public final class MyRunnable {
    private static final int READY_TO_START = 0;
    private static final int ACTIVE = 1;
    private static final int STOPPED = 2;
    private final AtomicInteger status = new AtomicInteger(READY_TO_START);

    public void start() {
      if (!status.compareAndSet(READY_TO_START, ACTIVE)) {
        throw new IllegalStateException("Already started");
      }

      // My one-time start code.
    }

    public void stop() {
        if (!status.compareAndSet(ACTIVE, STOPPED)) {
            throw new IllegalStateException("Can't stop, either not started or already stopped");
        }

      // My one-time stop code.
    }
}
于 2016-03-18T13:44:31.453 回答
1

这个解决方案是不够的。考虑这种情况:两个线程同时进入 start()。一个电话active.get(),它被false退回。然后第二个打电话active.get(),它也得到false了。在这种情况下,它们都将继续。然后第一个将 active 设置为 true。此时第二个也将 active 设置为 true,它们都将继续执行应该运行一次的其余代码。

一个解决方案可能是这样的:

public final class MyRunnable {
    private final AtomicBoolean active = new AtomicBoolean(false);
    private final AtomicBoolean closed = new AtomicBoolean(false);

    public void start() {
        synchronized (this) {
            if (closed.get()) {
                throw new IllegalStateException("Already closed!");
            }
            if (active.get()) {
                throw new IllegalStateException("Already running!");
            }

            active.set(true);
        }

        // My one-time start code.

        // My runnable code.
    }

    public void stop() {
        synchronized (this) {
            if (closed.get()) {
                throw new IllegalStateException("Already stopped!");
            }
            if (!active.get()) {
                throw new IllegalStateException("Stopping or already stopped!");
            }

            // My one-time stop code.

            closed.set(false);
            active.set(false);
        }
    }
}
于 2016-03-18T13:43:24.057 回答