由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - AtomicInteger和volatile int有啥区别
相关主题
java ,wait ,notify notifyall[转载] 现在还有什么OS不是THREAD级调度的吗?
请教一个多线程的问题java thread question
Volatile variables do not provide any atomicitythread independent on a single-cpu machine?
新手问个multi-threading关于synchronized和volatile的问题Thread对应的input和output问题
java的volatilemulti-threading guru们 (转载)
问个HttpClient 的问题多少个thread 就算不正常?
现在到底工作有多少需要Java的多线程呢?怎么学multithreading/concurrency?
Re: How to lock a file and detect a thread is over?one multi-threading question
相关话题的讨论汇总
话题: volatile话题: value话题: int话题: write
进入Java版参与讨论
1 (共1页)
g**e
发帖数: 6127
1
除了一个是object一个是premitive type
多线程操作的时候,这两个哪个性能好?
g*****g
发帖数: 34805
2
Take it this way, volatile int, you still need to
sync blocks to do read/write. AtomicInteger you don't.
You can check the latter's source code to see why.

【在 g**e 的大作中提到】
: 除了一个是object一个是premitive type
: 多线程操作的时候,这两个哪个性能好?

h*****0
发帖数: 4889
3
why do we need to sync blocks to read and write on a volatile int?

【在 g*****g 的大作中提到】
: Take it this way, volatile int, you still need to
: sync blocks to do read/write. AtomicInteger you don't.
: You can check the latter's source code to see why.

g**e
发帖数: 6127
4
为啥volatile需要sync?

【在 g*****g 的大作中提到】
: Take it this way, volatile int, you still need to
: sync blocks to do read/write. AtomicInteger you don't.
: You can check the latter's source code to see why.

g*****g
发帖数: 34805
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?
g**e
发帖数: 6127
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.

h*****0
发帖数: 4889
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.

g*****g
发帖数: 34805
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.

1 (共1页)
进入Java版参与讨论
相关主题
one multi-threading questionjava的volatile
Re: native thread 和green thread问个HttpClient 的问题
Re: Client-Server and actionPerformed现在到底工作有多少需要Java的多线程呢?
What are the transaction management benefits of tRe: How to lock a file and detect a thread is over?
java ,wait ,notify notifyall[转载] 现在还有什么OS不是THREAD级调度的吗?
请教一个多线程的问题java thread question
Volatile variables do not provide any atomicitythread independent on a single-cpu machine?
新手问个multi-threading关于synchronized和volatile的问题Thread对应的input和output问题
相关话题的讨论汇总
话题: volatile话题: value话题: int话题: write