Executors简单使用

  • Post author:
  • Post category:其他
public class ExecutorTest {
    private static Integer num = 1;
    private static boolean exeFlag = true;
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);//初始化10个 超过10个堵塞
        while (exeFlag){
            if(num <=100){
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("第"+ num +"个");
                        num++;
                    }
                });
            }else{
                if(((ThreadPoolExecutor)executorService).getActiveCount()==0){
                    executorService.shutdown();//关闭executors
                    exeFlag=false;
                    System.out.println("任务结束!");
                }
            }
            Thread.sleep(10);
        }
    }
}


版权声明:本文为KeepStrong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。