线程安全问题
当多个线程并发访问临界资源时,如果破坏了原子操作,可能会造成数据不一致。
临界资源:共享资源(同一个对象),一次仅允许一个线程使用,才可保证其正确性。
原子操作:不可分割的多步操作,被视作一个整体,其顺序和步骤不可打乱或缺省。
同步方式
1同步代码块:
//假设两个线程分别往同一个字符串数组里添加字符串
public class ThreadSafe() {
private static int index = 0; //字符串数组下标
public static void main(String[] args) throws Exception {
//创建数组
String[] str = new String[10];
//创建线程1
Runnable runnable1 = new Runnable() {
@Override
public void run() {
synchronized(str) { //这里如果使用同步方式,有可能线程1读完下面第一条语句时间到了,还没有来的及index++,线程2就开始运行了
str[index] = "hello";
index++;
}
}
};
//创建线程2
Runnable runnable2 = new Runnable() {
@Override
public void run() {
synchronized(str) {
str[index] = "Java";
index++;
}
}
};
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
}
}
2同步方法
public class BankCard {
//余额
private double money;
//标记
private boolean flag; //true 有钱可以取
//存钱
public synchronized void save(double m) {
while(flag) { //有钱
try {
this.wait(); //进入等待队列,同时释放锁
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
money = money + m;
System.out.println(Thread.currentThread().getName() + "存了" + m + "余额是" + money);
//修改锁标记
flag = true;
//唤醒取钱线程
this.notify();
}
//取钱
public synchronized void take(double m) {
while(!flag) { //没钱
try {
this.wait();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
money = money - m;
System.out.println(Thread.currentThread().getName() + "取了" + m + "余额是" + money);
//修改锁标记
flag = false;
//唤醒
this.notify();
}
}
一个线程可以同时拥有多个对象的锁标记,当线程阻塞时,不会释放已经拥有的锁标记,由此可能造成死锁。
当一个线程拥有A对象锁标记,并等待B对象锁标记,同时另有一个线程拥有B对象锁标记,并等待A对象锁标记,这时候产生了死锁现象。