AtomicLong(atomiclong线程安全)

  本篇文章为你整理了AtomicLong(atomiclong线程安全)的详细内容,包含有atomiclong原理 atomiclong线程安全 atomiclong和longadder atomiclong自增 AtomicLong,希望能帮助你了解 AtomicLong。

   The AtomicLong class provides you with a long variable which can be read and written

   atomically, and which also contains advanced atomic operations like compareAndSet(). The AtomicLong

   class is located in the java.util.concurrent.atomic package, so the full class name is

   java.util.concurrent.atomic.AtomicLong . This text describes the version of AtomicLong

   found in Java 8, but the first version was added in Java 5.

   The reasoning behind the AtomicLong design is explained in my Java Concurrency tutorial in the text about

   Compare and Swap.

  
If you want to create an AtomicLong with an initial value, you can do so like this:

  

 

 

  AtomicLong atomicLong = new AtomicLong(123);

  

 

   This example passes a value of 123 as parameter to the AtomicLong contructor,

   which sets the initial value of the AtomicLong instance to 123 .

  
You can get the value of an AtomicLong instance via the get() method.

   Here is an AtomicLong.get() example:

  

 

 

  AtomicLong atomicLong = new AtomicLong(123);

  long theValue = atomicLong.get();

  

 

  
You can set the value of an AtomicLong instance via the set() method.

   Here is an AtomicLong.set() example:

  

 

 

  AtomicLong atomicLong = new AtomicLong(123);

  atomicLong.set(234);

  

 

   This example creates an AtomicLong example with an initial value of 123, and then sets its

   value to 234 in the next line.

  
 

  Compare and Set the AtomicLong Value

   The AtomicLong class also has an atomic compareAndSet() method. This method

   compares the current value of the AtomicLong instance to an expected value, and if the two

   values are equal, sets a new value for the AtomicLong instance. Here is an

   AtomicLong.compareAndSet() example:

  

 

 

  AtomicLong atomicLong = new AtomicLong(123);

  long expectedValue = 123;

  long newValue = 234;

  atomicLong.compareAndSet(expectedValue, newValue);

  

 

   This example first creates an AtomicLong instance with an initial value of 123 .

   Then it compares the value of the AtomicLong to the expected value 123 and

   if they are equal the new value of the AtomicLong becomes 234;

  
The AtomicLong class contains a few methods you can use to add a value to the AtomicLong

   and get its value returned. These methods are:

  
The first method, addAndGet() adds a number to the AtomicLong and returns its value

   after the addition. The second method, getAndAdd() also adds a number to the AtomicLong

   but returns the value the AtomicLong had before the value was added. Which of these two methods

   you should use depends on your use case. Here are two examples:

  
System.out.println(atomicLong.getAndAdd(10));

  System.out.println(atomicLong.addAndGet(10));

  

 

 

   This example will print out the values 0 and 20. First the example gets the value

   of the AtomicLong before adding 10 to. Its value before addition is 0. Then the example

   adds 10 to the AtomicLong and gets the value after the addition. The value is now 20.

   You can also add negative numbers to the AtomicLong via these two methods. The result is

   effectively a subtraction.

   The methods getAndIncrement() and incrementAndGet() works like getAndAdd()

   and addAndGet() but just add 1 to the value of the AtomicLong.

  
 

  Subtracting From the AtomicLong Value

   The AtomicLong class also contains a few methods for subtracting values from the AtomicLong

   value atomically. These methods are:

  
The decrementAndGet() subtracts 1 from the AtomicLong value and returns its value

   after the subtraction. The getAndDecrement() also subtracts 1 from the AtomicLong value

   but returns the value the AtomicLong had before the subtraction.

  
 

  Using an AtomicLong as a Counter in a Lambda Expression

   A Java Lambda Expression cannot contain any member fields, and thus

   they cannot keep any state internally between calls to the lambda expression. However, you can bypass this limitation

   by creating an AtomicLong outside the Lambda Expression and use it from inside the Lambda Expression.

   Here is an example of that:

  

 

 

  import java.util.concurrent.atomic.AtomicLong;

  import java.util.function.Function;

  public class AtomicLongExample {

   public static void main(String[] args) {

   AtomicLong atomicLong = new AtomicLong();

   Function Long, Long myLambda = (input) - {

   long noOfCalls = atomicLong.incrementAndGet();

   System.out.println("Lambda called " + noOfCalls + " times.");

   return input * 2;

   System.out.println(myLambda.apply(1L));

   System.out.println(myLambda.apply(3L));

   System.out.println(myLambda.apply(5L));

  

 

   The output from running the above code look like this:

  

 

 

  Lambda called 1 times.

  Lambda called 2 times.

  Lambda called 3 times.

  

 

  以上就是AtomicLong(atomiclong线程安全)的详细内容,想要了解更多 AtomicLong的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: