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();}}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。