欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java ReentrantLock詳解

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

介紹

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

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

提供以下重要的方法

  1. lock():獲得鎖,如果鎖已被占用,則等待
  2. lockInterruptibly():獲得鎖,但有限響應中斷
  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í)行結果:

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不停的判斷是否可以獲得鎖,當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í)行結果

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í)行結束了,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í)行結果

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í)行結果

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í)行過程中可以響應中斷時間

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

相關文章

  • Java使用agent實現(xiàn)main方法之前的實例詳解

    Java使用agent實現(xiàn)main方法之前的實例詳解

    這篇文章主要介紹了Java使用agent實現(xiàn)main方法之前的實例詳解的相關資料,希望通過本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • 使用Java代碼實現(xiàn)Redis和數(shù)據(jù)庫數(shù)據(jù)同步

    使用Java代碼實現(xiàn)Redis和數(shù)據(jù)庫數(shù)據(jù)同步

    這篇文章主要介紹了使用Java代碼實現(xiàn)Redis和數(shù)據(jù)庫數(shù)據(jù)同步問題,文中通過代碼示例給大家講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • Java中集合關系圖及常見操作詳解

    Java中集合關系圖及常見操作詳解

    這篇文章主要為大家詳細介紹了Java中集合關系圖及常見操作,解析Java中的集合類型的繼承關系圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 深入解析Java編程中的抽象類

    深入解析Java編程中的抽象類

    這篇文章主要介紹了Java編程中的抽象類,抽象類體現(xiàn)了Java面向?qū)ο缶幊痰奶匦?需要的朋友可以參考下
    2015-10-10
  • Java類加載策略之雙親委派機制全面分析講解

    Java類加載策略之雙親委派機制全面分析講解

    這篇文章主要介紹了Java雙親委派機制,雙親委派模型是Java加載類的機制,采用雙親委派模型的好處是Java類隨著它的類加載器一起具備了一種帶有優(yōu)先級的層級關系,通過這種層級關系可以避免類的重復加載,感興趣的朋友可以參考下
    2023-12-12
  • Java鍵值對Pair的使用方式和操作實現(xiàn)

    Java鍵值對Pair的使用方式和操作實現(xiàn)

    鍵值對是一種常見的數(shù)據(jù)結構,它由一個唯一的鍵和與之關聯(lián)的值組成,本文就來介紹一下Java鍵值對Pair的使用方式和操作實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • JFileChooser實現(xiàn)對選定文件夾內(nèi)圖片自動播放和暫停播放實例代碼

    JFileChooser實現(xiàn)對選定文件夾內(nèi)圖片自動播放和暫停播放實例代碼

    這篇文章主要介紹了JFileChooser實現(xiàn)對選定文件夾內(nèi)圖片自動播放和暫停播放實例代碼,需要的朋友可以參考下
    2017-04-04
  • Java由淺入深帶你精通繼承super

    Java由淺入深帶你精通繼承super

    繼承就是子類繼承父類的特征和行為,使得子類對象(實例)具有父類的實例域和方法,或子類從父類繼承方法,使得子類具有父類相同的行為
    2022-03-03
  • Java設計模式之單一職責原則精解

    Java設計模式之單一職責原則精解

    設計模式(Design pattern)代表了最佳的實踐,通常被有經(jīng)驗的面向?qū)ο蟮能浖_發(fā)人員所采用。設計模式是軟件開發(fā)人員在軟件開發(fā)過程中面臨的一般問題的解決方案。本篇介紹設計模式七大原則之一的單一職責原則
    2022-02-02
  • Java中對象的克隆詳解

    Java中對象的克隆詳解

    這篇文章主要介紹了Java中對象的克隆詳解,Java中對象的復制分為淺復制與深復制的不同之處就在于深復制還會復制對象的引用對象,需要的朋友可以參考下
    2023-08-08

最新評論