崇之他和她

导航

day 015 ReentrantLock

死锁

线程互相拿着对方想要获取的资源,抱着不放,解决方法,不能同时抱着两个资源处理

package thread.syn;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 11:28 2021/1/17
 * @ Description:死锁
 * @ Modified By:
 * @Version: $version$
 */
public class DeadLock {
    public static void main(String[] args) {
        Makeup makeup1 = new Makeup(0,"小丽");
        Makeup makeup2 = new Makeup(1,"阿华");
        makeup1.start();
        makeup2.start();
    }
}
//口红对象
class Lipstick{

}
//镜子对象
class Mirror{

}
//化妆对象
class Makeup extends  Thread{
    //用static 保证资源只有一份
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();
    int choice;
    String girlName;

    public Makeup(int choice, String girlName) {
        this.choice = choice;
        this.girlName = girlName;
    }
    @Override
    public void run() {
        super.run();
        //化妆
        try {
            makeUp();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //化妆,互相持有对方的锁,需要对方的资源
    private  void makeUp() throws InterruptedException {
        if(choice == 0){
            synchronized (lipstick){
                System.out.println(this.girlName+"用着口红的锁,一秒后拿镜子");
                Thread.sleep(1000);
                synchronized (mirror){
                    System.out.println(this.girlName+"拿到镜子");
                }
            }
        }else {
            synchronized (mirror){
                System.out.println(this.girlName+"用着镜子的锁,一秒后拿口红");
                Thread.sleep(1000);
                synchronized (lipstick){
                    System.out.println(this.girlName+"拿到口红");
                }
            }
        }
    }
    //解决死锁修改后的方法
     //化妆,互相持有对方的锁,需要对方的资源
    private  void makeUp() throws InterruptedException {
        if(choice == 0){
            synchronized (lipstick){
                System.out.println(this.girlName+"用着口红的锁,一秒后拿镜子");
                Thread.sleep(1000);
                }
             synchronized (mirror){
                    System.out.println(this.girlName+"拿到镜子");
            }
        }else {
            synchronized (mirror){
                System.out.println(this.girlName+"用着镜子的锁,一秒后拿口红");
                Thread.sleep(1000);
                }
                synchronized (lipstick){
                    System.out.println(this.girlName+"拿到口红");
            }
        }
    }
}

ReentrantLock

package thread.syn;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ Author     :wwwzhqwww
 * @ Date       :Created in 17:40 2021/1/17
 * @ Description:测试lock
 * @ Modified By:
 * @Version: $version$
 */
public class TestLock {
    public static void main(String[] args) {
        TestLock2 lo = new TestLock2();
       Thread a =  new Thread(lo,"huangniu");
        Thread b =  new Thread(lo,"gouzi") ;
        Thread c =  new Thread(lo,"我") ;
       a.start();
       b.start();
       c.start();
    }
}
//
class TestLock2 implements Runnable{
    int ticketNum = 10;
    private final ReentrantLock re = new ReentrantLock();//可重入锁
    @Override
    public void run() {

        try{
            re.lock();
            while (true){
                if (ticketNum>0){

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"第"+ticketNum+"张票");
                    ticketNum--;
                }else {
                    break;
                }
            }
        }finally {
            re.unlock();
            System.out.println(Thread.currentThread().getName()+"释放ReentrantLock");
        }

    }
}

生产消费缓冲模式场景

缓冲区中有产品 消费者可以买,生产者等待

缓冲区中无产品 生产者生产 消费者等待

posted on 2021-01-17 19:02  崇之他和她  阅读(54)  评论(0编辑  收藏  举报