1. 程式人生 > >動態修改執行緒池數,併發執行數

動態修改執行緒池數,併發執行數

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<Runnable>(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();

    } }