comsir / coding Goto Github PK
View Code? Open in Web Editor NEWJava Demo Summary
Java Demo Summary
高并发锁的发展
1.0 sync/wait/notify ---->2.0lock/await/singal---->3.0 volatile/cas/atomicInteger/blockQueue
答:
1.原始构成
synchronized是关键字属于JVM层面
底层主要是monitorenter(底层是通过monitor对象完成,其实wait和notify等方法也依赖于monitor对象 只有在同步块或者方法中才能调用wait/notify等方法)
monitorexit
Lock是具体类(java.util.concurrent.locks.Lock)是API层面的锁
2.使用方法
synchronized 不需要用户去手动释放锁,当snchronized代码执行完后系统会自动让线程释放对锁的占用(monitorexit)
ReentrantLock则需要用户手动去释放锁,若没有主动释放锁,就有可能出现死锁现象
需要lock() unlock()方法配合try/finally语句块开完成
3.等待是否可中断
synchronized 不可中断,除非抛出异常或者正常运行完成
ReentrantLock 可中断,1设置超时时间方法:tryLock(long timeout, TimeUnit unit)
2.lockInterruptibly()放代码块中,调用interrupt()方法可中断
4 加锁是否公平
synchronized 非公平锁
ReentrantLock两者都可以,默认非公平锁,构造方法可以传入boolean值,true为公平锁,false为非公平锁
5.锁绑定多个条件Condition
synchronized没有
ReentrantLock用来实现分组唤醒需要唤醒的线程们,可以精确唤醒,而不是像sychrionzed要么随机唤醒一个线程,要么唤醒全部线程
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.