1. 程式人生 > >3.1.3 允許多個線程同時訪問:信號量

3.1.3 允許多個線程同時訪問:信號量

post shu ase current getname string 獲取 static map

package 第三章.信號量;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
* Created by zzq on 2018/1/23.
*/
public class SimpoTest implements Runnable {
final Semaphore semaphore=new Semaphore(5);
public void run() {
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "-------獲取執行權");
Thread.sleep(2000);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(20);
final SimpoTest simpoTest=new SimpoTest();
for (int i = 0; i < 10; i++) {
executorService.submit(simpoTest);
}
executorService.shutdown();

}
}

3.1.3 允許多個線程同時訪問:信號量