案例1
public class Account {
private String accountName;
private int balance;
public Account(String accountName, int balance) {
this.accountName = accountName;
this.balance = balance;
}
public void debit(int amount){ //更新转出方的余额
this.balance-=amount;
}
public void credit(int amount){ //更新转入方的余额
this.balance+=amount;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
public class TransferAccountLock implements Runnable {
private Account fromAccount; //转出账户
private Account toAccount; //转入账户
private int amount;
public TransferAccountLock(Account fromAccount, Account toAccount, int amount) {
this.fromAccount = fromAccount;
this.toAccount = toAccount;
this.amount = amount;
}
@Override
public void run() {
while (true) {
synchronized (fromAccount) {
synchronized (toAccount) {
if (fromAccount.getBalance() >= amount) {
fromAccount.debit(amount);
toAccount.credit(amount);
}
}
}
//转出账户的余额
System.out.println(fromAccount.getAccountName() + "->" + fromAccount.getBalance());
//转入账户的余额
System.out.println(toAccount.getAccountName() + "->" + toAccount.getBalance());
}
}
public static void main(String[] args) {
Account fromAccount = new Account("李四", 100000);
Account toAccount = new Account("张三", 300000);
Thread a = new Thread(new TransferAccountLock(fromAccount, toAccount, 10));
Thread b = new Thread(new TransferAccountLock(toAccount, fromAccount, 30));
a.start();
b.start();
}
}
此时例子中的a线程和b线程将互相等待,造成死锁,那什么是死锁,什么是活锁?
死锁: 一组互相竞争资源的线程因互相等待,导致“永久”阻塞的现象。
活锁: 活锁指的是任务或者执行者没有被阻塞,由于某些条件没有满足,导致一直重复尝试—失败—尝试—失败的过程。处于活锁的实体是在不断的改变状态, 活锁有可能自行解开。
死锁发生的条件
这四个条件同时满足,就会产生死锁。
互斥,共享资源 X 和 Y 只能被一个线程占用;
占有且等待,线程 T1 已经取得共享资源 X,在等待共享资源 Y 的时候,不释放共享资源 X;
不可抢占,其他线程不能强行抢占线程 T1 占有的资源;
循环等待,线程 T1 等待线程 T2 占有的资源,线程 T2 等待线程 T1 占有的资源,就是循环等待。
如何解决死锁问题
按照前面说的四个死锁的发生条件,我们只需要破坏其中一个,就可以避免死锁的产生。
其中,互斥这个条件我们没有办法破坏,因为我们用锁为的就是互斥,其他三个条件都有办法可以破坏
1.对于“占用且等待”这个条件,我们可以一次性申请所有的资源,这样就不存在等待了。
样例1
public class TransferAccount implements Runnable{
private Account fromAccount; //转出账户
private Account toAccount; //转入账户
private int amount;
Allocator allocator;
public TransferAccount(Account fromAccount, Account toAccount, int amount,Allocator allocator) {
this.fromAccount = fromAccount;
this.toAccount = toAccount;
this.amount = amount;
this.allocator=allocator;
}
@Override
public void run() {
while(true){
if(allocator.apply(fromAccount,toAccount)) {
try {
synchronized (fromAccount) {
synchronized (toAccount) {
if (fromAccount.getBalance() >= amount) {
fromAccount.debit(amount);
toAccount.credit(amount);
}
}
}
//转出账户的余额
System.out.println(fromAccount.getAccountName() + "->" + fromAccount.getBalance());
//转入账户的余额
System.out.println(toAccount.getAccountName() + "->" + toAccount.getBalance());
}finally {
allocator.free(fromAccount,toAccount);
}
}
}
}
public static void main(String[] args) {
Account fromAccount=new Account("M李四",100000);
Account toAccount=new Account("张三",300000);
Allocator allocator=new Allocator();
Thread a =new Thread(new TransferAccount(fromAccount,toAccount,10,allocator));
Thread b=new Thread(new TransferAccount(toAccount,fromAccount,30,allocator));
a.start();
b.start();
}
}
public class Allocator {
private List 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);
}
}
2.对于“不可抢占”这个条件,占用部分资源的线程进一步申请其他资源时,如果申请不到,可以主动 释放它占有的资源,这样不可抢占这个条件就破坏掉了。
样例2
public class TransferAccount02 implements Runnable{
private Account fromAccount; //转出账户
private Account toAccount; //转入账户
private int amount;
Lock fromLock=new ReentrantLock();
Lock toLock=new ReentrantLock();
public TransferAccount02(Account fromAccount, Account toAccount, int amount) {
this.fromAccount = fromAccount;
this.toAccount = toAccount;
this.amount = amount;
}
@Override
public void run() {
while(true){
if (fromLock.tryLock()) { //返回true和false
if (toLock.tryLock()) {//返回true和false
if (fromAccount.getBalance() >= amount) {
fromAccount.debit(amount);
toAccount.credit(amount);
}
}
}
//转出账户的余额
System.out.println(fromAccount.getAccountName() + "->" + fromAccount.getBalance());
//转入账户的余额
System.out.println(toAccount.getAccountName() + "->" + toAccount.getBalance());
}
}
public static void main(String[] args) {
Account fromAccount=new Account("李四",100000);
Account toAccount=new Account("张三",300000);
Thread a =new Thread(new TransferAccount02(fromAccount,toAccount,10));
Thread b=new Thread(new TransferAccount02(toAccount,fromAccount,30));
a.start();
b.start();
}
}
3.对于“循环等待”这个条件,可以靠按序申请资源来预防。所谓按序申请,是指资源是有线性顺序 的,申请的时候可以先申请资源序号小的,再申请资源序号大的,这样线性化后自然就不存在循环了。
样例3
public class TransferAccount03 implements Runnable{
private Account fromAccount; //转出账户
private Account toAccount; //转入账户
private int amount;
public TransferAccount03(Account fromAccount, Account toAccount, int amount) {
this.fromAccount = fromAccount;
this.toAccount = toAccount;
this.amount = amount;
}
@Override
public void run() {
Account left=null;
Account right=null;
if(fromAccount.hashCode()>toAccount.hashCode()){
left=toAccount;
right=fromAccount;
}
while(true){
synchronized (left) { //返回true和false
synchronized (right) {//返回true和false
if (fromAccount.getBalance() >= amount) {
fromAccount.debit(amount);
toAccount.credit(amount);
}
}
}
//转出账户的余额
System.out.println(fromAccount.getAccountName() + "->" + fromAccount.getBalance());
//转入账户的余额
System.out.println(toAccount.getAccountName() + "->" + toAccount.getBalance());
}
}
public static void main(String[] args) {
Account fromAccount=new Account("李四",100000);
Account toAccount=new Account("张三",300000);
Thread a =new Thread(new TransferAccount03(fromAccount,toAccount,10));
Thread b=new Thread(new TransferAccount03(toAccount,fromAccount,30));
a.start();
b.start();
}
}