Threads - Why a Lock has to be followed by try and finally

前端 未结 6 2215
北荒
北荒 2020-12-03 13:09

A Lock is always followed by a try/finally block, why?

ReentrantReadWriteLock readWriteLockBitmap = new ReentrantReadWriteLock();
Lock read = readWriteLockBi         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-03 13:22

    In case anything goes wrong (Exception being thrown etc.) you want to make sure the lock is released no matter what. It's just standard practice, even though in this case may technically be unnecessary.

提交回复
热议问题