简体   繁体   English

并发java

[英]concurrency java

static ConcurrentHashMap k;

X x; 
//line 3: 
synchronized(k){ x = k.get("LL");}

// line 5 

// line 12: 
synchronized(k){if(x.b){x.b = false;}} 

'k' is a shared map. “ k”是共享的地图。 First thread goes through line 3, second thread goes through line 3 when thread 1 is at line 5, thread one alters xb to false, what xb does thread 2 see? 当线程1位于第5行时,第一个线程经过第3行,第二个线程经过第3行,线程一将xb更改为false,线程2看到了什么xb? Line 5 is meant to show that thread 2 gets its x before thread one got in the second synch block 第5行旨在表明线程2在线程1进入第二个同步块之前先获得其x

You have somewhat overspecified the terms "first thread" and "second thread"; 您对术语“第一线程”和“第二线程”的定义有些过高; your question presupposes that the first thread to enter the first synchronized block will also be the first thread to enter the second synchronized block, but there's really no reason to expect that. 您的问题假设,第一个进入第一个synchronized块的线程也将是第一个进入第二个synchronized块的线程,但是实际上没有理由期望。

However, the first synchronized block doesn't do anything very relevant or interesting — nothing in your code-snippet mutates k , and the first synchronized block merely accesses it — so I'm just going to ignore the fact that it's synchronized . 但是,第一个synchronized块没有做任何非常相关或有趣的事情-代码段中的任何内容都不会使k突变,而第一个synchronized块仅访问它-因此,我将忽略它已synchronized的事实。 That will simplify the definitions slightly: now "first thread" means the first thread to enter the second synchronized block, and "second thread" means the second thread to enter the second synchronized block. 这将稍微简化定义:现在,“第一个线程”表示进入第二个synchronized块的第一个线程,而“第二个线程”表示进入第二个synchronized块的第二个线程。 (OK so far?) That matter of definition out of the way . (到目前为止还好吗?)这个定义问题不重要。 . .

Assuming that there's no possibility of some other thread coming in and setting xb to true — or, for that matter, of the first thread doing so, in code that's after the snippet that you quote — and similarly, no possibility of the two threads getting completely different results for k.get("LL") due to things going on elsewhere — then the second thread will see xb as false , just as one would naïvely expect. 假设没有其他线程进入并将xb设置为true的可能性,或者就此而言,第一个线程这样做的可能性是,在引述摘录之后的代码中,同样,两个线程也没有可能由于其他地方发生的事情, k.get("LL")结果完全不同-然后第二个线程将xb视为false ,就像一个天真的期望那样。 This is because 这是因为

If one action happens-before another, then the first is visible to and ordered before the second. 如果一个动作发生在另一个动作之前 ,则第一个动作对第二个动作可见,并在第二个动作之前排序。

and

An unlock on a monitor happens-before every subsequent lock on that monitor. 监视器上的解锁发生在该监视器上的每个后续锁定之前

(Both of the above quotations are from §17.5.5 of The Java Language Specification , Java SE 7 Edition ; see that section, and the section before it, for more formalities.) (以上两个引文均来自Java语言规范 Java SE 7 Edition的§17.5.5 ;有关更多形式,请参见该部分及其前面的部分。)

The explanation isn't exactly clear but here's what I understand it to be. 解释尚不清楚,但这是我的理解。

Since thread 1 is at line 5 (already through line 3) and hasn't yet hit line 12 (and your saying somewheres in between 3 and 12 thread 1 sets xb to false), thread 2 should see xb as whatever thread one just set it to (which is false). 由于线程1位于第5行(已经通过第3行)并且尚未到达第12行(并且您的说法在3到12之间,因此线程1将xb设置为false),因此线程2应该将xb视为刚刚设置的任何线程它(错误)。 Though I don't see that thread 2 actually cares to see xb in line 3. And it's not clear to me why line 5 (which I don't see) is not synchronized, yet line 12 which sets it to false is synchronized. 尽管我看不到线程2实际上在乎在第3行中看到xb。而且我不清楚为什么第5行(我看不到)为什么不同步,但是将其设置为false的第12行是同步的。

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

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