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

詳解java中的互斥鎖信號量和多線程等待機(jī)制

 更新時間:2017年09月22日 09:19:54   作者:feifeiwendao  
這篇文章主要介紹了Java編程中的互斥鎖,信號量和多線程等待機(jī)制實(shí)例詳解,簡單介紹了互斥鎖和信號量的區(qū)別,需要的朋友可以了解下。

互斥鎖和信號量都是操作系統(tǒng)中為并發(fā)編程設(shè)計基本概念,互斥鎖和信號量的概念上的不同在于,對于同一個資源,互斥鎖只有0和1 的概念,而信號量不止于此。也就是說,信號量可以使資源同時被多個線程訪問,而互斥鎖同時只能被一個線程訪問
互斥鎖在java中的實(shí)現(xiàn)就是 ReetranLock , 在訪問一個同步資源時,它的對象需要通過方法 tryLock() 獲得這個鎖,如果失敗,返回 false,成功返回true。根據(jù)返回的信息來判斷是否要訪問這個被同步的資源??聪旅娴睦?/p>

public class ReentranLockExample {
 private static int count = 0;
 private static ReentrantLock reentrantLock = new ReentrantLock();
 static class MyThread extends Thread{
  @Override
  public void run() {
   super.run();
   try {
    while (true){
     boolean result = reentrantLock.tryLock();
     if (result){
      System.out.println(Thread.currentThread().getName() + "get the lock success and run the syn code " + count ++);
      reentrantLock.unlock();
     }else{
      System.out.println(Thread.currentThread().getName() + "get the lock failed and run the syn code " + count);
     }
     System.out.println(Thread.currentThread().getName() + "run the asyntronized code " + count);
     Thread.sleep(500);
    }
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
 public static void main(String[] args){
  MyThread thread1 = new MyThread();
  MyThread thread2 = new MyThread();
  thread1.start();
  thread2.start();
 }
}

信號量相當(dāng)于一個計數(shù)器,如果線程想要訪問某個資源,則先要獲得這個資源的信號量,并且信號量內(nèi)部的計數(shù)器減1 ,信號量內(nèi)部的計數(shù)器大于0則意味著有可以使用的資源,當(dāng)線程使用完某個資源時,必須釋放這個資源的信號量。信號量的一個作用就是可以實(shí)現(xiàn)指定個線程去同事訪問某個資源。只需要在初始化 。

信號量在 Java中的實(shí)現(xiàn)是 Semaphore  ,其在初始化時傳入一個整型數(shù), 用來指定同步資源最大的并發(fā)訪問量

public class SemaphoreExample {
 private static Semaphore semaphore = new Semaphore(2);
 private String lock = "lock";
 private static int count = 0;
 static class MyThread extends Thread {
  @Override
  public void run() {
   super.run();
   try {
    while (true) {
     semaphore.acquire();
     Thread.sleep(500);
     System.out.println(Thread.currentThread().getName() + "get the lock success and run the syn code " + count++);
     semaphore.release();
     Thread.sleep(500);
    }
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
 public static void main(String[] args){
  MyThread thread1 = new MyThread();
  MyThread thread2 = new MyThread();
  MyThread thread3 = new MyThread();
  thread1.start();
  thread2.start();
  thread3.start();
 }
}

 CountDownLatch 實(shí)現(xiàn)一個等待機(jī)制,在諸如 等待與會者到達(dá)后,開始會議的使用中。ConutDownLatch 在初始化中一個計數(shù)器,用來指定需要等待的個數(shù)。在并發(fā)編程中,所解決的需求就是,等待所有的線程到達(dá)某個點(diǎn)后。才開始進(jìn)行下一步,有點(diǎn)類似于開會,只有當(dāng)所有的與會人員都到齊后,會議才能開始

public class CountDownLatchExample {
 private static CountDownLatch mCountDownLatch = new CountDownLatch(3);
 static class MyThread extends Thread {
  int awaitTime;
  public MyThread(int i) {
   this.awaitTime = i;
  }
  @Override
  public void run() {
   super.run();
   try {
    while (true) {
     Thread.sleep(awaitTime);
     System.out.println(Thread.currentThread().getName() + "arrived " );
     mCountDownLatch.countDown();
     mCountDownLatch.await(); //可以指定等待時間
     System.out.println(Thread.currentThread().getName() + "start meeting " );
    }
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
 public static void main(String[] args){
  MyThread thread1 = new MyThread(500);
  MyThread thread2 = new MyThread(1000);
  MyThread thread3 = new MyThread(2000);
  thread1.start();
  thread2.start();
  thread3.start();
 }
}

總結(jié)

以上就是本文有關(guān)Java編程中的互斥鎖,信號量和多線程等待機(jī)制實(shí)例詳解的全部內(nèi)容,希望對大家有所幫助。

有興趣的朋友可以了解:Java多線程賣票實(shí)例、Java多線程并發(fā)編程(互斥鎖Reentrant Lock)等。

相關(guān)文章

  • java實(shí)現(xiàn)哈夫曼文件解壓縮

    java實(shí)現(xiàn)哈夫曼文件解壓縮

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)哈夫曼文件解壓縮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • SpringBoot集成slf4j日志配置的方法

    SpringBoot集成slf4j日志配置的方法

    本文主要介紹了SpringBoot集成slf4j日志配置的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Java ==,equals()與hashcode()的使用

    Java ==,equals()與hashcode()的使用

    本文主要介紹了Java ==,equals()與hashcode()的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 深入Spring Boot之ClassLoader的繼承關(guān)系和影響

    深入Spring Boot之ClassLoader的繼承關(guān)系和影響

    這篇文章主要介紹了深入Spring Boot之ClassLoader的繼承關(guān)系和影響,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • SpringCloud-Gateway轉(zhuǎn)發(fā)WebSocket失敗問題及解決

    SpringCloud-Gateway轉(zhuǎn)發(fā)WebSocket失敗問題及解決

    這篇文章主要介紹了SpringCloud-Gateway轉(zhuǎn)發(fā)WebSocket失敗問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 使用Java語言實(shí)現(xiàn)一個冒泡排序

    使用Java語言實(shí)現(xiàn)一個冒泡排序

    冒泡排序也是一種簡單直觀的排序算法,基本思想是多次遍歷要排序的數(shù)組,每次比較相鄰的元素,如果順序不對就交換它們的位置,本篇文章將通過IDE使用 Java 語言實(shí)現(xiàn)一個冒泡排序,需要的朋友可以參考下
    2024-03-03
  • C語言指針數(shù)組案例詳解

    C語言指針數(shù)組案例詳解

    這篇文章主要介紹了C語言指針數(shù)組案例詳解,本文通過案例詳細(xì)的解釋了指針與數(shù)組的初始化還有關(guān)系與應(yīng)用,需要的朋友可以參考下這篇文章
    2021-07-07
  • 淺談Java并發(fā)編程中的線程

    淺談Java并發(fā)編程中的線程

    這篇文章主要介紹了淺談Java并發(fā)編程中的線程,操作系統(tǒng)運(yùn)行一個程序,就會創(chuàng)建一個進(jìn)程,在一個進(jìn)程里可以創(chuàng)建多個線程,因此線程也叫做輕量級進(jìn)程,需要的朋友可以參考下
    2023-08-08
  • 利用mysql實(shí)現(xiàn)的雪花算法案例

    利用mysql實(shí)現(xiàn)的雪花算法案例

    這篇文章主要介紹了利用mysql實(shí)現(xiàn)的雪花算法案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • SpringBoot自動裝配Condition的實(shí)現(xiàn)方式

    SpringBoot自動裝配Condition的實(shí)現(xiàn)方式

    這篇文章主要介紹了SpringBoot自動裝配Condition的實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08

最新評論