Java.lang.Object
Java.util.concurrent.atomic.AtomicBoolean
Inherit from object.
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
- private static class Barworker implements Runnable {
- private static Boolean exists = false;
- private String name;
- Public Barworker (String name) {this. name = name; }
- 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"); } }
- }
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
- private static class Barworker implements Runnable {
- private static Boolean exists = false;
- private String name;
- Public Barworker (String name) {
- this. Name = name;
- }
- public void Run () {
- if (!exists) {
- try {
- TimeUnit.SECONDS.sleep (1);
- } catch (Interruptedexception E1) {
- Do nothing
- }
- exists = true;
- SYSTEM.OUT.PRINTLN (name + "enter");
- try {
- SYSTEM.OUT.PRINTLN (name + "working");
- TimeUnit.SECONDS.sleep (2);
- } catch (Interruptedexception e) {
- Do nothing
- }
- SYSTEM.OUT.PRINTLN (name + "Leave");
- exists = false;
- } else {
- SYSTEM.OUT.PRINTLN (name + "Give Up");
- }
- }
- }
[Java] view plain copy
- private static class Barworker implements Runnable {
- private static Boolean exists = false;
- private String name;
- Public Barworker (String name) {
- this. Name = name;
- }
- public void Run () {
- if (!exists) {
- try {
- TimeUnit.SECONDS.sleep (1);
- } catch (Interruptedexception E1) {
- Do nothing
- }
- exists = true;
- SYSTEM.OUT.PRINTLN (name + "enter");
- try {
- SYSTEM.OUT.PRINTLN (name + "working");
- TimeUnit.SECONDS.sleep (2);
- } catch (Interruptedexception e) {
- Do nothing
- }
- SYSTEM.OUT.PRINTLN (name + "Leave");
- exists = false;
- } else {
- SYSTEM.OUT.PRINTLN (name + "Give Up");
- }
- }
- }
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
- private static class Barworker implements Runnable {
- private static Atomicboolean exists = new Atomicboolean (false);
- private String name;
- Public Barworker (String name) {
- this. Name = name;
- }
- public void Run () {
- if (Exists.compareandset (False, True)) {
- SYSTEM.OUT.PRINTLN (name + "enter");
- try {
- SYSTEM.OUT.PRINTLN (name + "working");
- TimeUnit.SECONDS.sleep (2);
- } catch (Interruptedexception e) {
- Do nothing
- }
- SYSTEM.OUT.PRINTLN (name + "Leave");
- Exists.set (FALSE);
- } else {
- SYSTEM.OUT.PRINTLN (name + "Give Up");
- }
- }
- }
[Java] view plain copy
- private static class Barworker implements Runnable {
- private static Atomicboolean exists = new Atomicboolean (false);
- private String name;
- Public Barworker (String name) {
- this. Name = name;
- }
- public void Run () {
- if (Exists.compareandset (False, True)) {
- SYSTEM.OUT.PRINTLN (name + "enter");
- try {
- SYSTEM.OUT.PRINTLN (name + "working");
- TimeUnit.SECONDS.sleep (2);
- } catch (Interruptedexception e) {
- Do nothing
- }
- SYSTEM.OUT.PRINTLN (name + "Leave");
- Exists.set (FALSE);
- } else {
- SYSTEM.OUT.PRINTLN (name + "Give Up");
- }
- }
- }
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