公平锁与非公平锁
公平锁:多个线程按照申请锁的顺序来获取锁 类似食堂排队,先来后到
非公平锁:是指多个线程获取锁的顺序并不是按照申请锁的顺序,有可能后申请的线程比先申请的线程先拿到锁,在高并发的情况下有可能造成优先级反转和饥饿现象
并发包中 ReentrantLock 可以创建公平锁和非公平锁 默认情况是非公平锁,Sychnoized也是非公平锁,非公平锁的有点是吞吐量大
当一个线程想获取锁时,先试图插队,如果占用锁的线程释放了锁,下一个线程还没来得及拿锁,那么当前线程就可以直接获得锁;如果锁正在被其它线程占用,则排队,排队的时候就不能再试图获得锁了,只能等到前面所有线程都执行完才能获得锁
可重入锁(可以避免死锁)
ReentrantLock 可重入锁(递归锁):同一个线程外层函数获取锁之后,内层函数依然能够获取该锁的代码
在同一个线程在外层方法获取锁的时候,进入内层方法会自动获取该锁
线程可以进入一个已经拥有锁所同步的代码块
Sychnoized和ReentrantLock 默认情况是是非公平的重入锁
后面一个demo会详细的解释这种情况
先来看下Sychnoized 重入锁的情况
package com.zz.study.lock; public class TestSynReentry { public static void main(String[] args) { new Thread( () -> { SendWarningPush(); },"one").start(); new Thread( () -> { SendWarningPush(); },"two").start(); } public static synchronized void SendWarningPush(){ System.out.println(Thread.currentThread().getName()+"\t send push msg"); SendWarningSms(); } public static synchronized void SendWarningSms(){ System.out.println(Thread.currentThread().getName()+"\t send Sms msg"); } } 看下运行的结果 two send push msg two send Sms msg one send push msg one send Sms msg
接下来看下ReentrantLock 重入锁的演示案例
package com.zz.study.lock; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class TestReentrantLock implements Runnable{ Lock lock=new ReentrantLock(); @Override public void run() { getMsg(); } public void getMsg(){ lock.lock(); lock.lock(); try { setMsg(); System.out.println(Thread.currentThread().getName()+"\t get()"); }finally { lock.unlock(); lock.unlock(); } } public void setMsg(){ lock.lock(); try { System.out.println(Thread.currentThread().getName()+"\t set()"); }finally { lock.unlock(); } } public static void main(String[] args) { Thread t1=new Thread(new TestReentrantLock()); Thread t2=new Thread(new TestReentrantLock()); t1.start(); t2.start(); } } 运行结果: Thread-0 set() Thread-0 get() Thread-1 set() Thread-1 get()
Process finished with exit code 0
可以清晰的看到不管是ReentrantLock 还是synchronized 都是可重入的 可以看到同一个线程获取到外层的锁的时候,内层的方法可以自动获取到锁
Where Can I Buy Isotretinoin Skin Health Low Price Mastercard https://agenericcialise.com/ – where to buy cialis cheap Cialis Y Ejercicio Generic Cialis generic cialis pill
http://mewkid.net/when-is-xicix/ – Amoxil Causes Gallstones Amoxicillin No Prescription eez.ejxi.qdcto.com.evk.un http://mewkid.net/when-is-xicix/
http://mewkid.net/when-is-xicix/ – Amoxicillin Amoxil ldj.regh.qdcto.com.zvs.gk http://mewkid.net/when-is-xicix/
Viagra In Farmacia Serve Ricetta Dualspoups https://acialisd.com/# – Cialis Reertevews Order Now Macrobid Website Cheapeast Free Doctor Consultation Bemolymn generic cialis online canada unoche liquid tadalafil
Buy Thyroxine Online India Wicmymmele https://ascialis.com/# – cialis generic cost gaideleplete Acheter Tadalis Sx Original affextop Cialis adoreakder Buy No Prescription Generic Flagyl In Prague
Comprar Cialis Online En Madrid Wicmymmele https://cialiser.com/ – cialis cheapest online prices gaideleplete Donde Comprar Cialis Seguro En Espana affextop Cialis adoreakder Side Affects To Amoxicillin
Cephalexin Alternative For Dogs Wicmymmele cialis order online gaideleplete liasons sp cialis es
viagra
Howdy! Do you know if they make any plugins to help with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.
If you know of any please share. Appreciate it!
What a material of un-ambiguity and preserveness of valuable experience regarding
unexpected emotions.
With havin so much written content do you ever run into any issues of plagorism or copyright violation? My website has a lot of completely unique content I’ve either
created myself or outsourced but it appears a
lot of it is popping it up all over the web without my agreement.
Do you know any techniques to help stop content from being ripped off?
I’d definitely appreciate it.