简体   繁体   English

与易失性“状态标志”布尔值同步?

[英]Synchronization with volatile 'status flag' boolean?

I've read about the 'status flag' pattern for the volatile usage. 我已经阅读了有关volatile用法的“状态标志”模式。 It says that I can use the volatile without any sync if the status flag doesn't depend on any other state. 它说,如果状态标志不依赖于任何其他状态,那么我可以使用volatile而不进行任何同步。 It will guarantee the visibility of the flag for other threads. 它将保证其他线程的标志可见性。 Moreover, write to the boolean is atomic. 而且,写入布尔值是原子的。

But in the other related question it's said that it's safe to use the volotile when only one thread can modify the flag. 但是在另一个相关问题中,据说只有当一个线程可以修改该标志时,才可以使用volotile Otherwise, I need to use any synchronization or AtomicBoolean . 否则,我需要使用任何同步或AtomicBoolean

In my example, I have the stopped flag, but it could be modified more than from within one thread: methods stop() and continue() . 在我的例子,我有stopped标志,但它可以修改不是从一个线程中更多:方法stop()continue() The doSmth() doesn't update any states. doSmth()不会更新任何状态。 If assume that it's OK not to do work when the stop() was invoked right after the continue() method, would be the code threadsafe? 如果假设在continue()方法之后立即调用stop()时不做任何工作是可以的,那么代码是线程安全的吗?

class MyClass {
    private volatile boolean stopped;

    public void doWork() {
        while(!stopped) {
            doSmth();
        }
    }

    public void stop() {
        stopped = true;
    }

    public void continue() {
        stopped = false;
    }
}

As for me, it should. 对于我来说,应该。 Could you please clarify if I'm wrong? 你能澄清一下我是否错了吗?

volatile simply ensures that changes to the variable are available to all threads. volatile仅确保对变量的更改可用于所有线程。

The background: a thread may make local copies of shared variables. 背景:线程可以在本地创建共享变量的副本。 Synchronizing the values of these local variables with the global shared variables is what volatile effects. 将这些局部变量的值与全局共享变量同步是一种volatile影响。

However that does not synchronize in the java sence of a single entry, monitor/critical region. 但是,这在单个条目,监视器/关键区域的情况下不会同步

The entire toolchest of java.util.concurrent offers things like ensuring that only one thread may change the value and such. 整个java.util.concurrent提供了诸如确保仅一个线程可以更改值之类的功能。 If you want to start from ground up, one can with two variables do some blocking things: search for Dijkstra algorithms . 如果您想从头开始,则可以使用两个变量来做一些阻碍性的事情:搜索Dijkstra算法

Here I think AtomicBoolean might be nice for non-blocking usage. 在这里,我认为AtomicBoolean对于非阻塞用法可能很好。


If you want to achieve to have a global boolean state that pauses resp. 如果要实现具有全局布尔状态,该状态将暂停 resp。 resumes threads when toggled (your stopped ), instead of some ugly busy wait : 触发(当你恢复线程stopped ),而不是一些丑陋的忙等待

public void run () {
    while (true) {
        doWork(); 
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException ex) {
            return;
        }
    }
}

Using a global CyclicBarrier - not the nicest API as it works with N predefined Runnable s. 使用全局CyclicBarrier不是最好的API,因为它可以与N个预定义的Runnable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM