GithubHelp home page GithubHelp logo

coding's People

Contributors

comsir avatar

coding's Issues

面试题:lock和synchnoized区别 用新的lock 有什么好处

高并发锁的发展
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要么随机唤醒一个线程,要么唤醒全部线程

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.