【 以下文字转载自 Programming 讨论区 】 发信人: dancingfrog (Dancing Frog), 信区: Programming 标 题: Java concurrency的疑惑,难道我理解错了? 发信站: BBS 未名空间站 (Mon Aug 10 11:50:19 2015, 美东) 小弟刚开始学,有一事不明。 如果用synchronized,可以保证一个对象不会被同时写入。但我觉得就算synchronized 还是会有问题。比如x,y两个线程同时读并且试图写一个变量v Time 0 xyTime 1 x---(write v+1)--->v //v becomes 4 after write y---(write v+1)--->v //failed, because v is locked by x Time 2 y---(write v+1)--->v //v is still 4, because the value read by y is 3. 这样看来,虽然synchronized可以保护写不同时进行,但无非是推迟了y写入v的时间, 问题依然是存在的。 请问这个问题java有解决吗?
You can file I-140 now, even you're in China. However, you can't file 485 before either your service date ends or you have waived the two year restriction.
l*n
6 楼
Look at the code below: public class Example { private int i; synchronized public void plusOne() { i++; } } what's the problem? When thread x call plusOne method, x get the monitor of Example, y cannot do anything with the synchronized method of Example, just block there and wait. After x has done with plusOne, y can have opportunity to call plusOne.
d*g
7 楼
【 以下文字转载自 Programming 讨论区 】 发信人: dancingfrog (Dancing Frog), 信区: Programming 标 题: Java concurrency的疑惑,难道我理解错了? 发信站: BBS 未名空间站 (Mon Aug 10 11:50:19 2015, 美东) 小弟刚开始学,有一事不明。 如果用synchronized,可以保证一个对象不会被同时写入。但我觉得就算synchronized 还是会有问题。比如x,y两个线程同时读并且试图写一个变量v Time 0 xyTime 1 x---(write v+1)--->v //v becomes 4 after write y---(write v+1)--->v //failed, because v is locked by x Time 2 y---(write v+1)--->v //v is still 4, because the value read by y is 3. 这样看来,虽然synchronized可以保护写不同时进行,但无非是推迟了y写入v的时间, 问题依然是存在的。 请问这个问题java有解决吗?
Look at the code below: public class Example { private int i; synchronized public void plusOne() { i++; } } what's the problem? When thread x call plusOne method, x get the monitor of Example, y cannot do anything with the synchronized method of Example, just block there and wait. After x has done with plusOne, y can have opportunity to call plusOne.