一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - java ReentrantLock詳解

java ReentrantLock詳解

2021-07-28 11:56jihite Java教程

這篇文章主要介紹了java ReentrantLock,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

介紹

reentrantlock稱為重入鎖,比內部鎖synchonized擁有更強大的功能,它可中斷、可定時、設置公平鎖

【注】使用reentrantlock時,一定要釋放鎖,一般釋放放到finnal里寫。

提供以下重要的方法

  1. lock():獲得鎖,如果鎖已被占用,則等待
  2. lockinterruptibly():獲得鎖,但有限響應中斷
  3. unlock():釋放鎖
  4. trylock():嘗試獲取鎖。如果獲得,返回true;否則返回false
  5. trylock(long time, timeunit unit):在給定時間內獲得鎖。如果獲得返回true;否則返回false

示例

例子1

java" id="highlighter_233156">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.concurrent.locks.reentrantlock;
 
public class reentrantlocktest {
 reentrantlock lock;
 
 reentrantlocktest(reentrantlock lock) {
  this.lock = lock;
 }
 
 private runnable getrunnable() {
  return new runnable() {
   @override
   public void run() {
    while(true) {
     try {
      if (lock.trylock()) {
       try {
        system.out.println("locked:" + thread.currentthread().getname());
        thread.sleep(800);
       } finally {
        lock.unlock();
        system.out.println("unlocked:" + thread.currentthread().getname());
       }
       system.out.println("break before");
       break;
      } else {
       //system.out.println("unable to lock " + thread.currentthread().getname());
      }
 
     } catch (interruptedexception e){
      system.out.println(thread.currentthread() + " is interupted");
      e.printstacktrace();
     }
    }
   }
  };
 }
 
 public static void main(string[] args) {
  reentrantlock lock = new reentrantlock();
  reentrantlocktest test = new reentrantlocktest(lock);
  reentrantlocktest test2 = new reentrantlocktest(lock);
  thread thread1 = new thread(test.getrunnable(), "firstthread");
  thread thread2 = new thread(test2.getrunnable(), "secondthread");
 
  thread1.start();
  thread2.start();
  try {
   thread.sleep(300);
  }catch (interruptedexception e) {
   e.printstacktrace();
  }
  system.out.println("interupt begin");
  thread2.interrupt();
  system.out.println("interupt end");
 }
}

一次執行結果:

locked:firstthread
interupt begin
interupt end
unlocked:firstthread
break before
locked:secondthread
unlocked:secondthread
thread[secondthread,5,main] is interupted
java.lang.interruptedexception: sleep interrupted
    at java.lang.thread.sleep(native method)
    at com.jihite.templet.javabase.reentrantlocktest$1.run(reentrantlocktest.java:23)
    at java.lang.thread.run(thread.java:748)
locked:secondthread
unlocked:secondthread
break before

 分析:firstthread執行,secondthread不停的判斷是否可以獲得鎖,當firstthread執行完,secondthread執行后被打斷

例子2

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.concurrent.timeunit;
import java.util.concurrent.locks.reentrantlock;
 
public class reentrantlocktest {
 reentrantlock lock;
 
 reentrantlocktest(reentrantlock lock) {
  this.lock = lock;
 }
 
 private runnable getrunnable() {
  return new runnable() {
   @override
   public void run() {
    while(true) {
     try {
      if (lock.trylock(700, timeunit.milliseconds)) {
       try {
        system.out.println("locked:" + thread.currentthread().getname());
        thread.sleep(800);
       } finally {
        lock.unlock();
        system.out.println("unlocked:" + thread.currentthread().getname());
       }
       system.out.println("break before");
       break;
      } else {
       //system.out.println("unable to lock " + thread.currentthread().getname());
      }
 
     } catch (interruptedexception e){
      system.out.println(thread.currentthread() + " is interupted");
      e.printstacktrace();
     }
    }
   }
  };
 }
 
 public static void main(string[] args) {
  reentrantlock lock = new reentrantlock();
  reentrantlocktest test = new reentrantlocktest(lock);
  reentrantlocktest test2 = new reentrantlocktest(lock);
  thread thread1 = new thread(test.getrunnable(), "firstthread");
  thread thread2 = new thread(test2.getrunnable(), "secondthread");
 
  thread1.start();
  thread2.start();
  try {
   thread.sleep(300);
  }catch (interruptedexception e) {
   e.printstacktrace();
  }
  system.out.println("interupt begin");
  thread2.interrupt();
  system.out.println("interupt end");
 }
}

一次執行結果

locked:firstthread
interupt begin
interupt end
thread[secondthread,5,main] is interupted
java.lang.interruptedexception
    at java.util.concurrent.locks.abstractqueuedsynchronizer.doacquirenanos(abstractqueuedsynchronizer.java:936)
    at java.util.concurrent.locks.abstractqueuedsynchronizer.tryacquirenanos(abstractqueuedsynchronizer.java:1247)
    at java.util.concurrent.locks.reentrantlock.trylock(reentrantlock.java:442)
    at com.jihite.templet.javabase.reentrantlocktest$1.run(reentrantlocktest.java:19)
    at java.lang.thread.run(thread.java:748)
locked:secondthread
unlocked:firstthread
break before
unlocked:secondthread
break before

分析:firstthread執行,secondthread等待,等待過程被打斷。打斷后firstthread執行結束了,secondthread得到鎖,繼續執行

例子3

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.concurrent.locks.reentrantlock;
 
public class reentrantlocktest2 {
 reentrantlock lock;
 
 reentrantlocktest2(reentrantlock lock) {
  this.lock = lock;
 }
 
 private runnable getrunnable() {
  return new runnable() {
   @override
   public void run() {
    while (true) {
     try {
      try {
       lock.lock();
//       lock.lockinterruptibly();
       system.out.println("locked:" + thread.currentthread().getname());
       thread.sleep(800);
       break;
      } finally {
       lock.unlock();
       system.out.println("unlocked:" + thread.currentthread().getname());
      }
     } catch (interruptedexception e) {
      e.printstacktrace();
     }
    }
   }
  };
 }
 
 public static void main(string[] args) {
  reentrantlock lock = new reentrantlock();
  reentrantlocktest2 test = new reentrantlocktest2(lock);
  reentrantlocktest2 test2 = new reentrantlocktest2(lock);
  thread thread1 = new thread(test.getrunnable(), "firstthread");
  thread thread2 = new thread(test2.getrunnable(), "secondthread");
 
  thread1.start();
  thread2.start();
  try {
   thread.sleep(600);
  }catch (interruptedexception e) {
   e.printstacktrace();
  }
  system.out.println("interupt begin");
  thread2.interrupt();
  system.out.println("interupt end");
 }
}

一次執行結果

locked:firstthread
interupt begin
interupt end
unlocked:firstthread
locked:secondthread
unlocked:secondthread
java.lang.interruptedexception: sleep interrupted
    at java.lang.thread.sleep(native method)
    at com.jihite.templet.javabase.reentrantlocktest2$1.run(reentrantlocktest2.java:22)
    at java.lang.thread.run(thread.java:748)
locked:secondthread
unlocked:secondthread

分析:firstthread先獲得鎖執行,secondthread在等待,此時中斷并未打斷等待。firstthread執行完,secondthread獲取后被打斷

例子4

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class reentrantlocktest2 {
 reentrantlock lock;
 
 reentrantlocktest2(reentrantlock lock) {
  this.lock = lock;
 }
 
 private runnable getrunnable() {
  return new runnable() {
   @override
   public void run() {
    while (true) {
     try {
      try {
//       lock.lock();
       lock.lockinterruptibly();
       system.out.println("locked:" + thread.currentthread().getname());
       thread.sleep(800);
       break;
      } finally {
       lock.unlock();
       system.out.println("unlocked:" + thread.currentthread().getname());
      }
     } catch (interruptedexception e) {
      e.printstacktrace();
     }
    }
   }
  };
 }
 
 public static void main(string[] args) {
  reentrantlock lock = new reentrantlock();
  reentrantlocktest2 test = new reentrantlocktest2(lock);
  reentrantlocktest2 test2 = new reentrantlocktest2(lock);
  thread thread1 = new thread(test.getrunnable(), "firstthread");
  thread thread2 = new thread(test2.getrunnable(), "secondthread");
 
  thread1.start();
  thread2.start();
  try {
   thread.sleep(600);
  }catch (interruptedexception e) {
   e.printstacktrace();
  }
  system.out.println("interupt begin");
  thread2.interrupt();
  system.out.println("interupt end");
 }
}

一次執行結果

locked:firstthread
interupt begin
interupt end
exception in thread "secondthread" java.lang.illegalmonitorstateexception
    at java.util.concurrent.locks.reentrantlock$sync.tryrelease(reentrantlock.java:151)
    at java.util.concurrent.locks.abstractqueuedsynchronizer.release(abstractqueuedsynchronizer.java:1261)
    at java.util.concurrent.locks.reentrantlock.unlock(reentrantlock.java:457)
    at com.jihite.templet.javabase.reentrantlocktest2$1.run(reentrantlocktest2.java:25)
    at java.lang.thread.run(thread.java:748)

分析:lock.lockinterruptibly();在執行過程中可以響應中斷時間

以上所述是小編給大家介紹的java reentrantlock詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本一区视频在线 | jux539原千岁在线播放 | 免费港剧在线观看港剧 | 欧美一区二区三区成人看不卡 | 香蕉免费看一区二区三区 | 日本中文字幕在线视频站 | 国产91一区二区在线播放不卡 | 免费十几分视频 | 特级淫片大乳女子高清视频 | 成人私人影院在线观看网址 | 9420高清完整版在线观看国语 | fistingvideos头交尿眼 | 欧美人曾交 | 国内精品久久久久影院中国 | 亚洲国产一区二区三区青草影视 | 国产精品成人 | 亚洲国产精品二区久久 | 欧美交换乱理伦片120秒 | 午夜综合网 | 四虎传媒 | 国产99视频精品免费视频7 | jiuse视频 | 大奶老太 | 天堂网站天堂小说 | 免费在线看 | 手机能看的黄色网站 | 国内精品久久久久影院男同志 | 古代翁熄乩伦小说h | 毛片一级毛片 | 成人精品视频一区二区在线 | 狠狠干2017 | 日本亚洲娇小与黑人tube | 亚洲国产精品综合一区在线 | 女人pp被扒开流水了 | 国产在线观看网站 | 亚洲国产精品久久人人爱 | 红楼影视h38bar在线线播放 | 免费看视频高清在线观看 | 亚洲天堂男人的天堂 | 美女被绑着吸下部的故事 | 日本亚欧乱色视频在线观看 |