我试图弄清楚下面的代码是否存在任何潜在的并发问题.具体而言,可见性问题与volatile变量有关.易失性定义为:此变量的值永远不会被线程本地缓存:所有读取和写入将直接进入"主存储器"
public static void main(String [] args)
{
Test test = new Test();
// This will always single threaded
ExecutorService ex = Executors.newSingleThreadExecutor();
for (int i=0; i<10; ++i)
ex.execute(test);
}
private static class Test implements Runnable {
// non volatile variable in question
private int state = 0;
@Override
public void run() {
// will we always see updated state value? Will updating state value
// guarantee future run's see the value?
if (this.state != -1)
this.state++; …Run Code Online (Sandbox Code Playgroud) 试图学习提升精神和文档中给出的例子让我有点困惑.
参考这段代码:
http://www.boost.org/doc/libs/1_46_1/libs/spirit/example/qi/roman.cpp
特别是这段语法:
start = eps [_val = 0] >>
(
+lit('M') [_val += 1000]
|| hundreds [_val += _1]
|| tens [_val += _1]
|| ones [_val += _1]
)
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么它是+亮('M')而不是*亮('M').因为毕竟不能有零个或多个M与一个或多个M?