线程可见性、数据操作原子性、volatile、原子量、同步锁、可重入锁示例

import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: ZhangHao
 * @Description: ReentrantLock测试相关
 * @Date: 2020/10/15 21:21
 * @Version: 1.0
 */
public class ReentrantLockTest {

    /**
     * 线程可见性:数据修改立刻被其他线程可见
     * 数据操作原子性:不可分割的数据操作
     */

    /**
     * 没有线程可见性,数据操作原子性
     */
    private int i1 = 0;

    @Test
    void test1(){
        for(int j = 0;j < 1000;j++){
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                i1++;
            }).start();
        }

        while (Thread.activeCount() > 2) Thread.yield();
        System.out.println(i1);
    }

    /**
     * 有线程可见性,没有数据操作原子性
     */
    private volatile int i2 = 0;

    @Test
    void test2(){
        for(int j = 0;j < 1000;j++){
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                i2++;
            }).start();
        }

        while (Thread.activeCount() > 2) Thread.yield();
        System.out.println(i2);
    }

    /**
     * Finalizer和main线程存活
     */
    private static volatile int i3 = 0;

    public static void main(String[] args) {
        for(int j = 0;j < 1000;j++){
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                i3++;
            }).start();
        }

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread.getAllStackTraces().entrySet();
        System.out.println(Thread.activeCount());
        /*while (Thread.activeCount() > 2) Thread.yield();*/
        while (Thread.activeCount() > 2) Thread.onSpinWait();
        System.out.println(i3);
    }

    /**
     * AtomicInteger的getAndIncrement()底层是Unsafe的CAS:compareAndSetInt(Object o, long offset, int expected, int x)
     * 有线程可见性,有数据操作原子性
     */
    private AtomicInteger i4 = new AtomicInteger();

    @Test
    void test4(){
        for(int j = 0;j < 1000;j++){
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                i4.getAndIncrement();
            }).start();
        }

        while (Thread.activeCount() > 2) Thread.yield();
        System.out.println(i4);
    }

    /**
     * 同步锁
     * 有线程可见性,有数据操作原子性
     */
    private int i5 = 0;

    @Test
    void test5(){
        for(int j = 0;j < 1000;j++){
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                /*synchronized (ReentrantLockTest.class){
                    i4++;
                }*/
                synchronized (this){
                    i5++;
                }
            }).start();
        }

        while (Thread.activeCount() > 2) Thread.yield();
        System.out.println(i5);
    }

    /**
     * 可重入锁
     * 有线程可见性,有数据操作原子性
     */
    private int i6 = 0;

    private Lock lock = new ReentrantLock();

    @Test
    void test6(){
        for(int j = 0;j < 1000;j++){
            new Thread(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                lock.lock();
                i6++;
                lock.unlock();
            }).start();
        }

        while (Thread.activeCount() > 2) Thread.yield();
        System.out.println(i6);
    }
}

猜你喜欢

转载自blog.csdn.net/haoranhaoshi/article/details/109112211