• Post Reply Bookmark Topic Watch Topic
  • New Topic

Aomic Integer  RSS feed

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was reading the concurrency from Sun tutorial, I don't find it much useful, Whatever it provides we can achieve the same through Synchronization, I think using sync is much easier to understand, Can anyone tell me..in which condition i should use Atomic integer rather than Synchronization ?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sunny Jain:
Hi,
I was reading the concurrency from Sun tutorial, I don't find it much useful, Whatever it provides we can achieve the same through Synchronization, I think using sync is much easier to understand, Can anyone tell me..in which condition i should use Atomic integer rather than Synchronization ?


AtomicInteger class uses the hardware support to atomically compare and set values to a memory location. This means the value is set only if the existing value is equal to the expected value. This process does not need any synchronization so many threads can execute the same code concurrently. (However, the javadoc says that in some processors there may be some kind of locking.)
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The atomic libraries allow for "optimistic locking". While it is true that you "can achieve the same through Synchronization", and that "using sync is much easier to understand", java synchronization is "pessemistic locking".

Henry
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I understand, in some situations using atomic variables can lead to much better performance than using synchronization.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I agree with you, But I am unable to understand the following piece of code:


Here atomic_a and atomic_b are atomic integer but aa and bb are simple integer, four threads are running, after every thread finishes i am printing the value of atomic_a and aa, as I understand aa should give the unexpected result, but why atomic_a is giving result as 4 always.
all aa,bb,atomic_a,atomic_b are instance variable.

Please help me to understand that.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunny:
why atomic_a is giving result as 4 always.


Every thread increments the variable twice and decrements it once. So, in all with 4 threads the variable gets increment 8 times and decrement 4 times. If the initial value was 0, then the final value will be 4.
I do not see anything wrong in the above. Do you have other ideas?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, It is getting incremented 8 times and decremented 4 times. But this is possible only if every atomic statement execute 4 times before the next statement. both atomic and normal integer are getting incremented 8 times and decremented 4 times, but the sequence is in which they increment or decrement is of more important.about normal integer i can understand.

I tried to put some log in the method, What I saw is:
for eg:
atomic_b.incrementAndGet(); //line 2
atomic_b.decrementAndGet(); //line 3

line 2 is executed exactly 4 times before line 3 get executed. In most of the case in the order in which line 2 was executer was preserved, but that was not the case always, but always one line is executed by each thread before the next line.assume all line atomic operation.
I am just confuse how all this happening?
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 2 is executed exactly 4 times before line 3 get executed. In most of the case in the order in which line 2 was executer was preserved, but that was not the case always, but always one line is executed by each thread before the next line.assume all line atomic operation.
I am just confuse how all this happening?


Huh??? Could you elaborate your question? I must admit that I read it quite a few times, and still don't get what you are asking.

Henry
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Let me put all these in straight forward words:
following two code will performs equally, Then why should i go for atomic integer rather than simple integer, as far as i think, atomic integer is the Object and normal int is primitive so primitive is lighter on JVM rather than Object.




Suppose two thread access this method, possible value to be printed are :
case 1 :
2
4
case 2:
3
4
case 3:
4
4

This is equally applicable to both, then what is the benefit of atomic..?

If I am having a wrong concept about atomic please help me to know with an example please, under which situation atomic is better than normal ?
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
following two code will performs equally, Then why should i go for atomic integer rather than simple integer, as far as i think, atomic integer is the Object and normal int is primitive so primitive is lighter on JVM rather than Object.


Just because code performs consistently in your program, doesn't mean the the operations are atomic.

In Java (with integers), only a store and load is guaranteed to be atomic -- there is also the cache, blah blah blah, volatile variables, etc. but let's not worry about that here.

This means that when two processors are trying to write to the same variable, it is guaranteed to be one or the other, and not a combination of both. This means that when one processor is loading, while another is storing, the loaded value is guaranteed to be either the before or after value, not a combination of both.

The increment operator is not atomic -- it consists of a load, a register inc, and a store. This means that when two processors are trying to increment the same variable, it is possible for both to load the same value, both to inc, and then both store the same value back. ie. it is incremented only once.


With the atomic class, the increment() methods are guaranteed to be atomic, without any need for synchronization. It is implemented with a JNI call to a CAS operator. Upon CAS collision, it will simply retry the operation. This may not seem like much, but with the CAS operator exposed via the compareAndSet() method, optimistic locking can be done. You can implement a very complex operation (not just increment) without the need to synchronize.

Henry
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a small sample I've made to illustrate (correctly I hope) the difference between incrementing a primitive and incrementing an Atomic variable.



I've run this three times :

atomic99[100000]
atomic98[100000]
atomic99[100000]

normal91[89722]
normal97[98623]
normal97[99321]

With an Atomic variable, I am guaranteed that that counter will be incremented once, and that no other thread will increment at the same time. Using a primitive, the counter does not make it up to 100000, so some threads incremented it simultaneously.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!