Java多线程:Semaphore
自從5.0開始,jdk在java.util.concurrent包里提供了Semaphore 的官方實(shí)現(xiàn)。
Java 5.0里新加了4個(gè)協(xié)調(diào)線程間進(jìn)程的同步裝置,它們分別是: Semaphore, CountDownLatch, CyclicBarrier和Exchanger.
Semaphore為并發(fā)包中提供用于控制某資源同時(shí)可以被幾個(gè)線程訪問(wèn)的類。
Semaphore當(dāng)前在多線程環(huán)境下被擴(kuò)放使用,操作系統(tǒng)的信號(hào)量是個(gè)很重要的概念,在進(jìn)程控制方面都有應(yīng)用。Java 并發(fā)庫(kù) 的Semaphore 可以很輕松完成信號(hào)量控制,Semaphore可以控制某個(gè)資源可被同時(shí)訪問(wèn)的個(gè)數(shù),通過(guò) acquire() 獲取一個(gè)許可,如果沒(méi)有就等待,而 release() 釋放一個(gè)許可。
Semaphore維護(hù)了當(dāng)前訪問(wèn)的個(gè)數(shù),提供同步機(jī)制,控制同時(shí)訪問(wèn)的個(gè)數(shù)。在數(shù)據(jù)結(jié)構(gòu)中鏈表可以保存“無(wú)限”的節(jié)點(diǎn),用Semaphore可以實(shí)現(xiàn)有限大小的鏈表。另外重入鎖 ReentrantLock 也可以實(shí)現(xiàn)該功能,但實(shí)現(xiàn)上要復(fù)雜些。
Constructor and Description
Semaphore(int permits)
Creates a Semaphore with the given number of permits and nonfair fairness setting.
permits 初始許可數(shù),最大訪問(wèn)線程數(shù)
Semaphore(int permits, boolean fair)
Creates a Semaphore with the given number of permits and the given fairness setting.
permits 初始許可數(shù),最大訪問(wèn)線程數(shù)
fair 當(dāng)設(shè)置為false時(shí),線程獲取許可的順序是無(wú)序的,也就是說(shuō)新線程可能會(huì)比等待的老線程會(huì)先獲得許可;當(dāng)設(shè)置為true時(shí),信號(hào)量保證它們調(diào)用的順序(即先進(jìn)先出;FIFO)
主要方法:
void acquire() 從信號(hào)量獲取一個(gè)許可,如果無(wú)可用許可前 將一直阻塞等待,
void acquire(int permits) 獲取指定數(shù)目的許可,如果無(wú)可用許可前 也將會(huì)一直阻塞等待
boolean tryAcquire() 從信號(hào)量嘗試獲取一個(gè)許可,如果無(wú)可用許可,直接返回false,不會(huì)阻塞
boolean tryAcquire(int permits) 嘗試獲取指定數(shù)目的許可,如果無(wú)可用許可直接返回false,
boolean tryAcquire(int permits, long timeout, TimeUnit unit) 在指定的時(shí)間內(nèi)嘗試從信號(hào)量中獲取許可,如果在指定的時(shí)間內(nèi)獲取成功,返回true,否則返回false
void release() 釋放一個(gè)許可,別忘了在finally中使用,注意:多次調(diào)用該方法,會(huì)使信號(hào)量的許可數(shù)增加,達(dá)到動(dòng)態(tài)擴(kuò)展的效果,如:初始permits 為1, 調(diào)用了兩次release,最大許可會(huì)改變?yōu)?
int availablePermits() 獲取當(dāng)前信號(hào)量可用的許可
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit;public class SemaphoreTest {public static void main(String[] args) {// 線程池ExecutorService exec = Executors.newCachedThreadPool();// 只能5個(gè)線程同時(shí)訪問(wèn)final Semaphore semp = new Semaphore(5);// 模擬20個(gè)客戶端訪問(wèn)for (int index = 0; index < 20; index++) {final int NO = index;Runnable run = new Runnable() {public void run() {try {// 獲取許可semp.acquire();System.out.println("Accessing: " + NO);// Thread.sleep((long) (Math.random() * 10000));TimeUnit.SECONDS.sleep((long) (Math.random()*1000));// 訪問(wèn)完后,釋放semp.release();System.out.println("----------availablePermits-------" + semp.availablePermits());} catch (InterruptedException e) {e.printStackTrace();}}};exec.execute(run);}// 退出線程池exec.shutdown();}}
總結(jié)
以上是生活随笔為你收集整理的Java多线程:Semaphore的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 大数据文件分隔符
- 下一篇: Oracle_杂货箱_不定期更新