java 线程池 ---- newSingleThreadExecutor()

 

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

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

    }
}

 

    原文作者:huanggy
    原文地址: https://www.cnblogs.com/huanggy/p/9865629.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞