java 线程池 ---- newFixedThreadPool()

 

class MyThread implements Runnable{
    private int index;

    public MyThread(int index){
        this.index = index;
    }

    @Override
    public void run() {
        System.out.println("处理任务:" + index);
    }
}
public class Test1 {
    public static void main(String[] args){

        // 创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 15; i++){
            MyThread myThread = new MyThread(i);
            // 任务丢进线程池
            executor.execute(myThread);
        }
        // 关闭服务
        executor.shutdown();

    }
}

 

posted @ 2018-10-28 14:56  huanggy  阅读(678)  评论(0编辑  收藏  举报