zoukankan      html  css  js  c++  java
  • SemaphoreDemo

    package com.fh.interview;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Semaphore;
    
    /**
     * @author
     *
     * Semaphore用来做特殊资源的并发访问控制是相当合适的,如果有业务场景需要进行流量控制,可以优先考虑Semaphore。
     * @create 2018-06-03 下午4:21
     **/
    public class SemaphoreDemo {
    
        private static Semaphore semaphore = new Semaphore(5);
    
        public static void main(String[] args) {
            ExecutorService pool = Executors.newFixedThreadPool(10);
            for (int i=0;i<10;i++){
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println("start");
                            semaphore.acquire();
                            System.out.println("do");
                            Thread.sleep(10000);
                            semaphore.release();
                            System.out.println("release");
                        }catch (Exception e){
    
                        }
                    }
                });
            }
            pool.shutdown();
        }
    }
  • 相关阅读:
    GCC编绎详解
    GUN C/C++ __attribute__ 用法 转
    rust 参考的资料 转
    Eclipse环境安装rust
    GNU Debugger for Windows----GDB
    minGW cygwin gnuwin32
    tdm-gcc
    GNU tools
    The MinGW and mingw-w64 projects.----GCC
    crosstool-NG
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/9129559.html
Copyright © 2011-2022 走看看