以下四个条件同时满足时机会产生死锁
- 产生死锁的条件
- 互斥,共享资源 X 和 Y 只能被一个线程占用;
- 占有且等待,线程 T1 已经取得共享资源 X,在等待共享资源 Y 的时候,不释放共享资源 X;
- 不可抢占,其他线程不能强行抢占线程 T1 占有的资源;
- 循环等待,线程 T1 等待线程 T2 占有的资源,线程 T2 等待线程 T1 占有的资源,就是循环等
案例,fromAccount账户和toAccount在操作时只允许一个线程操作:
public class TransferAccount implements Runnable{
private Account fromAccount; //转出账户
private Account toAccount; //转入账户
public TransferAccount(Account fromAccount, Account toAccount) {
this.fromAccount = fromAccount;
this.toAccount = toAccount;
}
@Override
public void run() {
while(true){
synchronized (fromAccount) {
// 1 可能发生死锁的情况
synchronized (toAccount) {
System.out.println("成功进入"+System.currentTimeMillis());
}
}
}
}
public static void main(String[] args) {
Account a1= new Account();
Account b1= new Account();
Thread a = new Thread(new TransferAccount(a1,b1));
Thread b = new Thread(new TransferAccount(b1,a1));
a.start();
b.start();
}
}
public class Account {
}
``
如图,标记位置1出当线程A和B同时进入到位置1处,即线程A需要获取b1对象的锁,而此时线程B已经获取到了b1对象的锁,需要获取a1对象的锁,a1对象又被线程A获取了,以此形成了互相等待对方释放锁,这便是死锁
那么如何解决死锁呢?
我们看死锁产生的条件的第一条:互斥,共享资源 X 和 Y 只能被一个线程占用;
既然是要获取锁,那么共享资源必定智能被一个线程占用,那么第一条我们没有办法破坏
第二条:占有且等待,线程 T1 已经取得共享资源 X,在等待共享资源 Y 的时候,不释放共享资源 X;
如果我们可以让共享资源X和Y要么被同时获取,要么都不获取,那么即可破坏,代码如下
public class TransferAccount implements Runnable{
private Account fromAccount; //转出账户
private Account toAccount; //转入账户
private Allocator allocator;
public TransferAccount(Account fromAccount, Account toAccount,Allocator allocator) {
this.fromAccount = fromAccount;
this.toAccount = toAccount;
this.allocator = allocator;
}
@Override
public void run() {
while(true){
try {
if (allocator.apply(fromAccount,toAccount)) {
// 同时获得共享资源X和Y后才能进入
System.out.println("成功进入"+System.currentTimeMillis());
}
} finally {
// 释放资源
allocator.free(fromAccount,toAccount);
}
}
}
public static void main(String[] args) {
Account fromAccount=new Account();
Account toAccount=new Account();
Allocator allocator = new Allocator();
Thread a = new Thread(new TransferAccount(fromAccount,toAccount,allocator));
Thread b = new Thread(new TransferAccount(toAccount,fromAccount,allocator));
a.start();
b.start();
}
}
`
public class Allocator {
private List<Object> list=new ArrayList<>();
synchronized boolean apply(Object from,Object to){
if(list.contains(from)||list.contains(to)){
return false;
}
list.add(from);
list.add(to);
return true;
}
synchronized void free(Object from,Object to){
list.remove(from);
list.remove(to);
}
}
第三条:不可抢占,其他线程不能强行抢占线程 T1 占有的资源;
如果不适用阻塞的synchronized那么即可破坏这一条
public class TransferAccount implements Runnable{
private Account fromAccount; //转出账户
private Account toAccount; //转入账户
Lock fromLock = new ReentrantLock();
Lock toLock = new ReentrantLock();
public TransferAccount(Account fromAccount, Account toAccount) {
this.fromAccount = fromAccount;
this.toAccount = toAccount;
}
@Override
public void run() {
while(true){
// 可重入锁ReentrantLock的tryLock尝试获取锁,获得了则返回true,否则返回false
if(fromLock.tryLock()) {
if(toLock.tryLock()) {
System.out.println("成功进入"+System.currentTimeMillis());
}
}
}
}
public static void main(String[] args) {
Account fromAccount=new Account();
Account toAccount=new Account();
Thread a = new Thread(new TransferAccount(fromAccount,toAccount));
Thread b = new Thread(new TransferAccount(toAccount,fromAccount));
a.start();
b.start();
}
}
第四条:循环等待,线程 T1 等待线程 T2 占有的资源,线程 T2 等待线程 T1 占有的资源,就是循环等
即不允许循环等待的代码,如果把调用代码修改为
Thread a = new Thread(new TransferAccount(fromAccount,toAccount));
Thread b = new Thread(new TransferAccount(fromAccount,toAccount));
以上便是针对死锁的分析及解决方案