1. Last week I blogged about "Better AtomicInteger and AtomicLong in Java" and how I added it to GridGain 'lang.utils' package to be used within GridGain project and by our user base. Recently I made it even better. The standard JDK atomic 'compareAndSet(..)' operations always check for some 'expected' value and if it matches, then the set happens. What if you simply want to set a value if its greater or less than current value, or if it simply does not match the current value?

    Again, code looks pretty simple:
    public class MyAtomicInteger extends AtomicInteger {
      ... // Omitting methods from previous blog
      public boolean setIfGreater(int update) {         
         while (true) {             
              int cur = get();
    
              if (update > cur) {
                  if (compareAndSet(cur, update))
                      return true;
              }
              else
                  return false;
          }
      }
    
      public boolean setIfGreaterEquals(int update) {
          while (true) {
              int cur = get();
    
              if (update >= cur) {
                  if (compareAndSet(cur, update))
                      return true;
              }
              else
                  return false;
          }
      }
    
      public boolean setIfLess(int update) {
          while (true) {
              int cur = get();
              
              if (update < cur) {
                  if (compareAndSet(cur, update))
                     return true;
              }
              else
                  return false;
          }
      }      
      
      public boolean setIfLessEquals(int update) {
          while (true) {
              int cur = get();
    
              if (update <= cur) {
                  if (compareAndSet(cur, update))
                      return true;
              }
              else
                  return false;
          }
      }
    
      public boolean setIfNotEquals(int update) {
          while (true) {
            int cur = get();
    
            if (update != cur) {
                if (compareAndSet(cur, update))
                    return true;
            }
            else
                return false;
          }
      }
    }
    
    16

    View comments

  2. I was always annoyed by the fact that AtomicLong.compareAndSet(...) method in Java only checked for exact equality. What if you need to check if the value is less than or greater than? So I finally just created my own AtomicLong and AtomicInteger classes and added them to GridGain utilities to be used by us and by all our users.

    The code is very simple:

    public class MyAtomicLong extends AtomicLong {
    public boolean greaterAndSet(long check, long update) {
    while (true) {
    long cur = get();

    if (check > cur) {
    if (compareAndSet(cur, update))
    return true;
    }
    else
    return false;
    }
    }

    public boolean greaterEqualsAndSet(long check, long update) {
    while (true) {
    long cur = get();

    if (check >= cur) {
    if (compareAndSet(cur, update))
    return true;
    }
    else
    return false;
    }
    }

    public boolean lessAndSet(long check, long update) {
    while (true) {
    long cur = get();

    if (check < cur) {
    if (compareAndSet(cur, update))
    return true;
    }
    else
    return false;
    }
    }

    public boolean lessEqualsAndSet(long check, long update) {
    while (true) {
    long cur = get();

    if (check <= cur) {
    if (compareAndSet(cur, update))
    return true;
    }
    else
    return false;
    }
    }
    }

    Based on the comments I received, I have also added predicate-based 'checkAndSet(...)' method:

    public boolean checkAndSet(GridPredicate<Long> p, long update) {
    while (true) {
    long cur = get();

    if (p.apply(cur)) {
    if (compareAndSet(cur, update))
    return true;
    }
    else
    return false;
    }
    }

    Of course in GridGain these classes are named GridAtomicInteger and GridAtomicLong, since we prefix all our classes with the word Grid.
    10

    View comments

  3. We are migrating and finishing the upcoming GridGain’s book “Pragmatic Cloud Computing with GridGain using Java and Scala”. Here’s a short chapter that I’ve just finished on the subject of:

    “Remote Actors vs. GridGain and Concurrency Unification”

    Enjoy!

    0

    Add a comment





  4. We’ve just released GridGain 3.1.0 – our latest stable release of GridGain’s High Performance Cloud Computing platform that allows anyone easily develop, scale and manage compute and data intensive JVM based applications.

    Some of the key new features and enhancements (combined with 3.0.9 announcement last week):

    • Improvements in stability and performance for Data Grid
    • New Distributed Data Structures:
      • GridCacheAtomicLong
      • GridCacheAtomicReference
      • GridCacheAtomicSequence
      • GridCacheAtomicStamped
      • GridCacheQueue
    • Fully distributed Functional Programming framework for Java
    • Distributed Java closure execution
    • Improved AOP-based distributed execution
    • Improved Data Grid querying support for:
      • Native SQL (including full joints support)
      • Predicate-based Full Scan
      • Lucene
      • H2 text
    • Support for local and remote reduction and transformations in Data Grid queries
    • Improved Scala-based DSL for Compute and Data Grid:
      • Enhanced distributed closure execution with zero deployment
      • Ad-hoc Data Grid queries
      • Interactive GridGain Scala REPL
      • Work with Compute and Data Grid from Scala REPL
    • Dramatically enhanced GridGain Visor for distributed monitoring and management
    • Brand new TCP/IP guaranteed discovery SPI for large cloud-based deployments
    • New enterprise licensing mechanism with dynamic hot-upgradability
    • New optimized marshaling
    • Improved documentation and examples

    GridGain 3.1.0 comes in two editions:

    • Free Community Edition, GPLv3 licensed
    • Enterprise Edition

    Enjoy!

    0

    Add a comment




  5. GridGain will be presenting at Scala Days 2011 on June 2nd, 2011. The event is hosted by Stanford University. The presentation starts at Session 3: Parallelism track at 3:50pm.

    If you are around – stop by to see a non-stop Scala distributed coding. Should be fun!
    0

    Add a comment

About me
About me
- Antoine de Saint-Exupery -
- Antoine de Saint-Exupery -
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
Blog Archive
Blogs I frequent
Loading
Dynamic Views theme. Powered by Blogger.