标签:ant nal imp strong while lib catch thread rtl
1.ReentrantLock
2.ReentrantLock的实现
3.synchronized和ReentrantLock的区别
public class ReentrantLockDemo implements Runnable{ private static ReentrantLock lock = new ReentrantLock(true); @Override public void run(){ while (true){ try{ lock.lock(); System.out.println(Thread.currentThread().getName() + " get lock"); Thread.sleep(1000); } catch (Exception e){ e.printStackTrace(); } finally { lock.unlock(); } } } public static void main(String[] args) { ReentrantLockDemo rtld = new ReentrantLockDemo(); Thread thread1 = new Thread(rtld); Thread thread2 = new Thread(rtld); thread1.start(); thread2.start(); } }
只有当ReentrantLock构造中传入为true时才是公平锁
Java多线程和并发(九),ReentrantLock(公平锁)
标签:ant nal imp strong while lib catch thread rtl
原文地址:https://www.cnblogs.com/xzmxddx/p/10362851.html