LOCK&&synchronized

 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();
            }

        }
    }
}

你可能感兴趣的:(java,前端,开发语言)