二、并发编程之线程安全

1、当多个线程同时共享同一全局变量或静态变量,做写操作时,可能会发生数据冲突问题,也就是线程安全问题。

2、线程安全解决办法之Java内置的锁(synchronized)

2.1、同步代码块

public class Thread01 implements Runnable {
    private int trainCount = 100;
    public void run() {
        while (trainCount > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sale();
        }
    }

    private void sale() {
        synchronized (this) {
            if (trainCount > 0) {
                System.out.println(Thread.currentThread().getName() + ",出售第" + (100 - trainCount + 1) + "张票!");
                trainCount--;
            }
        }
    }

    public static void main(String[] args) {
        Thread01 t = new Thread01();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        t1.start();
        t2.start();
    }
}

2.2、同步方法

public class Thread02 implements Runnable {
    private int trainCount = 100;

    public void run() {
        while (trainCount > 0) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sale();
        }
    }

    private synchronized void sale() {
        if (trainCount > 0) {
            System.out.println(Thread.currentThread().getName() + ",出售第" + (100 - trainCount + 1) + "张票!");
            trainCount--;
        }
    }

    public static void main(String[] args) {
        Thread01 t = new Thread01();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        t1.start();
        t2.start();
    }
}

3、线程安全解决办法之ThreadLocal

public class Res {
    public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>(){
        protected Integer initialValue() {
            return 0;
        }
    };

    public Integer getNum(){
        int count = threadLocal.get() + 1;
        threadLocal.set(count);
        return count;
    }
}
public class Thread03 extends Thread {
    private Res res;

    public Thread03(Res res){
        this.res = res;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(Thread.currentThread().getName() + "---" + "i---" + i + "--num:" + res.getNum());
        }
    }

    public static void main(String[] args) {
        Res res = new Res();
        Thread03 t1 = new Thread03(res);
        Thread03 t2 = new Thread03(res);
        Thread03 t3 = new Thread03(res);
        t1.start();
        t2.start();
        t3.start();
    }
}

4、多线程的三大特性:原子性、可见性、有序性

5、内存模型

总结:什么是Java内存模型:java内存模型简称jmm义了一个线程另一个线程可见。共享变量存放在主内存中,每个线程都有自己的本地内存,多个线程同时访问一个数据的时候,可能本地内存没有及时刷新到主内存,所以就会发生线程安全问题。

6、volatile

6.1、用volatile修饰的变量,可保证修改后立即刷新到主内存中,其他线程需要读取时会直接读取主内存中的变量。

6.2、用volatile修饰变量可防止重排序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值