JAVA并发编程-线程安全性

线程安全性:当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些线程如何交替执行,并且在主调代码中不需要任何额外的同步或协同,各个类都能表现出正确的行为,那么称这个类是线程安全的。

1.原子性:提供了互斥访问,同一时刻只能有一个线程对它进行操作

使用int类型,共5000个线程,每次允许200个线程同时进行。不能确保每次结果都为5000。

package com.fukexin.concurrency.example.count;


import com.fukexin.concurrency.annoations.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
@NotThreadSafe
public class CountExample1 {

    public static int clientTotal = 5000;
    public static int threadTotal = 200;
    public static int count = 0;

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for(int i = 0;i < clientTotal;i++)
        {
            executorService.execute(()->{
                try {
                    semaphore.acquire();
                    add();
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    log.error("exception:{}",e);
                }
                countDownLatch.countDown();
        });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("count:{}",count);
    }
    public static void add()
    {
        count++;
    }
}
11:47:06.027 [main] INFO com.fukexin.concurrency.example.count.CountExample1 - count:4999

使用原子类AtomicInteger代替int,能够保证每次结果都为5000。

package com.fukexin.concurrency.example.count;

import com.fukexin.concurrency.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@ThreadSafe
public class CountExample2 {
    public static int clientTotal = 5000;
    public static int threadTotal = 200;
    public static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for(int i = 0;i < clientTotal;i++)
        {
            executorService.execute(()->{
                try {
                    semaphore.acquire();
                    add();
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    log.error("exception:{}",e);
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("count:{}",count.get());
    }
    public static void add()
    {
        count.incrementAndGet();
    }
}
11:47:58.942 [main] INFO com.fukexin.concurrency.example.count.CountExample2 - count:5000

原理分析:

count.incrementAndGet()调用U.getAndAddInt(this, VALUE, 1) + 1,其中U时Unsafe类的一个实例

public final native boolean compareAndSetInt(Object var1, long var2, int var4, int var5)是一个底层方法,
  public final int getAndAddInt(Object o, long offset, int delta) {
        int v;
        do {
            v = this.getIntVolatile(o, offset);
        } while(!this.weakCompareAndSetInt(o, offset, v, v + delta));

        return v;
    }

以2+1=3为例:

o当前的对象 count 

offset 当前的值2

delta 为增量即1

v为调用底层方法得到底层当前主内存的值

weakCompareAndSetInt(o, offset, v, v + delta)

当有一个线程时当前值(工作内存)为2,从底层(主内存)获得的值也为2,通过比较两者相等,则允许更新为3,否则重新取出从底层获取当前值和当前值进行比较结果相同才进行更新操作。即期望的值与底层值相同才进行更新操作。

 

AtomicIntegerFieldUpdater

原子性的更新某一个类的字段,该字段必须要用volatile修饰,并且不能被static修饰

package com.fukexin.concurrency.example.atomic;

import com.fukexin.concurrency.annoations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;


@Slf4j
@ThreadSafe
public class AtomicExample5 {

    private static AtomicIntegerFieldUpdater<AtomicExample5> updater = AtomicIntegerFieldUpdater.newUpdater(AtomicExample5.class,"count");

    @Getter
    public volatile int count=100;


    public static void main(String[] args) {

        AtomicExample5 atomicExample5 = new AtomicExample5();

        if(updater.compareAndSet(atomicExample5,100,120))
        {
            log.info("update success1:{}",atomicExample5.getCount());
        }

        if(updater.compareAndSet(atomicExample5,100,120))
        {
            log.info("update success2:{}",atomicExample5.getCount());
        }
        else
        {
            log.info("update failed:{}",atomicExample5.getCount());
        }
    }
}
12:30:42.000 [main] INFO com.fukexin.concurrency.example.atomic.AtomicExample5 - update success1:120
12:30:42.005 [main] INFO com.fukexin.concurrency.example.atomic.AtomicExample5 - update failed:120

AtomicStampReference

解决了CAS的ABA问题,比如一个线程将A改为B后又改为A,通过一个版本变化来标记变量变换。

锁:

synchronized关键字 依赖jvm实现

修饰代码块,作用于调用的对象

package com.fukexin.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedExample1 {


    public void test1()
    {
        synchronized (this)
        {
            for(int i = 0;i<5;i++)
            {
                log.info("test1-{}",i);
            }
        }
    }


    public static void main(String[] args) {
        SynchronizedExample1 synchronizedExample1 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            synchronizedExample1.test1();
        });
        executorService.execute(()->{
            synchronizedExample1.test1();
        });
    }
}
15:17:24.028 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-0
15:17:24.033 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-1
15:17:24.033 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-2
15:17:24.033 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-3
15:17:24.033 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-4
15:17:24.033 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-0
15:17:24.033 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-1
15:17:24.033 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-2
15:17:24.033 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-3
15:17:24.033 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test1-4

 

同一个对象调用同一个方法,先执行test1再执行test1

修饰方法,作用于调用的对象

package com.fukexin.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedExample1 {


    public synchronized void test2()
    {
        for(int i = 0;i<5;i++)
        {
            log.info("test2-{}",i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample1 synchronizedExample1 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            synchronizedExample1.test2();
        });
        executorService.execute(()->{
            synchronizedExample1.test2();
        });
    }
}
15:20:53.715 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-0
15:20:53.719 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-1
15:20:53.719 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-2
15:20:53.719 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-3
15:20:53.719 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-4
15:20:53.719 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-0
15:20:53.720 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-1
15:20:53.720 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-2
15:20:53.720 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-3
15:20:53.720 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample1 - test2-4

修饰静态方法,作用于这个类

package com.fukexin.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedExample2 {


    public static  synchronized void test2()
    {
        for(int i = 0;i<5;i++)
        {
            log.info("test2-{}",i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample2 synchronizedExample1 = new SynchronizedExample2();
        SynchronizedExample2 synchronizedExample2 = new SynchronizedExample2();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            synchronizedExample1.test2();
        });
        executorService.execute(()->{
            synchronizedExample1.test2();
        });
    }
}
15:28:09.774 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-0
15:28:09.779 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-1
15:28:09.780 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-2
15:28:09.780 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-3
15:28:09.780 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-4
15:28:09.780 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-0
15:28:09.780 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-1
15:28:09.780 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-2
15:28:09.780 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-3
15:28:09.780 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test2-4

修饰类,作用于这个类

package com.fukexin.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedExample2 {


    public static void test1()
    {
        synchronized (SynchronizedExample2.class)
        {
            for(int i = 0;i<10;i++)
            {
                log.info("test1-{}",i);
            }
        }
    }



    public static void main(String[] args) {
        SynchronizedExample2 synchronizedExample1 = new SynchronizedExample2();
        SynchronizedExample2 synchronizedExample2 = new SynchronizedExample2();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            synchronizedExample1.test1();
        });
        executorService.execute(()->{
            synchronizedExample1.test1();
        });
    }
}
15:29:35.825 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-0
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-1
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-2
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-3
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-4
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-5
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-6
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-7
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-8
15:29:35.832 [pool-1-thread-1] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-9
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-0
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-1
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-2
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-3
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-4
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-5
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-6
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-7
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-8
15:29:35.833 [pool-1-thread-2] INFO com.fukexin.concurrency.example.sync.SynchronizedExample2 - test1-9

通过synchronized来实现计数功能

即将add方法声明为同步方法,同时只允许一个线程进行add操作。

  public synchronized static void add()
    {
        count++;
    }

可见性:一个线程对主内存的修改可以及时的被其他线程观察到

导致共享变量在线程间不可见的原因:

1.线程交叉执行

2.重排序结合线程交叉执行

3.共享变量更新的值没有在工作内存和主内存间及时更新

JVM关于synchronized的两条规定:

1.线程解锁前,必须把共享变量的最新值刷新到主内存

2.线程加锁时,应清空工作内存中的值,从而使用共享变量时需要从主内存中重新读取最新的值。

volatile关键字通过加入内存屏障和禁止指令重排序优化来实现

1.对volatile变量进行写操作时,会在写操作后加入一条store屏障指令,将工作内存中的值刷新到主内存

2.对volatile变量进行读操作时,会在读操作前加入一条load屏障指令,从主内存中读取共享变量到工作内存

 

有序性:一个线程在观察其他线程中的指令执行顺序,由于指令重排序的存在,该观察结果通常杂乱无序

volatile,禁止指令重排序

synchronized,lock(相当于让线程顺序执行代码)

happens-before原则

1.程序次序规则,一个线程内,按照代码执行顺序,书写在前面的操作先行发生于书写后面的操作。

2.锁定规则,一个unLock操作先行发生于后面对同一个锁的Lock操作

3.volatile变量规则,对一个变量的写操作先行发生于后面对这个变量的读操作

4.传递规则,如果操作A先行发生于操作B,而操作B又先行发生于操作C,则可以得出操作A先行发生于操作C

5.线程启动规则,Thread对象的start()方法先行发函俄国与此线程的每一个动作

6.线程中断规则,对线程interrupt()方法的调用先行发生于被中断线程的代码检测到中断事件的发生

7.线程终结规则,线程中的所有操作都先行发生于线程的中止检测,我们可以通过Thread.join()方法结束,Thread.isAlive()的返回值手段检测线程已经终止执行

8.对象终结规则,一个对象的初始化完成先行发生于他的finalize()方法的开始

总结

原子性:Atomic包  CAS算法 synchronized lock

可见性:synchronized,volatile

有序性:如果两个线程的执行顺序无法从happens-before原则中推导出来,他们就不能保证有序性,虚拟机可以随意地对它们进行指令重排序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值