• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compareAndSet()

 
Greenhorn
Posts: 6
Postgres Database Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but I don't know what does this mean at all and comparing which one to which one? when the success gets false and when does it get true?

 
Saloon Keeper
Posts: 27819
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is basically a high-performance type of synchronized access. Note that AtomicInteger is not the same thing as an Integer nor is there a direct inheritance between them.

On the IBM System/380 mainframe there was an instruction called "compare-and-swap" that operated almost identically to the compareAndSet() method here. A similar instruction appears on many advanced microprocessors.

In the normal course of events, if two separate threads attempted to read and update an Integer, they had the potential to interfere with each other unless access to that integer was declared synchronized. But synchronization is an expensive and time-consuming operation. If you could invoke a compare-and-swap machine instruction instead, the overhead would be very low. And, just as importantly, compare-and-swap often locks the system memory bus for the duration of the time spent retrieving the old value and replacing it with the new one. Which is to say, it's an interlock not only within a process but between multiple cores/CPUs at the hardware level.

You wouldn't normally use AtomicInteger for something like a bank balance. It's more likely to be used as a semaphore. Most currencies, after all, aren't integral values. But you can use compare-and-swap to lock on a bank balance record via an integer record ID.

In reality, compare-and-swap is intended more for critical system processes than for general application use. General applications can frequently get by with synchronization.
 
Hoshyar Karimi
Greenhorn
Posts: 6
Postgres Database Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. You redirected me to interesting subjects. But still I don't know in which case that comparison returns true and in which case it returns false.
 
Master Rancher
Posts: 4854
74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hoshyar Karimi wrote:Sorry but I don't know what does this mean at all and comparing which one to which one? when the success gets false and when does it get true?


For this it's helpful to consult the Javadoc for the method in question, AtomicInteger.compareAndSet():

public final boolean compareAndSet(int expectedValue, int newValue)

Atomically sets the value to newValue if the current value == expectedValue


Basically, it's only going to set newValue if "this" (the AtomicInteger instance) currently has a value equal to expectedValue.  The idea is, we know we just read that value, and we expect that it hasn't changed.  But in case any other threads are accessing that value at the same time.  So if the value in the AtomicInteger has changed due to another thread, we don't want to set it now, because that would mean we're overwriting whatever the other thread did, which would be rude, and a bug.  Normally this should be done inside a loop, so if you fail the first time, you keep trying until it works.  Eventually you should be able to read the value and change it without another thread intervening.

In this code, you are specifically taking the int value inide the AtomicInteger object called "balance", and comparing it to the int value "expectedValue".  If they are the same, the compareAndSet will succeed and return true.  If they are different, it will fail and return false.
 
Hoshyar Karimi
Greenhorn
Posts: 6
Postgres Database Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mike. Now I got that
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic