动态修改线程池数,并发执行数

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

import static java.lang.Thread.sleep;

public class TestMain {

    public static void main(String args[]) {


        /*
        RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
        */

        /*ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3,
                0L,  TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>());*/
  /* ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue(5));*/
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(3);

        for(int i=0;i<20;i++){
            final int num =i;

            executor.execute(new Runnable(){
                @Override
                public void run() {
                    if(num==6){
                        executor.setCorePoolSize(2);
                        executor.setMaximumPoolSize(2);
                    }
                   *//* System.out.println("线程池中线程数目:"+executor.getPoolSize()+",队列中等待执行的任务数目:"+
                            executor.getQueue().size()+",已执行玩别的任务数目:"+executor.getCompletedTaskCount()+"----"+workingQueue.size());*//*
                    System.out.println(num);
                    try {
                        sleep(1000);
                    }catch (Exception e){

                    }
                }
            });

        }
        executor.shutdown();

    }
}

你可能感兴趣的:(java,基础)