Home  >  Article  >  Java  >  AtomicBoolean of Atomic class in java

AtomicBoolean of Atomic class in java

(*-*)浩
(*-*)浩forward
2019-10-15 17:01:352676browse

AtomicBoolean of Atomic class in java

Class

Under the java.util.concurrent.atomic package, there are AtomicBoolean, AtomicInteger, AtomicLong, AtomicReference and other classes. Their basic The characteristic is that in a multi-threaded environment, when executing the methods contained in these class instances, it is exclusive. That is, when a thread enters the method and executes the instructions in it, it will not be interrupted by other threads, and other threads will just spin. The same as the lock, wait until the execution of the method is completed before the JVM selects another thread from the waiting queue to enter.

Example

Taking AtomicBoolean as an example, if a single thread executes an ordinary method (as follows), there will be no threading problem:

package com.secbro.test.atomic;

/**
 * @author zhuzhisheng
 * @Description
 * @date on 2016/5/26.
 */
public class NormalBoolean implements Runnable{

    public static boolean exits = false;

    private String name;

    public NormalBoolean(String name){
        this.name = name;
    }


    @Override
    public void run() {
        if(!exits){
            exits = true;
            System.out.println(name + ",step 1");
            System.out.println(name + ",step 2");
            System.out.println(name + ",step 3");
            exits = false;
        } else {
            System.out.println(name + ",step else");
        }
    }

    public static void main(String[] args) {
        new NormalBoolean("张三").run();
    }
}

However, When multi-threaded execution occurs, when the command after the judgment is executed, other threads will insert and change the value of exits. As shown in the following code snippet:

package com.secbro.test.atomic;
/**
 * @author zhuzhisheng
 * @Description
 * @date on 2016/5/26.
 */
public class NormalBoolean2 implements Runnable{
    public static boolean exits = false;
    private String name;
    public NormalBoolean2(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        if(!exits){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            exits = true;
            System.out.println(name + ",step 1");
            System.out.println(name + ",step 2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + ",step 3");
            exits = false;
        } else {
            System.out.println(name + ",step else");
        }
    }
}

Execute two threads at the same time, and the printed result is:

张三,step 1
李四,step 1
张三,step 2
李四,step 2
张三,step 3
李四,step 3

Now, using AtomicBoolean can ensure safe operation in multi-threaded situations, and only one thread is used for business processing.

package com.secbro.test.atomic;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author zhuzhisheng
 * @Description
 * @date on 2016/5/26.
 */
public class TestAtomicBoolean implements Runnable{

    public static AtomicBoolean exits = new AtomicBoolean(false);

    private String name;

    public TestAtomicBoolean(String name) {
        this.name = name;
    }

    @Override
    public void run() {

        if(exits.compareAndSet(false,true)){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + ",step 1");

            System.out.println(name + ",step 2");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + ",step 3");
            exits.set(false);
        } else {
            System.out.println(name + ",step else");
        }

    }
}

When two threads execute this class, the printed result is as follows:

张三,step else
李四,step 1
李四,step 2
李四,step 3

Test class:

package com.secbro.test.atomic;
/**
 * @author zhuzhisheng
 * @Description
 * @date on 2016/5/26.
 */
public class TestBoolean {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new NormalBoolean2("李四"));
        Thread thread2 = new Thread(new NormalBoolean2("张三"));
        thread1.start();
        thread2.start();
        //-------------------------------------------------------
        Thread thread3 = new Thread(new TestAtomicBoolean("李四"));
        Thread thread4 = new Thread(new TestAtomicBoolean("张三"));
        thread3.start();
        thread4.start();
    }
}

The above is the detailed content of AtomicBoolean of Atomic class in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete