g*g
5 楼
volative only guarrantee you'll get the latest value on read,
it's not atomic. Let's say you have two threading trying to do
increment. Both threads see the initial value 0, and write
1 back. With sync block, the result value is guarranteed to be 2.
That's AtomicInteger gives you too.
【在 g**e 的大作中提到】
: 为啥volatile需要sync?
it's not atomic. Let's say you have two threading trying to do
increment. Both threads see the initial value 0, and write
1 back. With sync block, the result value is guarranteed to be 2.
That's AtomicInteger gives you too.
【在 g**e 的大作中提到】
: 为啥volatile需要sync?
g*e
6 楼
volatile is actually write safe. but its value to write doesn't depend on
the current value.
In your example, i++ or i+=1 is not thread safe, because this actually
involves reading the current value, incrementing it, then setting the new
value.
【在 g*****g 的大作中提到】
: volative only guarrantee you'll get the latest value on read,
: it's not atomic. Let's say you have two threading trying to do
: increment. Both threads see the initial value 0, and write
: 1 back. With sync block, the result value is guarranteed to be 2.
: That's AtomicInteger gives you too.
the current value.
In your example, i++ or i+=1 is not thread safe, because this actually
involves reading the current value, incrementing it, then setting the new
value.
【在 g*****g 的大作中提到】
: volative only guarrantee you'll get the latest value on read,
: it's not atomic. Let's say you have two threading trying to do
: increment. Both threads see the initial value 0, and write
: 1 back. With sync block, the result value is guarranteed to be 2.
: That's AtomicInteger gives you too.
h*0
7 楼
ok...
but for get or set method, we don't need to sync
【在 g*****g 的大作中提到】
: volative only guarrantee you'll get the latest value on read,
: it's not atomic. Let's say you have two threading trying to do
: increment. Both threads see the initial value 0, and write
: 1 back. With sync block, the result value is guarranteed to be 2.
: That's AtomicInteger gives you too.
but for get or set method, we don't need to sync
【在 g*****g 的大作中提到】
: volative only guarrantee you'll get the latest value on read,
: it's not atomic. Let's say you have two threading trying to do
: increment. Both threads see the initial value 0, and write
: 1 back. With sync block, the result value is guarranteed to be 2.
: That's AtomicInteger gives you too.
g*g
8 楼
But i++ kind of stuff is why you want to use AtomicInteger,
very handy for a counter. If it's just get and set, there's no
difference, that can easily tell by looking into the source code.
【在 g**e 的大作中提到】
: volatile is actually write safe. but its value to write doesn't depend on
: the current value.
: In your example, i++ or i+=1 is not thread safe, because this actually
: involves reading the current value, incrementing it, then setting the new
: value.
very handy for a counter. If it's just get and set, there's no
difference, that can easily tell by looking into the source code.
【在 g**e 的大作中提到】
: volatile is actually write safe. but its value to write doesn't depend on
: the current value.
: In your example, i++ or i+=1 is not thread safe, because this actually
: involves reading the current value, incrementing it, then setting the new
: value.
相关阅读
推荐一个CFX web services入门教程java perf 书请问关于top level Apache project (转载)为啥logger.info print 出来的时间超前?Design question --- hibernateAmazon.com is hiring in Seattle WA- Software Engineers/Senior Software Engineers哪里用NoSQL比较合适?Senior Java Architect / Developer有没有人想做点小project的 (转载)JAVA is better?最近我在看不少数学大牛的网页,发现@Override annotation.CINAOUG 2012年1月数据库讲座 (转载)java ,wait ,notify notifyalljava string stream请教个garbage collector问题求助: ldap的ssl connection我现在把demo制作过程给写上去了 (转载)find all tables used in a hibernate/jdbc project?javaFX谁有经验?