java.util.concurrent.atomic
Class AtomicInteger

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.lang.Number sample code for java.lang.Number definition code for java.lang.Number 
      extended by java.util.concurrent.atomic.AtomicInteger
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable

public class AtomicInteger
extends Number sample code for java.lang.Number definition code for java.lang.Number
implements Serializable sample code for java.io.Serializable definition code for java.io.Serializable

An int value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer sample code for java.lang.Integer definition code for java.lang.Integer . However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.

Since:
1.5
See Also:
Serialized Form

Constructor Summary
AtomicInteger sample code for java.util.concurrent.atomic.AtomicInteger.AtomicInteger() definition code for java.util.concurrent.atomic.AtomicInteger.AtomicInteger() ()
          Create a new AtomicInteger with initial value 0.
AtomicInteger sample code for java.util.concurrent.atomic.AtomicInteger.AtomicInteger(int) definition code for java.util.concurrent.atomic.AtomicInteger.AtomicInteger(int) (int initialValue)
          Create a new AtomicInteger with the given initial value.
 
Method Summary
 int addAndGet sample code for java.util.concurrent.atomic.AtomicInteger.addAndGet(int) definition code for java.util.concurrent.atomic.AtomicInteger.addAndGet(int) (int delta)
          Atomically add the given value to current value.
 boolean compareAndSet sample code for java.util.concurrent.atomic.AtomicInteger.compareAndSet(int, int) definition code for java.util.concurrent.atomic.AtomicInteger.compareAndSet(int, int) (int expect, int update)
          Atomically set the value to the given updated value if the current value == the expected value.
 int decrementAndGet sample code for java.util.concurrent.atomic.AtomicInteger.decrementAndGet() definition code for java.util.concurrent.atomic.AtomicInteger.decrementAndGet() ()
          Atomically decrement by one the current value.
 double doubleValue sample code for java.util.concurrent.atomic.AtomicInteger.doubleValue() definition code for java.util.concurrent.atomic.AtomicInteger.doubleValue() ()
          Returns the value of the specified number as a double.
 float floatValue sample code for java.util.concurrent.atomic.AtomicInteger.floatValue() definition code for java.util.concurrent.atomic.AtomicInteger.floatValue() ()
          Returns the value of the specified number as a float.
 int get sample code for java.util.concurrent.atomic.AtomicInteger.get() definition code for java.util.concurrent.atomic.AtomicInteger.get() ()
          Get the current value.
 int getAndAdd sample code for java.util.concurrent.atomic.AtomicInteger.getAndAdd(int) definition code for java.util.concurrent.atomic.AtomicInteger.getAndAdd(int) (int delta)
          Atomically add the given value to current value.
 int getAndDecrement sample code for java.util.concurrent.atomic.AtomicInteger.getAndDecrement() definition code for java.util.concurrent.atomic.AtomicInteger.getAndDecrement() ()
          Atomically decrement by one the current value.
 int getAndIncrement sample code for java.util.concurrent.atomic.AtomicInteger.getAndIncrement() definition code for java.util.concurrent.atomic.AtomicInteger.getAndIncrement() ()
          Atomically increment by one the current value.
 int getAndSet sample code for java.util.concurrent.atomic.AtomicInteger.getAndSet(int) definition code for java.util.concurrent.atomic.AtomicInteger.getAndSet(int) (int newValue)
          Set to the give value and return the old value.
 int incrementAndGet sample code for java.util.concurrent.atomic.AtomicInteger.incrementAndGet() definition code for java.util.concurrent.atomic.AtomicInteger.incrementAndGet() ()
          Atomically increment by one the current value.
 int intValue sample code for java.util.concurrent.atomic.AtomicInteger.intValue() definition code for java.util.concurrent.atomic.AtomicInteger.intValue() ()
          Returns the value of the specified number as an int.
 long longValue sample code for java.util.concurrent.atomic.AtomicInteger.longValue() definition code for java.util.concurrent.atomic.AtomicInteger.longValue() ()
          Returns the value of the specified number as a long.
 void set sample code for java.util.concurrent.atomic.AtomicInteger.set(int) definition code for java.util.concurrent.atomic.AtomicInteger.set(int) (int newValue)
          Set to the given value.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.util.concurrent.atomic.AtomicInteger.toString() definition code for java.util.concurrent.atomic.AtomicInteger.toString() ()
          Returns the String representation of the current value.
 boolean weakCompareAndSet sample code for java.util.concurrent.atomic.AtomicInteger.weakCompareAndSet(int, int) definition code for java.util.concurrent.atomic.AtomicInteger.weakCompareAndSet(int, int) (int expect, int update)
          Atomically set the value to the given updated value if the current value == the expected value.
 
Methods inherited from class java.lang.Number sample code for java.lang.Number definition code for java.lang.Number
byteValue sample code for java.lang.Number.byteValue() definition code for java.lang.Number.byteValue() , shortValue sample code for java.lang.Number.shortValue() definition code for java.lang.Number.shortValue()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

AtomicInteger sample code for java.util.concurrent.atomic.AtomicInteger(int) definition code for java.util.concurrent.atomic.AtomicInteger(int)

public AtomicInteger(int initialValue)
Create a new AtomicInteger with the given initial value.

Parameters:
initialValue - the initial value

AtomicInteger sample code for java.util.concurrent.atomic.AtomicInteger() definition code for java.util.concurrent.atomic.AtomicInteger()

public AtomicInteger()
Create a new AtomicInteger with initial value 0.

Method Detail

get sample code for java.util.concurrent.atomic.AtomicInteger.get() definition code for java.util.concurrent.atomic.AtomicInteger.get()

public final int get()
Get the current value.

Returns:
the current value

set sample code for java.util.concurrent.atomic.AtomicInteger.set(int) definition code for java.util.concurrent.atomic.AtomicInteger.set(int)

public final void set(int newValue)
Set to the given value.

Parameters:
newValue - the new value

getAndSet sample code for java.util.concurrent.atomic.AtomicInteger.getAndSet(int) definition code for java.util.concurrent.atomic.AtomicInteger.getAndSet(int)

public final int getAndSet(int newValue)
Set to the give value and return the old value.

Parameters:
newValue - the new value
Returns:
the previous value

compareAndSet sample code for java.util.concurrent.atomic.AtomicInteger.compareAndSet(int, int) definition code for java.util.concurrent.atomic.AtomicInteger.compareAndSet(int, int)

public final boolean compareAndSet(int expect,
                                   int update)
Atomically set the value to the given updated value if the current value == the expected value.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful. False return indicates that the actual value was not equal to the expected value.

weakCompareAndSet sample code for java.util.concurrent.atomic.AtomicInteger.weakCompareAndSet(int, int) definition code for java.util.concurrent.atomic.AtomicInteger.weakCompareAndSet(int, int)

public final boolean weakCompareAndSet(int expect,
                                       int update)
Atomically set the value to the given updated value if the current value == the expected value. May fail spuriously.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful.

getAndIncrement sample code for java.util.concurrent.atomic.AtomicInteger.getAndIncrement() definition code for java.util.concurrent.atomic.AtomicInteger.getAndIncrement()

public final int getAndIncrement()
Atomically increment by one the current value.

Returns:
the previous value

getAndDecrement sample code for java.util.concurrent.atomic.AtomicInteger.getAndDecrement() definition code for java.util.concurrent.atomic.AtomicInteger.getAndDecrement()

public final int getAndDecrement()
Atomically decrement by one the current value.

Returns:
the previous value

getAndAdd sample code for java.util.concurrent.atomic.AtomicInteger.getAndAdd(int) definition code for java.util.concurrent.atomic.AtomicInteger.getAndAdd(int)

public final int getAndAdd(int delta)
Atomically add the given value to current value.

Parameters:
delta - the value to add
Returns:
the previous value

incrementAndGet sample code for java.util.concurrent.atomic.AtomicInteger.incrementAndGet() definition code for java.util.concurrent.atomic.AtomicInteger.incrementAndGet()

public final int incrementAndGet()
Atomically increment by one the current value.

Returns:
the updated value

decrementAndGet sample code for java.util.concurrent.atomic.AtomicInteger.decrementAndGet() definition code for java.util.concurrent.atomic.AtomicInteger.decrementAndGet()

public final int decrementAndGet()
Atomically decrement by one the current value.

Returns:
the updated value

addAndGet sample code for java.util.concurrent.atomic.AtomicInteger.addAndGet(int) definition code for java.util.concurrent.atomic.AtomicInteger.addAndGet(int)

public final int addAndGet(int delta)
Atomically add the given value to current value.

Parameters:
delta - the value to add
Returns:
the updated value

toString sample code for java.util.concurrent.atomic.AtomicInteger.toString() definition code for java.util.concurrent.atomic.AtomicInteger.toString()

public String sample code for java.lang.String definition code for java.lang.String  toString()
Returns the String representation of the current value.

Overrides:
toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
the String representation of the current value.

intValue sample code for java.util.concurrent.atomic.AtomicInteger.intValue() definition code for java.util.concurrent.atomic.AtomicInteger.intValue()

public int intValue()
Description copied from class: Number sample code for java.lang.Number.intValue() definition code for java.lang.Number.intValue()
Returns the value of the specified number as an int. This may involve rounding or truncation.

Specified by:
intValue sample code for java.lang.Number.intValue() definition code for java.lang.Number.intValue() in class Number sample code for java.lang.Number definition code for java.lang.Number
Returns:
the numeric value represented by this object after conversion to type int.

longValue sample code for java.util.concurrent.atomic.AtomicInteger.longValue() definition code for java.util.concurrent.atomic.AtomicInteger.longValue()

public long longValue()
Description copied from class: Number sample code for java.lang.Number.longValue() definition code for java.lang.Number.longValue()
Returns the value of the specified number as a long. This may involve rounding or truncation.

Specified by:
longValue sample code for java.lang.Number.longValue() definition code for java.lang.Number.longValue() in class Number sample code for java.lang.Number definition code for java.lang.Number
Returns:
the numeric value represented by this object after conversion to type long.

floatValue sample code for java.util.concurrent.atomic.AtomicInteger.floatValue() definition code for java.util.concurrent.atomic.AtomicInteger.floatValue()

public float floatValue()
Description copied from class: Number sample code for java.lang.Number.floatValue() definition code for java.lang.Number.floatValue()
Returns the value of the specified number as a float. This may involve rounding.

Specified by:
floatValue sample code for java.lang.Number.floatValue() definition code for java.lang.Number.floatValue() in class Number sample code for java.lang.Number definition code for java.lang.Number
Returns:
the numeric value represented by this object after conversion to type float.

doubleValue sample code for java.util.concurrent.atomic.AtomicInteger.doubleValue() definition code for java.util.concurrent.atomic.AtomicInteger.doubleValue()

public double doubleValue()
Description copied from class: Number sample code for java.lang.Number.doubleValue() definition code for java.lang.Number.doubleValue()
Returns the value of the specified number as a double. This may involve rounding.

Specified by:
doubleValue sample code for java.lang.Number.doubleValue() definition code for java.lang.Number.doubleValue() in class Number sample code for java.lang.Number definition code for java.lang.Number
Returns:
the numeric value represented by this object after conversion to type double.