多线程(四)锁
目录
死锁
- 一个同步块同时拥有“两个以上对象的锁”时,就可能会发生“死锁”的问题(相互等待对方的资源)
产生死锁的条件
- 以下死锁的四个必要条件,只要想办法破其中的任意一个或者多个条件就可以避免死锁的发生
-
互斥条件:一个资源每次只能被一个进程使用
-
请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放
-
不剥夺条件:进程已获得的资源,在未使用完之前,不能强制剥夺
-
循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系
Lock锁
- 通过显示定义同步锁对象来实现同步。同步锁使用Lock对象充当
- ReentrantLock类实现了Lock
Lock锁和synchronized锁的区别
-
- Lock是显示锁(手动)
- synchronized是隐式锁
-
- Lock只有代码块锁
- synchronized有代码块锁和方法锁
-
代码实例
死锁
- 张三要镜子,但是镜子在李四手上,李四要口红,但是口红在张三手上
- 死锁,永远不会停止
public class SXP10 {
public static void main(String[] args) {
Makeup g1=new Makeup(0,"张三");
Makeup g2=new Makeup(1,"李四");
g1.start();
g2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
class Makeup extends Thread{
//需要的资源只有一份,用static来保证只有一份
static Lipstick lipstick=new Lipstick();
static Mirror mirror=new Mirror();
int choice;
String girlNmae;//使用化妆品的人
Makeup(int choice,String girlNmae){
this.choice=choice;
this.girlNmae=girlNmae;
}
@Override
public void run(){
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException {
if(choice==0){
synchronized (lipstick){//获得口红的锁
System.out.println(this.girlNmae+"获得口红的锁");
Thread.sleep(1000);
synchronized (mirror){//1秒钟之后获得镜子的锁
System.out.println(this.girlNmae+"获得镜子的锁");
}
}
}else{
synchronized (mirror) {//获得镜子的锁
System.out.println(this.girlNmae + "获得镜子的锁");
Thread.sleep(2000);
synchronized (lipstick){//1秒钟之后获得口红的锁
System.out.println(this.girlNmae+"获得口红的锁");
}
}
}
}
}
防止上述死锁
public class SXP10 {
public static void main(String[] args) {
Makeup g1=new Makeup(0,"张三");
Makeup g2=new Makeup(1,"李四");
g1.start();
g2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
class Makeup extends Thread{
//需要的资源只有一份,用static来保证只有一份
static Lipstick lipstick=new Lipstick();
static Mirror mirror=new Mirror();
int choice;
String girlNmae;//使用化妆品的人
Makeup(int choice,String girlNmae){
this.choice=choice;
this.girlNmae=girlNmae;
}
@Override
public void run(){
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException {
if(choice==0){
synchronized (lipstick){//获得口红的锁
System.out.println(this.girlNmae+"获得口红的锁");
Thread.sleep(1000);
}
synchronized (mirror){//1秒钟之后获得镜子的锁
System.out.println(this.girlNmae+"获得镜子的锁");
}
}else{
synchronized (mirror) {//获得镜子的锁
System.out.println(this.girlNmae + "获得镜子的锁");
}
Thread.sleep(2000);
synchronized (lipstick){//1秒钟之后获得口红的锁
System.out.println(this.girlNmae+"获得口红的锁");
}
}
}
}
//输出:
张三获得口红的锁
李四获得镜子的锁
张三获得镜子的锁
李四获得口红的锁
Lock锁
public class Sxp11 {
public static void main(String[] args) {
TestLock2 t1=new TestLock2();
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
}
}
class TestLock2 implements Runnable{
int ticketNums=10;
//定义lock锁
private final ReentrantLock lock=new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock();//加锁
if(ticketNums>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNums--);
}else {
break;
}
}finally {
//解锁
lock.unlock();
}
}
}
}
//输出:
10
9
8
7
6
5
4
3
2
1