临界区和竞态条件
- 一个程序运行多个线程本身是没有问题的,问题在于多个线程访问共享资源;
- 多个线程只读共享资源没有问题,但是多个线程对共享资源进行读写操作时发生指令交错,就会出现问题。
- 一段代码内如果存在对共享资源的多线程读写操作,则这段代码块称为临界区。(Critical Section)
- 多个线程在临界区内执行,由于代码的执行序列不同导致结果无法预测,称之为发生了竞态条件(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这篇文章
如何解决
为了解决临界区的竞态条件发生,可以用以下方式
- 阻塞式:synchronized,lock
- 非阻塞式:原子变量
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思考
- 如果synchronized加在for循环外面,那么锁的颗粒度变粗,还是可以保证结果正确。
- 如果两个线程锁住不同的对象,相当于没锁
- 如果线程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());
}
48

被折叠的 条评论
为什么被折叠?



