Deadlock
- Multiple threads each occupy some shared resources, and wait for the resources occupied by other threads to run. As a result, two or more threads are waiting for each other to release resources and all stop execution. A certain synchronization block has both "two The problem of "deadlock" may occur when the above objects are locked.
Deadlock avoidance method
- Four necessary conditions for deadlock:
- Mutually exclusive conditions: A resource can only be used by one process at a time.
- Request and hold conditions: When a process is blocked by requesting resources, it keeps on holding the acquired resources.
- Non-deprivation conditions: The resources that the process has acquired cannot be deprived forcibly before they are used up.
- Circulation waiting condition: A kind of cyclic waiting resource relationship is formed between several processes.
以上列出了死锁的四个必要条件,我们只要想办法破其中的任意一个或多个条件就可以避免死锁的发生
Case
package Thread;
//死锁 :多个线程互相拿着对方所需要的资源,然后形成僵持
public class DeadLock {
public static void main(String[] args) {
Makeop makeup = new Makeop(0,"妹妹");
Makeop makeup1 = new Makeop(1,"姐姐");
makeup.start();
makeup1.start();
}
}
//口红
class Liostick{
}
//镜子
class Mirrir{
}
class Makeop extends Thread {
//需要的资源只有一份,用static来保证只有一份
static Liostick lt = new Liostick();
static Mirrir mr = new Mirrir();
//选择
int choice;
//使用化妆品的人
String girlName;
Makeop(int choice, String girlName) {
this.choice = choice;
this.girlName = girlName;
}
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException {
if (choice == 0) {
//获得口红的锁
synchronized (lt) {
System.out.println(this.getName() + "获得口红的锁");
// 1s 之后获得镜子
Thread.sleep(1000);
//获得镜子的锁
synchronized (mr) {
System.out.println(this.getName() + "获得镜子的锁");
}
}
} else {
//获得镜子的锁
synchronized (mr) {
System.out.println(this.getName() + "获得镜子的锁");
// 1s 之后获得口红
Thread.sleep(1000);
//获得口红的锁
synchronized (lt) {
System.out.println(this.getName() + "获得口红的锁");
}
}
}
}
}
Modified case: Put the lock outside another lock
package Thread;
//死锁 :多个线程互相拿着对方所需要的资源,然后形成僵持
public class DeadLock {
public static void main(String[] args) {
Makeop makeup = new Makeop(0,"妹妹");
Makeop makeup1 = new Makeop(1,"姐姐");
makeup.start();
makeup1.start();
}
}
//口红
class Liostick{
}
//镜子
class Mirrir{
}
class Makeop extends Thread {
//需要的资源只有一份,用static来保证只有一份
static Liostick lt = new Liostick();
static Mirrir mr = new Mirrir();
//选择
int choice;
//使用化妆品的人
String girlName;
Makeop(int choice, String girlName) {
this.choice = choice;
this.girlName = girlName;
}
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException {
if (choice == 0) {
//获得口红的锁
synchronized (lt) {
System.out.println(this.getName() + "获得口红的锁");
// 1s 之后获得镜子
Thread.sleep(1000);
}
//获得镜子的锁
synchronized (mr) {
System.out.println(this.getName() + "获得镜子的锁");
}
} else {
//获得镜子的锁
synchronized (mr) {
System.out.println(this.getName() + "获得镜子的锁");
// 1s 之后获得口红
Thread.sleep(2000);
}
//获得口红的锁
synchronized (lt) {
System.out.println(this.getName() + "获得口红的锁");
}
}
}
}