Synchronized 和 Lock的区别,以及使用Lock的好处

Synchronized 和 Lock 在java并发编程中广泛使用,简单的来说下这两者的区别,记录下

一,两者的构造

  synchronized是关键字,它是属于JVM层面的

  Lock是一个具体的类,它是属于API层面的锁  (java.util.concurrent.locks.Lock)

分享图片

分享图片

synchronized底层是通过monitor对象来完成

二,使用方法

  synchronized 不需要用户手动去释放锁,当synchronized代码执行完后系统会自动让线程释放对锁的占用

  reentrantlock则需要用户去手动释放锁,若没有主动释放锁,可能会导致死锁现象。需要lock()和unlock()方法配合try/finally语句块来完成

分享图片

 

三,等待是否可以中断

  synchronized不可中断,除非抛出异常或者正常运行完成

  reentrantlock 可中断:

    1),设置超时方法tryLock(long timeout,TimeUnit unit)

    2),lockInterruptibly()放代码块中,调用interrupt()方法可中断

推荐查看此博客https://www.cnblogs.com/dolphin0520/p/3923167.html

 

四,加锁是否公平

  synchronized 非公平锁

  reentrantlock两者都可以,认非公平锁,构造方法可以传入boolean值,true为公平锁,false为非公平锁 

  

分享图片

 

五,锁绑定多个条件Condition

  synchronized 没有

  reentrantlock用来实现分组唤醒需要唤醒的线程们,可以精确唤醒,而不是像synchronized要么随机唤醒一个要么唤醒全部线程。

 Lock的好处,在第五点看来绑定多个Condition  可以精确唤醒,用一下列子来看看:

/**
 * 
 * 编写一个线程,开启三个线程,这三个线程的ID分别为A,B,C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按顺序显示。
 * 如:ABCABCABC... 依次递归
 *
 */
public class TestABCAlternate {
    
    public static void main(String[] args) {
        AlternateDemo ad = new AlternateDemo();
        
        new Thread(new Runnable() {
            public void run() {
                
                for (int i = 1; i <= 20; i++) {
                    ad.loopA(i);
                }
                
            }
        },"A").start();
        
        new Thread(new Runnable() {
            public void run() {
                
                for (int i = 1; i <= 20; i++) {
                    ad.loopB(i);
                }
                
            }
        },"B").start();
        
        new Thread(new Runnable() {
            public void run() {
                
                for (int i = 1; i <= 20; i++) {
                    ad.loopC(i);
                    
                    System.out.println("===========================");
                }
            }
        },"C").start();
    }
}

class AlternateDemo{
    private int number = 1;//当前正在执行线程的标记
    
    private Lock lock = new reentrantlock();
    
    private Condition condition1 = lock.newCondition();  //多个condition 精确唤醒
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
    /**
     * 
     * @param totalLoop : 循环第几轮
     */
    public void loopA(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            while(number!=1) {    //使用while  不使用if  是防止虚假唤醒
                condition1.await();
            }
            //2.打印
            for (int i = 1; i <= 1; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i+"\t"+totalLoop);
            }
            //3.唤醒
            number = 2;
            condition2.signal();
        } catch (Exception e) {
            // Todo: handle exception
        } finally {
            lock.unlock();
        }
    }
    public void loopB(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            while(number!=2) {
                condition2.await();
            }
            //2.打印
            for (int i = 1; i <= 1; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i+"\t"+totalLoop);
            }
            //3.唤醒
            number = 3;
            condition3.signal();
        } catch (Exception e) {
            // Todo: handle exception
        } finally {
            lock.unlock();
        }
    }
    public void loopC(int totalLoop) {
        lock.lock();
        try {
            //1.判断
            while(number!=3) {
                condition3.await();
            }
            //2.打印
            for (int i = 1; i <= 1; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i+"\t"+totalLoop);
            }
            //3.唤醒
            number = 1;
            condition1.signal();
        } catch (Exception e) {
            // Todo: handle exception
        } finally {
            lock.unlock();
        }
    }
}

 

/** *  * 编写一个线程,开启三个线程,这三个线程的ID分别为A,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按顺序显示。 * 如:ABCABCABC... 依次递归 * */public class TestABCAlternate {public static void main(String[] args) {AlternateDemo ad = new AlternateDemo();new Thread(new Runnable() {public void run() {for (int i = 1; i <= 20; i++) {ad.loopA(i);}}},"A").start();new Thread(new Runnable() {public void run() {for (int i = 1; i <= 20; i++) {ad.loopB(i);}}},"B").start();new Thread(new Runnable() {public void run() {for (int i = 1; i <= 20; i++) {ad.loopC(i);System.out.println("===========================");}}},"C").start();}}class AlternateDemo{private int number = 1;//当前正在执行线程的标记private Lock lock = new reentrantlock();private Condition condition1 = lock.newCondition();private Condition condition2 = lock.newCondition();private Condition condition3 = lock.newCondition();/** *  * @param totalLoop : 循环第几轮 */public void loopA(int totalLoop) {lock.lock();try {//1.判断while(number!=1) {condition1.await();}//2.打印for (int i = 1; i <= 1; i++) {System.out.println(Thread.currentThread().getName()+"\t"+i+"\t"+totalLoop);}//3.唤醒number = 2;condition2.signal();} catch (Exception e) {// Todo: handle exception} finally {lock.unlock();}}public void loopB(int totalLoop) {lock.lock();try {//1.判断while(number!=2) {condition2.await();}//2.打印for (int i = 1; i <= 1; i++) {System.out.println(Thread.currentThread().getName()+"\t"+i+"\t"+totalLoop);}//3.唤醒number = 3;condition3.signal();} catch (Exception e) {// Todo: handle exception} finally {lock.unlock();}}public void loopC(int totalLoop) {lock.lock();try {//1.判断while(number!=3) {condition3.await();}//2.打印for (int i = 1; i <= 1; i++) {System.out.println(Thread.currentThread().getName()+"\t"+i+"\t"+totalLoop);}//3.唤醒number = 1;condition1.signal();} catch (Exception e) {// Todo: handle exception} finally {lock.unlock();}}}

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...