Java??????ReentrantLock
???????????? ???????[ 2015/6/18 14:00:12 ] ???????????????
??????
????????ó?????????????????ReentrantLock????????????????????Щ??
????????1?????????ò???????????????????У???????У?
????a???????????????????????????????????′μ?????????????????????????????????У??????????????
????b????????潻????????н????????????????????ε?????o???????У????????????????
???????????????????????з???????????????У????磺???????????????????Щ????????????????????????
1 private ReentrantLock lock = new ReentrantLock();
2
3 if (lock.tryLock()) { //????????lock????????????false???????????????????Ч??
4
5 try {
6
7 //????
8
9 } finally {
10 lock.unlock();
11 }
12
13 }
????e.g ??????????????????????????δ?????????????????ж??ε????????
????????2?????????ò??????????У??????????У??????У?????synchronized??
????????????????????????????????????ó?????????????????????????????????????
????????synchronized?????????????????????????jvm??????????????С??????Lock?и??????????????????????????????synchronized??????????
???????????????????????????????磺????????????????????????????????
????ReentrantLock????????????????
????private ReentrantLock lock = new ReentrantLock(); //???????false?????????
????private ReentrantLock lock = new ReentrantLock(true); //?????
????try {
????lock.lock(); //??????????????????????????????????????Ч??
????//????
????} finally {
????lock.unlock();
????}
??????????????????????
???????????£????????????????????У?????????????????????????????????
?????????????£????????????????jvm???????????δ?????????????????????????????????????????
????????3?????????ò??????????У???????????????????????У?????????У?
??????????????????2??????????????????????????????????????????????С?
????????????????????????????????????????????????????????????????????????????
1 try {
2 if (lock.tryLock(5?? TimeUnit.SECONDS)) { //????????lock????????5s???????????????????5s?????????????????false???????
3
4
5 try {
6
7
8 //????
9
10
11 } finally {
12 lock.unlock();
13 }
14
15
16 }
17 } catch (InterruptedException e) {
18 e.printStackTrace(); //????????ж??(interrupt)??????InterruptedException
19 }
20
????????4?????????ò??????????У??????С???????ж???????е??????????????????????????
????synchronized??Lock???????????????????ж?(interrupt)????????????????lockInterruptibly()??????ж????????????????????2?????????????г??????????ж?????????
??????????????????????Щ?????????????á??磺???????????????е???????????????????????????????????????
????try {
????lock.lockInterruptibly();
????//????
????} catch (InterruptedException e) {
????e.printStackTrace();
????} finally {
????lock.unlock();
????}
?????????????
???????????????????????“?????????????(Parallel computing)”??????????????reentrant??re-entrant?????????????????????????????????ν???????????????????? ???????н???????????????????????????????????????????????????????
??????????????????ó?????????????ReentrantLock?о????????????????
????1??lock ??????????????????????????????
????2??lockInterruptibly ???????????????????ж????????????У???????ж??
????3??tryLock() ????????????????????true.??????????????????
????4??tryLock(long time??TimeUnit unit) ???????????????????
????5??unlock()??????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11