此类的主要作用就是限制线程并发的数量。
public class Service {
private Semaphore semaphore=new Semaphore(2);//同一时间做多允许一个线程执行
public void testMethod(){
try {
semaphore.acquire();// 使用掉一个许可,减法
System.out.println(Thread.currentThread().getName()+"begin timer="+System.currentTimeMillis());
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+"end timer"+System.currentTimeMillis());
semaphore.release();//创建一个许可
}catch (Exception e){
System.out.println("线程"+Thread.currentThread().getName()+"进入了catch");
e.fillInStackTrace();
}
}
}
public class ThreadA extends Thread {
private Service service;
public ThreadA(Service service){
super();
this.service=service;
}
public void run(){
service.testMethod();
}
}
public class ThreadB extends Thread {
private Service service;
public ThreadB(Service service){
super();
this.service=service;
}
public void run(){
service.testMethod();
}
}
public class ThreadC extends Thread {
private Service service;
public ThreadC(Service service){
super();
this.service=service;
}
public void run(){
service.testMethod();
}
}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
Service service=new Service();
ThreadA a=new ThreadA(service);
a.setName("A");
a.start();
ThreadB b=new ThreadB(service);
b.setName("B");
b.start();
Thread.sleep(1000);
b.interrupt();//中断线程B
System.out.println("main中断了b");
}
}
public class Service {
private Semaphore semaphore=new Semaphore(1);
public void testMethod(){
semaphore.acquireUninterruptibly();// 不可中断
System.out.println(Thread.currentThread().getName()+"begin timer="+System.currentTimeMillis());
for (int i=0;i<Integer.MAX_VALUE/50;i++){
String newString =new String();
Math.random();
}
System.out.println(Thread.currentThread().getName()+"end timer="+System.currentTimeMillis());
semaphore.release();
}
}
public class MyService {
private Semaphore semaphore=new Semaphore(10);
public void testMethod(){
try {
semaphore.acquire();
System.out.println(semaphore.availablePermits());//获取当前的许可数
System.out.println(semaphore.availablePermits()+" "+semaphore.drainPermits());//获取当前许可数,并将许可数清零
System.out.println(semaphore.availablePermits());
}catch (Exception e){
e.fillInStackTrace();
}finally {
semaphore.release();
}
}
}
public class MyService {
private Semaphore semaphore=new Semaphore(1);
public void testMethod(){
try {
semaphore.acquire();
Thread.sleep(1000);
System.out.println("大约还有"+semaphore.getQueueLength()+"个线程等待");
System.out.println("是否还有线程在等待信号量呢"+semaphore.hasQueuedThreads());
}catch (Exception e){
e.fillInStackTrace();
}finally {
semaphore.release();
}
}
}
public class MyService {
private Semaphore semaphore=new Semaphore(1,true);//true为公平,false为不公平
public void testMethod(){
try {
semaphore.acquire();
Thread.sleep(1000);
System.out.println("大约还有"+semaphore.getQueueLength()+"个线程等待");
System.out.println("是否还有线程在等待信号量呢"+semaphore.hasQueuedThreads());
}catch (Exception e){
e.fillInStackTrace();
}finally {
semaphore.release();
}
}
}
public class RepastService {
volatile private Semaphore setSemaphore = new Semaphore(10);//厨师
volatile private Semaphore getSemaphore = new Semaphore(20);//就餐者
volatile private ReentrantLock lock = new ReentrantLock();
volatile private Condition setCondition = lock.newCondition();
volatile private Condition getCondition = lock.newCondition();
//producePosition 变量含义是最多只有4个盒子存放菜品
volatile private Object[] producePosition = new Object[4];
private boolean isEmpty() {
boolean isEmpty = true;
for (int i = 0; i < producePosition.length; i++) {
if (producePosition[i] != null) {
isEmpty = false;
break;
}
}
if (isEmpty == true) {
return true;
} else {
return false;
}
}
private boolean isFull() {
boolean isFull = true;
for (int i = 0; i < producePosition.length; i++) {
if (producePosition[i] == null) {
isFull = false;
break;
}
}
return isFull;
}
public void set() {
try {
setSemaphore.acquire();
lock.lock();
while (!isEmpty()) {
getCondition.await();//生产者等待
}
for (int i = 0; i < producePosition.length; i++) {
if (producePosition[i] == null) {
producePosition[i] = "数据";
System.out.println(Thread.currentThread().getName() + "生产了" + producePosition[i]);
break;
}
}
getCondition.signalAll();
lock.unlock();
} catch (Exception e) {
e.fillInStackTrace();
} finally {
setSemaphore.release();
}
}
public void get() {
try {
getSemaphore.acquire();//允许同时16个人就餐
lock.lock();
while (isEmpty()) {
getCondition.await();
}
for (int i = 0; i < producePosition.length; i++) {
if (producePosition[i] != null) {
System.out.println(Thread.currentThread().getName() + "消费了" + producePosition[i]);
producePosition[i] = null;
break;
}
}
setCondition.signalAll();
lock.unlock();
} catch (Exception e) {
e.fillInStackTrace();
} finally {
getSemaphore.release();
}
}
}
public class ThreadC extends Thread {
private RepastService service;
public ThreadC(RepastService service){
super();
this.service=service;
}
public void run(){
service.get();
}
}
public class ThreadP extends Thread {
private RepastService service;
public ThreadP(RepastService service){
super();
this.service=service;
}
public void run(){
service.set();
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
RepastService service=new RepastService();
ThreadP[] arrayP=new ThreadP[60];
ThreadC[] arrayC=new ThreadC[60];
for (int i=0;i<60;i++){
arrayP[i]=new ThreadP(service);
arrayC[i]=new ThreadC(service);
}
Thread.sleep(2000);
for (int i=0;i<60;i++){
arrayP[i].start();
arrayC[i].start();
}
}
}