最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java ReentrantLock詳解

 更新時間:2019年04月03日 14:29:11   作者:jihite  
這篇文章主要介紹了java ReentrantLock,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

介紹

ReentrantLock稱為重入鎖,比內(nèi)部鎖synchonized擁有更強大的功能,它可中斷、可定時、設(shè)置公平鎖

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

提供以下重要的方法

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

示例

例子1

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");
 }
}

一次執(zhí)行結(jié)果:

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執(zhí)行,secondThread不停的判斷是否可以獲得鎖,當(dāng)firstThread執(zhí)行完,secondThread執(zhí)行后被打斷

例子2

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");
 }
}

一次執(zhí)行結(jié)果

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執(zhí)行,secondThread等待,等待過程被打斷。打斷后firstThread執(zhí)行結(jié)束了,secondThread得到鎖,繼續(xù)執(zhí)行

例子3

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");
 }
}

一次執(zhí)行結(jié)果

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先獲得鎖執(zhí)行,secondThread在等待,此時中斷并未打斷等待。firstThread執(zhí)行完,secondThread獲取后被打斷

例子4

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");
 }
}

一次執(zhí)行結(jié)果

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();在執(zhí)行過程中可以響應(yīng)中斷時間

以上所述是小編給大家介紹的java ReentrantLock詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • java 中sleep() 和 wait() 的對比

    java 中sleep() 和 wait() 的對比

    這篇文章主要介紹了java 中sleep() 和 wait() 的對比的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • SpringBoot事務(wù)不回滾的解決方案

    SpringBoot事務(wù)不回滾的解決方案

    這篇文章主要介紹了SpringBoot事務(wù)不回滾的解決方案的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • J2EE基礎(chǔ)之EJB全面了解

    J2EE基礎(chǔ)之EJB全面了解

    下面小編就為大家?guī)硪黄狫2EE基礎(chǔ)之EJB全面了解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • Spring Boot中如何使用Convert接口實現(xiàn)類型轉(zhuǎn)換器

    Spring Boot中如何使用Convert接口實現(xiàn)類型轉(zhuǎn)換器

    這篇文章主要介紹了Spring Boot中使用Convert接口實現(xiàn)類型轉(zhuǎn)換器的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot使用ip2region獲取地理位置信息的方法

    SpringBoot使用ip2region獲取地理位置信息的方法

    這篇文章主要介紹了SpringBoot使用ip2region獲取地理位置信息的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 小程序與后端Java接口交互實現(xiàn)HelloWorld入門

    小程序與后端Java接口交互實現(xiàn)HelloWorld入門

    本文主要介紹了小程序與后端Java接口交互實現(xiàn)HelloWorld入門 ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Java代理模式實例分析

    Java代理模式實例分析

    這篇文章主要介紹了Java代理模式,結(jié)合實例形式對比分析了java代理模式的使用方法與相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • springboot整合mybatis流程詳解

    springboot整合mybatis流程詳解

    這篇文章主要為大家詳細(xì)介紹了springboot整合mybatisplus的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-05-05
  • SpringBoot項目中連接Gauss數(shù)據(jù)庫

    SpringBoot項目中連接Gauss數(shù)據(jù)庫

    本文主要介紹了SpringBoot項目中連接Gauss數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Java發(fā)展史之Java由來

    Java發(fā)展史之Java由來

    本文主要給大家簡單講解了一下java的發(fā)展史,詳細(xì)說明了java的由來以及如何一步步發(fā)展起來的,想了解的小伙伴可以來參考下
    2016-10-10

最新評論

那坡县| 揭东县| 河池市| 于都县| 鸡泽县| 秦皇岛市| 宁乡县| 太康县| 九江县| 霸州市| 林周县| 滁州市| 吉木乃县| 石首市| 丰原市| 洛宁县| 禄劝| 宜兰市| 廉江市| 松桃| 石狮市| 卓尼县| 满洲里市| 桂阳县| 开远市| 定州市| 澄迈县| 荔波县| 宁陕县| 顺平县| 丹寨县| 安康市| 措美县| 南部县| 凯里市| 绥江县| 丽江市| 双城市| 黔东| 阜阳市| 岑巩县|