java之线程池

java线程池

线程池的作用:

public class ThreadPoolDemo {
    public static class MyTask implements Runnable{

        @Override
        public void run() {
            System.out.println(System.currentTimeMillis() + ":Thread ID:" + Thread.currentThread().getId());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args){
            MyTask task = new MyTask();
            ExecutorService es = Executors.newFixedThreadPool(5);
            for(int i = 0;i < 10 ;i++){
                es.submit(task);
            }
        }
    }
}

 

使用线程池去处理任务,一个线程池有5个线程,5个线程可以直接一次处理掉5个任务,

posted on 2018-04-20 17:16  张小泽的小号  阅读(92)  评论(0编辑  收藏  举报

导航