Redian新闻
>
新手问个multi-threading关于synchronized和volatile的问题
avatar
新手问个multi-threading关于synchronized和volatile的问题# Java - 爪哇娇娃
j*u
1
J1也是可以申请绿卡的对吧?是不是可以先申请NIW排期,等到排期到之前waiver就行
。也就是说现在的排期大概是5年,那我们等到4年左右的样子waiver就可以了呢?
avatar
l*n
2
如果我有一段关于synchronized的代码,比如:
Class A {
private double number = 0;
public synchronized void setNumber(value) (
this.number = value;
}
public synchronized double getNumber() {
return this.number;
}
}
这个时候,我没有对number用volatile的modifier,是不是会出现visibility的问题。
这里不会有两个tread同时setNumber,而且number我给了private,不会被class外面直
接修改。但是如果某个thread call setNumber修改了这个double的值,这里修改后的
number能不能被所有的thread看到?
或者我换个问法,假如某个thread用了setNumber(4), 结束之后另一个thread马上
调用getNumber(),返回的值,会不会一定是4?
在这种情况下,是不是应该同时使用synchronized和volatile?
恳请版上各位大神赐教!
avatar
i*u
3
可以申请I-140,但提交I-485之前要办好J-1豁免。我的J-1客户就如此做的。
avatar
g*g
4
It's a problem without volatile keyword. The recommended way today is to use
AtomicDouble instead and you don't need to synchronize the methods.

【在 l******n 的大作中提到】
: 如果我有一段关于synchronized的代码,比如:
: Class A {
: private double number = 0;
: public synchronized void setNumber(value) (
: this.number = value;
: }
: public synchronized double getNumber() {
: return this.number;
: }
: }

avatar
l*n
5
我也觉得是有问题,但是这和我从书上看来的矛盾:
Locking is not just about mutual exclusion; it is also about memory
visibility. To ensure that all threads see the most up to date values of
shared mutable variables, the reading and writing threads must synchronize
on a common lock.
这个意思我的理解就是synchronized可以保证visibility to all threads,但是这段
代码如果有问题,就跟这个意思矛盾。
synchronized可以确保两个thread不会call一个method,但是synchronized对于
visibility到底有没有作用?

use

【在 g*****g 的大作中提到】
: It's a problem without volatile keyword. The recommended way today is to use
: AtomicDouble instead and you don't need to synchronize the methods.

avatar
g*g
6
I didn't read the original post carefully. His example should work actually.
But not the recommended way to do it.
volatile is tricky, basically it works for getter/setter without sync. But
Atomic is much easier.

【在 l******n 的大作中提到】
: 我也觉得是有问题,但是这和我从书上看来的矛盾:
: Locking is not just about mutual exclusion; it is also about memory
: visibility. To ensure that all threads see the most up to date values of
: shared mutable variables, the reading and writing threads must synchronize
: on a common lock.
: 这个意思我的理解就是synchronized可以保证visibility to all threads,但是这段
: 代码如果有问题,就跟这个意思矛盾。
: synchronized可以确保两个thread不会call一个method,但是synchronized对于
: visibility到底有没有作用?
:

avatar
w*z
7
According the JSR-133, the code should work.
But there is more to synchronization than mutual exclusion. Synchronization
ensures that memory writes by a thread before or during a synchronized block
are made visible in a predictable manner to other threads which synchronize
on the same monitor. After we exit a synchronized block, we release the
monitor, which has the effect of flushing the cache to main memory, so that
writes made by this thread can be visible to other threads. Before we can
enter a synchronized block, we acquire the monitor, which has the effect of
invalidating the local processor cache so that variables will be reloaded
from main memory. We will then be able to see all of the writes made visible
by the previous release.
http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#s
for this case, you can use synchronized only on writes or atomic and define
the variable as volatile. It might give you a slightly better performance.

actually.

【在 g*****g 的大作中提到】
: I didn't read the original post carefully. His example should work actually.
: But not the recommended way to do it.
: volatile is tricky, basically it works for getter/setter without sync. But
: Atomic is much easier.

avatar
l*n
8
谢谢你的回复!
我其实在thinking in java上面找到了类似的code,说不加volatile不行。
所以看起来这个是一个比较tricky的部分,应该尽量避免这样使用。

Synchronization
block
synchronize
that
of
visible

【在 w**z 的大作中提到】
: According the JSR-133, the code should work.
: But there is more to synchronization than mutual exclusion. Synchronization
: ensures that memory writes by a thread before or during a synchronized block
: are made visible in a predictable manner to other threads which synchronize
: on the same monitor. After we exit a synchronized block, we release the
: monitor, which has the effect of flushing the cache to main memory, so that
: writes made by this thread can be visible to other threads. Before we can
: enter a synchronized block, we acquire the monitor, which has the effect of
: invalidating the local processor cache so that variables will be reloaded
: from main memory. We will then be able to see all of the writes made visible

avatar
c*e
9
volatile貌似很少人用,一般就是用volatile boolean来设一个true,false.
synchronized更常用。
volatile让这个值不要存到cpu的register里面,而是存到内存里,这样就保证了永远
是最新的值。
貌似现在都是用web application + framework,里面已经有了multi-threading这些东
西了,没必要自己还用这些。一般过去的桌面的application,才需要搞这些高深的东西。

【在 l******n 的大作中提到】
: 谢谢你的回复!
: 我其实在thinking in java上面找到了类似的code,说不加volatile不行。
: 所以看起来这个是一个比较tricky的部分,应该尽量避免这样使用。
:
: Synchronization
: block
: synchronize
: that
: of
: visible

avatar
l*n
10
This is not frequently used in everyday's work but maybe the interviewer
will ask.

西。

【在 c*********e 的大作中提到】
: volatile貌似很少人用,一般就是用volatile boolean来设一个true,false.
: synchronized更常用。
: volatile让这个值不要存到cpu的register里面,而是存到内存里,这样就保证了永远
: 是最新的值。
: 貌似现在都是用web application + framework,里面已经有了multi-threading这些东
: 西了,没必要自己还用这些。一般过去的桌面的application,才需要搞这些高深的东西。

avatar
c*e
11
这种东西就是考理论的,这些得高分的,和实际编程经验2码事。

【在 l******n 的大作中提到】
: This is not frequently used in everyday's work but maybe the interviewer
: will ask.
:
: 西。

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。