Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Atomic methods v/s snycronization methods  RSS feed

 
Anish Shah
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I read quite a few articles before posting this topic.
Got few question..

Ok let me try to ask question in a different way..

Below is a class in which if i create new instance of this class say SC then at a time only 1 synchronized method can run i.e if both incrementC and incrementB are called simultaneoulsy then one of them would have to wait.








So by have an integer Atomic, it means that a a particular instant of AtomicCounter can have two methods operate simulatneously like incrementC and
incrementB as they both are unsynchronized and also would do the work of incrementingC and B.

Thanks everyone
[ October 24, 2008: Message edited by: Anish Shah ]
 
Henry Wong
author
Sheriff
Posts: 22840
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So by have an integer Atomic, it means that a a particular instant of AtomicCounter can have two methods operate simulatneously like incrementC and
incrementB as they both are unsynchronized and also would do the work of incrementingC and B.


Not quite. The methods are indeed unsynchronized. But there is a penalty if the two methods "operate sumultaneously". If they collide -- meaning they operate to the point that one steps on the other -- one of the operations will have to retry.

From your point of view, you don't see it, but if a collision occur, the increment and decrement will take longer due to a retry.

Henry
 
Anish Shah
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for th reply,

Could they both step over each other even if they are incrementing different variables ??

Can they both step over each other like on incrementAndGet() ?? How would they collide if they are incrementing different variables.

Thanks
 
Henry Wong
author
Sheriff
Posts: 22840
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could they both step over each other even if they are incrementing different variables ??


Of course not. They have to be changing the same variable at the exact same time, to even have a chance at colliding with each other.

Can they both step over each other like on incrementAndGet() ?? How would they collide if they are incrementing different variables.


For different variables, they can't. However, for different variables, you don't need to synchronize either (if synchronization is used).


Having said this, the purpose of synchronized and atomic variables, isn't to allow B and C to be incremented in a thread safe manner. It is to allow multiple threads to simultaneously increment B (or C) (ie. the same variable) in a thread safe manner.

Henry
[ October 24, 2008: Message edited by: Henry Wong ]
 
Henry Wong
author
Sheriff
Posts: 22840
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can they both step over each other like on incrementAndGet() ??


The incrementAndGet() method is actually a call to the get() and the compareAndSet() method. A get() is used to get the original value. The value is incremented by one. Then a call to compareAndSet() is used to replace the original value with the incremented one.

If a collision occured, then the original value would have changed, and the compareAndSet() would fail. This would cause a retry to call get() and compareAndSet() again.

So... to answer your question. Both threads can't step on each other. Only one will be stepped on (if any). The one that does the stepping will get through.

Henry
[ October 24, 2008: Message edited by: Henry Wong ]
 
Anish Shah
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

So if two different threads going through two different methods and incrementing two different variables (Atomic variables) would never collide with each other ??

Let me give a scenario,
Multiple threads are running. Multiple threads can call on either incrementA or incrementB. both increment different variables. so in this scenario if both Atomic varibales b and c can run simultaneously without colliding or waiting on each other (My concepts are weak down here a bit. Wouldn't they have to wait on incrementAndGet. But its unsnchronized method, i hope so not)


In other scenario my both same methods are synchronized, hence they wait on other method to complete as only 1 synchronized method can run at a particular instant in an object which is not the case with AtomicInteger example. ??

I hope i didn't confuse ..


THANK YOU...
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just another cross poster.
http://forums.sun.com/thread.jspa?threadID=5342484
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anish:
So if two different threads going through two different methods and incrementing two different variables (Atomic variables) would never collide with each other ??


Collision happens when two people are going on the same path.
Where is the *same* path here? Two threads are doing two different things.

You are confusing between two concepts.
Atomic classes does not guarantee that at any point no 2 threads will concurrently execute any method.
What they guarantee is that even if more than one threads are concurrently modifying an atomic object, the result will be as expected i.e. if you wanted to increment by 1 then at the end of the operation, the value will be incremented by 1.
However, what it does not say is that it will be incremented by 1 over the value you saw before calling the method.
For example,
if you do something like this:


and expect the value of c to be 1 after the if condition, it will not always be true.

As Henry told, the atomic classes are written using the CAS(compare and set) instruction. This instruction is a processor supported instruction that assures that the value of a reference will be set only if the existing value is as expected. This instruction is guaranteed to be atomic.

incrementAndGet() is implemented to repeat the cas operation in an infinite loop till the time the existing value is as expected. This takes time if concurrently another thread modifies the value and hence the expected and actual value differs. This is what Henry termed as collission.

Also, as Darryl has pointed out, it seems that you have posted the same post in the sun forum. Please BeForthrightWhenCrossPostingToOtherSites
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!