日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

同步器 java_您可能不知道的五个高级Java同步器

發布時間:2023/12/3 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 同步器 java_您可能不知道的五个高级Java同步器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

同步器 java

除了基于每個Java對象具有的鎖定位的通用同步外,您還可以使用Java中更復雜的同步器,例如:

  • 信號量 –使用許可的概念表示一個位置中允許的最大線程數。 當使用值1時,其行為類似于同步,也稱為二進制信號量。 但是,這里有很大的不同,您在信號量上獲得許可,而不是鎖定對象,它只是一個變量,用于在線程獲得許可時進行計數,而在線程釋放許可時進行計數。 您真正擁有的唯一東西是線程鎖定,直到獲得許可為止。 在下面的示例中,我們將3定義為允許的數量,因此在3 獲得之后 ,4線程將等待釋放,然后繼續執行。
// Define the semaphore to control 3 permits. // 3 Threads can acquire the mySemaphore Semaphore mySemaphore = new Semaphore(3, true);// 3 threads can execute this line of code. The 4 thread must wait for a release mySemaphore.acquire();// .. somewhere in the code a thread releases the mySemaphore, // and now the next waiting thread can acquire mySemaphore.release();
    • CountDownLatch –用一個數字初始化該類(倒數),當達到0時,線程等待解除阻塞并遵循其方式。 (在等待之后 ,閂鎖無法重復使用)
// Initializes a countdown starting from 3 CountDownLatch latch = new CountDownLatch(3);// ... other threads are running... // Some thread blocks and waits for the latch countdown // to reach "0" latch.await();// ... code, methods, other objects... etc...// ... at some place the OTHER threads do the countdown, // decrementing the latch.. when it reachs 0 // the blocked thread with the "await()" follows its way latch.countDown();
  • CyclicBarrier –此類的行為與CountDownLatch相反。 在N await()之后 ,被阻塞的線程可以按照自己的方式進行。 (可以重用CyclicBarrier)
// 3 threads must await before can unblock CyclicBarrier barrier = new CyclicBarrier(3);// threads they block here until the 3 is reached barrier.await();// after 3 threads in await this code will run! System.out.println("Thank you to the 3 friends who awaited for me!”);
  • 移相器 –非常復雜的同步器,由CountDownLatch和CyclicBarrier混合而成,具有許多自定義選項。 如果您需要一個類似于2個以前的同步器的行為,但是它們還不夠,那么您想深入研究一下這個同步器。 它的行為類似于CyclicBarrier,但是您可以注冊一組線程并隨時注銷,以實現其他同步器無法實現的自定義級別。 考慮是否需要等待線程到達,然后才能繼續或啟動另一組任務。 在Oracle網站上有關此的更多信息:

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Phaser.html

void runTasks(List<Runnable> tasks) {// Initialize the phaser, "1" to register selffinal Phaser phaser = new Phaser(1); // create and start threadsfor (final Runnable task : tasks) {// register herephaser.register();new Thread() {public void run() {// await all creationphaser.arriveAndAwaitAdvance(); task.run();}}.start();}// allow threads to start and deregister selfphaser.arriveAndDeregister();}
  • Exchanger –最好的解釋來自Oracle文檔本身: “一個同步點,線程可以在該同步點進行配對并在配對中交換元素 ”。 一個線程想將信息發送到另一個線程并阻塞等待發送數據,而在EXCHANGE中也接收另一個線程想發送的信息! 雙方都會發生這種行為!
// Create the exchanger. // We choose String as the data datatype Exchanger<String> ex = new Exchanger<String>();// // .... Somewhere at Thread 1, //// I will block until I can send str1 to Thread 2, and receive a value too! String str1 = "value to send from Thread 1 to Thread 2"; String valueReturnedFromThread2 = ex.exchange(str1);// // ... Somewhere at Thread 2, //// I will block until I can send str2 to Thread 1 // I will receive a value from Thread 1 too! String str2 = "value to send to Thread 1 from Thread 2"; String valueReturnedFromThread1 = ex.exchange(str2);

參考: Lviv的Java用戶組博客中的JCG合作伙伴 Bohdan Bandrivskyy, 您可能不知道五個高級Java同步器 。

翻譯自: https://www.javacodegeeks.com/2013/05/five-advanced-java-synchronizers-you-probably-dont-know.html

同步器 java

總結

以上是生活随笔為你收集整理的同步器 java_您可能不知道的五个高级Java同步器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。