Java中的死锁是两个或更多线程永远被阻塞,互相等待的情况。
它导致正在执行的线程在等待与指定对象关联的锁或监视器时阻塞。
假设死锁是由于进程竞争资源而引起的,我们下面给出死锁发生的四个必要条件,这四个条件是Coffman首先提出的,所以也称为Coffman条件:
(1) 资源独占(mutual exclusion): 一个资源在同一时刻只能分配给一个进程. 如果某一进程申请某一资源, 而该资源正被另外某一进程所占有, 则申请者需等待, 直到占有者释放该资源;
(2) 不可剥夺(no-preemption): 资源申请者不能强行地从资源占有者手中夺取资源. 即资源只能由其占有者在使用完后自愿地释放;
(3) 保持申请(hold and wait): 进程在占有部分资源后还可申请新的资源, 而且在申请新资源的时候并不释放它已经占有的资源;
(4) 循环等待(circular wait): 存在一个进程等待序列{p1,p2,…,pn}, 其中p1等待p2所占有的某一资源, p2等待p3所占有的某一资源,…,pn等待p1所占有的某一资源.
当且仅当上述四个条件同时满足时, 死锁才会发生. 换言之, 只要破坏上述四个条件中的任意一个, 死锁就不会发生.
死锁示例
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Deadlock {
public static void main(String[] args) {
Deadlock deadlock = new Deadlock();
new Thread(deadlock::action1).start();
new Thread(deadlock::action2).start();
}
private Lock lock1 = new ReentrantLock(true);
private Lock lock2 = new ReentrantLock(true);
public void action1() {
try {
lock1.lock();
System.out.println("LOCK1 IS BLOCKED");
TimeUnit.MILLISECONDS.sleep(200);
lock2.lock();
System.out.println("LOCK2 IS BLOCKED");
TimeUnit.MILLISECONDS.sleep(200);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock1.unlock();
lock2.unlock();
}
}
public void action2() {
try {
lock2.lock();
System.out.println("LOCK2 IS BLOCKED");
TimeUnit.MILLISECONDS.sleep(200);
lock1.lock();
System.out.println("LOCK1 IS BLOCKED");
TimeUnit.MILLISECONDS.sleep(200);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock1.unlock();
lock2.unlock();
}
}
}
package com.example.thread.deadlock.lock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BankAccount {
double balance;
final int id;
final Lock lock = new ReentrantLock();
BankAccount(int id, double balance) {
this.id = id;
this.balance = balance;
}
void withdraw(double amount) {
// Wait to simulate io like database access ...
try {
Thread.sleep(10l);
} catch (InterruptedException e) {
}
balance -= amount;
}
void deposit(double amount) {
// Wait to simulate io like database access ...
try {
Thread.sleep(10l);
} catch (InterruptedException e) {
}
balance += amount;
}
static void transfer(BankAccount from, BankAccount to, double amount) {
from.lock.lock();
from.withdraw(amount);
to.lock.lock();
to.deposit(amount);
to.lock.unlock();
from.lock.unlock();
}
public static void main(String[] args) {
final BankAccount fooAccount = new BankAccount(1, 100d);
final BankAccount barAccount = new BankAccount(2, 100d);
new Thread() {
public void run() {
System.out.println(this.getId()+this.getName()+":"+fooAccount.balance);
System.out.println(this.getId()+this.getName()+":"+barAccount.balance);
BankAccount.transfer(fooAccount, barAccount, 10d);
System.out.println(this.getId()+this.getName()+":"+fooAccount.balance);
System.out.println(this.getId()+this.getName()+":"+barAccount.balance);
}
}.start();
new Thread() {
public void run() {
System.out.println(this.getId() + this.getName() + ":" + fooAccount.balance);
System.out.println(this.getId() + this.getName() + ":" + barAccount.balance);
BankAccount.transfer(barAccount, fooAccount, 10d);
System.out.println(this.getId() + this.getName() + ":" + fooAccount.balance);
System.out.println(this.getId() + this.getName() + ":" + barAccount.balance);
}
}.start();
}
}
package com.example.thread.deadlock._synchronized;
public class BankAccount {
double balance;
int id;
BankAccount(int id, double balance) {
this.id = id;
this.balance = balance;
}
void withdraw(double amount) {
// Wait to simulate io like database access ...
try {Thread.sleep(10l);} catch (InterruptedException e) {}
balance -= amount;
}
void deposit(double amount) {
// Wait to simulate io like database access ...
try {Thread.sleep(10l);} catch (InterruptedException e) {}
balance += amount;
}
static void transfer(BankAccount from, BankAccount to, double amount) {
synchronized(from) {
from.withdraw(amount);
synchronized(to) {
to.deposit(amount);
}
}
}
public static void main(String[] args) {
final BankAccount fooAccount = new BankAccount(1, 100d);
final BankAccount barAccount = new BankAccount(2, 100d);
new Thread() {
public void run() {
BankAccount.transfer(fooAccount, barAccount, 10d);
}
}.start();
new Thread() {
public void run() {
BankAccount.transfer(barAccount, fooAccount, 10d);
}
}.start();
}
}
如何避免Java中的死锁?
尽管不可能完全避免死锁,但是我们可以遵循某些措施或指针来避免死锁:
-
避免嵌套锁–必须避免为多个线程提供锁,这是造成死锁情况的主要原因。当您将锁锁定到多个线程时,通常会发生这种情况。
-
避免不必要的锁–应将锁授予重要线程。将锁授予导致死锁条件的不必要线程。
-
使用线程连接–当一个线程在等待另一个线程完成时,通常会发生死锁。在这种情况下,我们可以将Thread.join与线程花费的最长时间一起使用。