–Semaphore信号量的作用

  • –Semaphore信号量经常用于限制获取某种资源的线程数量, 比如下面这个例子:
    操场上游5个跑道, 一个跑道一次只能有一个学生在上面跑步, 一旦所有跑道在使用, 那么后面的学生就需要等待, 知道有一个学生不跑了

    • acquire() //获取一个信号量
    • release() //执行完成之后释放信号量

–案例1

package com.ygq.thread.threadPool;

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

/**
 * @author :GQ.Yin
 * @date :Created in 2019/7/22 14:14
 * @description:信号量示例
 * @version: $version$
 */
public class SemaPhoreSample {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newCachedThreadPool();//创建一个可缓存线程池
        final Semaphore semaphore = new  Semaphore(5); //定义5个信号量, 也就是只允许5个同时运行
        for (int i = 1; i < 20; i++) {
            final int index = i;
            threadPool.execute(new Runnable() {
                public void run() {
                    try {
                        semaphore.acquire();  //获取一个信号量, 占用一个跑道

                        play();  //调用play方法

                        semaphore.release();  //执行完成之后,释放信号量
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        threadPool.shutdown();


    }


    public static void play(){
        try {
            System.out.println(new Date() + " " + Thread.currentThread().getName() + ": 获得服务器进入资格");
            Thread.sleep(2000);
            System.out.println(new Date() + " " + Thread.currentThread().getName() + ": 退出服务器");
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

–案例2

package com.ygq.thread.threadPool;

import java.util.Date;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author :GQ.Yin
 * @date :Created in 2019/7/22 14:14
 * @description:信号量示例
 * @version: $version$
 */
public class SemaPhoreSample2 {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newCachedThreadPool();//创建一个可缓存线程池
        final Semaphore semaphore = new  Semaphore(5); //定义5个信号量, 也就是只允许5个同时运行
        for (int i = 1; i < 20; i++) {
            final int index = i;
            threadPool.execute(new Runnable() {
                public void run() {
//                    if (semaphore.tryAcquire()){ //尝试获取一次信号量, 获取到返回true
//                        play();  //调用play方法
//                        semaphore.release();  //执行完成之后,释放信号量
//                    } else {
//                        System.out.println("对不起, 服务器满员, 请稍候再尝试");
//                    }

                    try {
                        if (semaphore.tryAcquire(5, TimeUnit.SECONDS)){ //尝试获取一次信号量, 并且等待5秒钟, 获取到返回true
                            play();  //调用play方法
                            semaphore.release();  //执行完成之后,释放信号量
                        } else {
                            System.out.println("对不起, 服务器满员, 请稍候再尝试");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            });
        }

        threadPool.shutdown();


    }


    public static void play(){
        try {
            System.out.println(new Date() + " " + Thread.currentThread().getName() + ": 获得服务器进入资格");
            Thread.sleep(2000);
            System.out.println(new Date() + " " + Thread.currentThread().getName() + ": 退出服务器");
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}