2018329621184_边凯昂_homework7 Version 0 |
|
👤 Author: by 244766935qqcom 2020-11-11 15:33:31 |
2018329621184 边凯昂
Examples:
/**
* @Author: Usher
* @Descr iption:
* 现在有线程1和线程2。线程1执行过程中,
* 先锁定了对象a,然后需要再锁定b才能继续执行代码;
* 而线程2正巧相反,先锁定了b,需要再锁定a才能继续执行代码。
* 这时,两个线程都等着对方解锁,才能继续执行,
* 这时,两个线程就进入等待状态,
* 最终不会有线程执行。这就变成了死锁。
*/
class DeadLock implements Runnable{
boolean lockFormer;
static Object object1 = new Object();
static Object object2 = new Object();
DeadLock(boolean lockFormer){
this.lockFormer = lockFormer;
}
/**
sleep()方法,实现主线程持有a的对象锁并请求b的对象锁、副线程持有b的对象锁并请求a的对象锁的场景,即发生死锁。
*/
@Override
public void run() {
if (this.lockFormer){
synchronized (object1){
System.out.println("t1 locked object1 require to lock object2");
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
synchronized (object2){
System.out.println("t1 locked object2");
}
}
}else {
synchronized (object2){
System.out.println("t2 locked object2 require to lock object1");
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
synchronized (object1){
System.out.println("t2 locked object1");
}
}
}
}
}
Four conditions:
< 1 > Mutual Exclusion.
That is, a resource can only be occupied by one process in a period of time, and cannot be occupied by two or more processes at the same time.This exclusive resource, such as a CD-ROM drive, printer, etc., must be released by the process holding the resource before other processes can take possession of the resource.This is determined by the properties of the resource itself. In this deadlock, resources a and b are nonsharable.
< 2 > No Preemption.
In this deadlock, resources acquired by the process cannot be forcibly taken from the occupant until they are used up, but can only be released by the occupant process.
< 3 > Hold and Wait.
Processes already possess at least one resource, but request new resources; Because the resource is occupied by another process, the process is blocked; However, it continues to occupy existing resources while it waits for new ones.Also take the single-log bridge as an example, a and B two people meet on the bridge. A crosses one section of the bridge (that is, takes possession of some resources) and needs to cross the rest of the bridge (applies for new resources), but that part of the bridge is occupied by B (B crosses one section of the bridge).A can not pass, forward can not, and do not retreat;B is in the same situation.
< 4 > Circular Wait.
There is a process waiting sequence {P1, P2..., Pn}, where P1 waits for a resource occupied by P2, and P2 waits for a source occupied by P3..., while Pn waits for some resource occupied by P1 to form a process cycle waiting loop.Just like the previous single-plank bridge problem, A waits for the deck possessed by B, while B waits for the deck possessed by A, thus waiting for each other in a circular way. In this deadlock two process and resources a and b form a cicle.
My simple way for avoiding deadlocks:
Detect deadlocks and restore. Stop process 1 and release the resourse a, then process 2 occupy the resource a, then process 2 continue. After process 2 finish, process 2 release its resources, then process 1 begin to work, it occupies its resource and release them after it finishes.