Atomicboolean Introduction and use

Source: Internet
Author: User

Java.lang.Object      

Java.util.concurrent.atomic.AtomicBoolean
Inherit from object.
    • Introduced:

When this boolean value is changed, it is not allowed to insert between, preserving the atomicity of the operation

    • Methods and examples
    • Compareandset (Boolean expect, Boolean update)
This method mainly consists of two functions 1. Compares the values of Atomicboolean and expect, if consistent, executes the statements within the method. is actually an if statement 2. Setting the value of the Atomicboolean to update the most thing is that the two things are one go, this will not be interrupted between the actions, any internal or external statements will not be able to run between two actions. Provides a solution for multi-threaded control.

[Java] view plain copy

  1. private static class Barworker implements Runnable {
  2. private static Boolean exists = false;
  3. private String name;
  4. Public Barworker (String name) {this. name = name; }
  5. public void Run () {if (!exists) {exists = true;    SYSTEM.OUT.PRINTLN (name + "enter");    SYSTEM.OUT.PRINTLN (name + "working");    SYSTEM.OUT.PRINTLN (name + "Leave");   exists = false;   } else {System.out.println (name + "Give Up"); }  }
  6. }

The static variable exists is used to implement only one worker at a time at work. But suppose exists's judgment and exists = true; there are other instructions. Java Code

[Java] view plain copy

  1. private static class Barworker implements Runnable {
  2. private static Boolean exists = false;
  3. private String name;
  4. Public Barworker (String name) {
  5. this. Name = name;
  6. }
  7. public void Run () {
  8. if (!exists) {
  9. try {
  10. TimeUnit.SECONDS.sleep (1);
  11. } catch (Interruptedexception E1) {
  12. Do nothing
  13. }
  14. exists = true;
  15. SYSTEM.OUT.PRINTLN (name + "enter");
  16. try {
  17. SYSTEM.OUT.PRINTLN (name + "working");
  18. TimeUnit.SECONDS.sleep (2);
  19. } catch (Interruptedexception e) {
  20. Do nothing
  21. }
  22. SYSTEM.OUT.PRINTLN (name + "Leave");
  23. exists = false;
  24. } else {
  25. SYSTEM.OUT.PRINTLN (name + "Give Up");
  26. }
  27. }
  28. }

[Java] view plain copy

  1. private static class Barworker implements Runnable {
  2. private static Boolean exists = false;
  3. private String name;
  4. Public Barworker (String name) {
  5. this. Name = name;
  6. }
  7. public void Run () {
  8. if (!exists) {
  9. try {
  10. TimeUnit.SECONDS.sleep (1);
  11. } catch (Interruptedexception E1) {
  12. Do nothing
  13. }
  14. exists = true;
  15. SYSTEM.OUT.PRINTLN (name + "enter");
  16. try {
  17. SYSTEM.OUT.PRINTLN (name + "working");
  18. TimeUnit.SECONDS.sleep (2);
  19. } catch (Interruptedexception e) {
  20. Do nothing
  21. }
  22. SYSTEM.OUT.PRINTLN (name + "Leave");
  23. exists = false;
  24. } else {
  25. SYSTEM.OUT.PRINTLN (name + "Give Up");
  26. }
  27. }
  28. }

At this point the output is bar2 enter bar2 working bar1 enter bar1 working bar1 leave bar2 leave see two threads working at the same time. In this case, you can use Atomicboolean Java code

[Java] view plain copy

  1. private static class Barworker implements Runnable {
  2. private static Atomicboolean exists = new Atomicboolean (false);
  3. private String name;
  4. Public Barworker (String name) {
  5. this. Name = name;
  6. }
  7. public void Run () {
  8. if (Exists.compareandset (False, True)) {
  9. SYSTEM.OUT.PRINTLN (name + "enter");
  10. try {
  11. SYSTEM.OUT.PRINTLN (name + "working");
  12. TimeUnit.SECONDS.sleep (2);
  13. } catch (Interruptedexception e) {
  14. Do nothing
  15. }
  16. SYSTEM.OUT.PRINTLN (name + "Leave");
  17. Exists.set (FALSE);
  18. } else {
  19. SYSTEM.OUT.PRINTLN (name + "Give Up");
  20. }
  21. }
  22. }

[Java] view plain copy

  1. private static class Barworker implements Runnable {
  2. private static Atomicboolean exists = new Atomicboolean (false);
  3. private String name;
  4. Public Barworker (String name) {
  5. this. Name = name;
  6. }
  7. public void Run () {
  8. if (Exists.compareandset (False, True)) {
  9. SYSTEM.OUT.PRINTLN (name + "enter");
  10. try {
  11. SYSTEM.OUT.PRINTLN (name + "working");
  12. TimeUnit.SECONDS.sleep (2);
  13. } catch (Interruptedexception e) {
  14. Do nothing
  15. }
  16. SYSTEM.OUT.PRINTLN (name + "Leave");
  17. Exists.set (FALSE);
  18. } else {
  19. SYSTEM.OUT.PRINTLN (name + "Give Up");
  20. }
  21. }
  22. }


Because it provides atomic operations, where Exists.compareandset (false, true) This operation makes the comparison and assignment operations an atomic operation, which does not provide an opportunity. Output is BAR1 enter bar1 working bar2 give Up

Atomicboolean Introduction and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.