LOCK&&synchronized

发布时间:2023年12月29日

?synchronized:隐式锁,出了作用域自动释放,有代码块锁和方法锁

LOCK:显示锁(手动开启和释放)只有代码块锁;花费较少的时间调度线程,性能更好

public class DeadLock extends Thread{
    public static void main(String[] args) {
        Makeup g1 = new Makeup(0,"A");
        Makeup g2 = new Makeup(1,"B");
        g1.start();
        g2.start();
    }

}
class Lipstick{

}
class Mirror{

}
class Makeup extends Thread{
    static Lipstick lip = new Lipstick();
    static Mirror mirror = new Mirror();

    int choice;
    String name;
    Makeup(int choices,String name){
        this.choice = choices;
        this.name = name;

    }
    public void run(){
        try {
            makeup();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    private  void makeup() throws InterruptedException {
      if(choice == 0){
            synchronized (lip){
                System.out.println(this.name+"拿到口红");
                Thread.sleep(1000);
                synchronized (mirror){
                    System.out.println(this.name+"拿到镜子");
                }
            }
        }else{
            synchronized (mirror){
                System.out.println(this.name+"拿到镜子");
                Thread.sleep(1000);
                synchronized (lip){
                    System.out.println(this.name+"拿到口红");
                }
            }
        }
    }
}
public class DeadLock extends Thread{
    public static void main(String[] args) {
        Makeup g1 = new Makeup(0,"A");
        Makeup g2 = new Makeup(1,"B");
        g1.start();
        g2.start();
    }

}
class Lipstick{

}
class Mirror{

}
class Makeup extends Thread{
    static Lipstick lip = new Lipstick();
    static Mirror mirror = new Mirror();

    int choice;
    String name;
    Makeup(int choices,String name){
        this.choice = choices;
        this.name = name;

    }
    public void run(){
        try {
            makeup();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    private  void makeup() throws InterruptedException {
        


        if(choice == 0){
            synchronized (lip){
                System.out.println(this.name+"拿到口红");
                Thread.sleep(1000); 
            }
            synchronized (mirror){
                    System.out.println(this.name+"拿到镜子");
                }
        }else{
            synchronized (mirror){
                System.out.println(this.name+"拿到镜子");
                Thread.sleep(1000);
                
            }
             synchronized (lip){
                    System.out.println(this.name+"拿到口红");
                }
        }
    }
}
import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
    public static void main(String[] args) {
        testlock2 t1 = new testlock2();
        testlock2 t2 = new testlock2();
        testlock2 t3 = new testlock2();

       new Thread(t1).start();
        //new Thread(t2).start();
        //new Thread(t3).start();
    }
}
class testlock2 implements Runnable{
   int num = 10;
private final ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while(true){
            lock.lock();
            try{
                if(num >0){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(num--);
                }
                else{
                    break;
                }
            }finally {
                lock.unlock();
            }

        }
    }
}

文章来源:https://blog.csdn.net/weixin_59426666/article/details/135233555
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。