码迷,mamicode.com
首页 > 编程语言 > 详细

java 线程池 ---- newCachedThreadPool()

时间:2018-10-28 16:13:42      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:关闭   col   dex   service   需要   system   run   设置   int   

 

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.newCachedThreadPool();

        for (int i = 0; i < 15; i++){
            MyThread myThread = new MyThread(i);
            // 任务丢进线程池
            executor.execute(myThread);
        }
        // 不需要关闭服务, 如果线程空闲 60 秒, 将会自动销毁; 而核心线程数设置为 0, 所以 60s 后会销毁所有线程

    }
}

 

java 线程池 ---- newCachedThreadPool()

标签:关闭   col   dex   service   需要   system   run   设置   int   

原文地址:https://www.cnblogs.com/huanggy/p/9865639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!