11.临界区和竞态条件,以及synchronized解决

临界区和竞态条件

  1. 一个程序运行多个线程本身是没有问题的,问题在于多个线程访问共享资源
  2. 多个线程只读共享资源没有问题,但是多个线程对共享资源进行读写操作时发生指令交错,就会出现问题。
  3. 一段代码内如果存在对共享资源的多线程读写操作,则这段代码块称为临界区。(Critical Section)
  4. 多个线程在临界区内执行,由于代码的执行序列不同导致结果无法预测,称之为发生了竞态条件(Race Condition)。竞争情况好理解一点。

示例

这个例子中,count就是一个共享变量。

有两个线程,一个线程对共享变量做5000次自增,另一个做5000次自减;在常规的认识下,count最后的值应该是0,但是结果不一定是。

public class Main {
    static int count = 0;

    public static void main(String[] args) {

        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    count++;
                }
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    count--;
                }
            }
        };

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(count);
    }

}

原因

参考JMM这篇文章

如何解决

为了解决临界区的竞态条件发生,可以用以下方式

  1. 阻塞式:synchronized,lock
  2. 非阻塞式:原子变量

synchronized解决

synchronized实际上是用对象锁保证了临界区内代码的原子性,临界区内的代码对外是不可分隔的,不会被线程切换打断。

public class Main {

    static int count = 0;
    static final Object lock = new Object();

    public static void main(String[] args) {

        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    synchronized (lock) {
                        count++;
                    }
                }
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    synchronized (lock) {
                        count--;
                    }
                }
            }
        };

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(count);
    }

}

synchronized思考

  1. 如果synchronized加在for循环外面,那么锁的颗粒度变粗,还是可以保证结果正确。
  2. 如果两个线程锁住不同的对象,相当于没锁
  3. 如果线程1加锁,线程2不加锁,相当于没锁

面向对象的改进

将共享变量的操作封装到方法中

public class Calculator {

    int value;

    public void add() {
        synchronized (this) {
            value++;
        }
    }

    public void sub() {
        synchronized (this) {
            value--;
        }
    }

    public int getValue() {
        synchronized (this) {
            return value;
        }
    }
}
public static void main(String[] args) {

        Calculator calculator = new Calculator();
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    calculator.add();
                }
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    calculator.sub();
                }
            }
        };

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(calculator.getValue());
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值