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.
相关阅读
关于Runtime.getRuntime().exec的问题Re: Out of memory, Java heap space 的问题怎么解决? (转载)Java虚拟磁盘问题Classpath questions[合集] How to remove absolute path in eclipse project file想给程序加个最简单的窗口界面,请帮忙另一个入门问题。问一下关于load image的servlet[合集] Are JSPs Dead?也来问一个Runtime.getRuntime().exec的问题想找几个模板[合集] 帮我看看这个问题出在哪。HTTP Status 404 The requested resource (/abc/index.jsp) is not available.Java Reflection Performance 测验问个deploy相关的问题回国之后Eclipse的CDT里如何生成tag? (转载)EJB book请问同时执行几个bat文件的问题如何安装Geronimo?