线程池的理解及使用

1.初识线程池:
根据系统自身的环境情况,有效的限制执行线程的数量,使得运行效果达到最佳。线程主要是通过控制执行的线程的数量,超出数量的线程排队等候,等待有任务执行完毕,再从队列最前面取出任务执行。
2.线程池作用:
减少创建和销毁线程的次数,每个工作线程可以多次使用
可根据系统情况调整执行的线程数量,防止消耗过多内存
3.使用
ExecutorService:线程池接口
ExecutorService pool = Executors.常见线程
eg:ExecutorService pool = Executors.newSingleThreadExecutor();

4.常见线程池
①newSingleThreadExecutor
单个线程的线程池,即线程池中每次只有一个线程工作,单线程串行执行任务
②newFixedThreadExecutor(n)
固定数量的线程池,没提交一个任务就是一个线程,直到达到线程池的最大数量,然后后面进入等待队列直到前面的任务完成才继续执行
③newCacheThreadExecutor(推荐使用)
可缓存线程池,当线程池大小超过了处理任务所需的线程,那么就会回收部分空闲(一般是60秒无执行)的线程,当有任务来时,又智能的添加新线程来执行。
④newScheduleThreadExecutor
大小无限制的线程池,支持定时和周期性的执行线程

5.实例
publicclass MyThread  extends Thread {
     @Override
     publicvoid run() {
        System. out.println(Thread. currentThread().getName() +  "执行中。。。");
    }
}

①newSingleThreadExecutor

publicclass TestSingleThreadExecutor {
     publicstaticvoid main(String[] args) {
         //创建一个可重用固定线程数的线程池
        ExecutorService pool = Executors.  newSingleThreadExecutor();
         //创建实现了Runnable接口对象
        Thread tt1 =  new MyThread();
        Thread tt2 =  new MyThread();
        Thread tt3 =  new MyThread();
        Thread tt4 =  new MyThread();
        Thread tt5 =  new MyThread();
         //将线程放入池中并执行
        pool.execute(tt1);
        pool.execute(tt2);
        pool.execute(tt3);
        pool.execute(tt4);
        pool.execute(tt5);
         //关闭
        pool.shutdown();
    }
}
result:
pool-1-thread-1执行中。。。
pool-1-thread-1 执行中。。。
pool-1-thread-1 执行中。。。
pool-1-thread-1 执行中。。。
pool-1-thread-1 执行中。。。

②newFixedThreadExecutor(n)

publicclass TestFixedThreadPool {
     publicstaticvoid main(String[] args) {
         //创建一个可重用固定线程数的线程池
        ExecutorService pool = Executors. newFixedThreadPool(2);
         //创建实现了Runnable接口对象
        Thread t1 =  new MyThread();
        Thread t2 =  new MyThread();
        Thread t3 =  new MyThread();
        Thread t4 =  new MyThread();
        Thread t5 =  new MyThread();
         //将线程放入池中进行执行
        pool.execute(t1);
        pool.execute(t2);
        pool.execute(t3);
        pool.execute(t4);
        pool.execute(t5);
         //关闭线程池
        pool.shutdown();
    }
}
result:
pool-1-thread-1 执行中。。。
pool-1-thread-2 执行中。。。
pool-1-thread-1 执行中。。。
pool-1-thread-2 执行中。。。
pool-1-thread-1 执行中。。。

③newCacheThreadExecutor

publicclass TestCachedThreadPool {
     publicstaticvoid main(String[] args) {
         //创建一个可重用固定线程数的线程池
        ExecutorService pool = Executors. newCachedThreadPool();
         //创建实现了Runnable接口对象
        Thread t1 =  new MyThread();
        Thread t2 =  new MyThread();
        Thread t3 =  new MyThread();
        Thread t4 =  new MyThread();
        Thread t5 =  new MyThread();
         //将线程放入池中进行执行
        pool.execute(t1);
        pool.execute(t2);
        pool.execute(t3);
        pool.execute(t4);
        pool.execute(t5);
         //关闭线程池
        pool.shutdown();
    }
}
result:
pool-1-thread-1 执行中。。。
pool-1-thread-2 执行中。。。
pool-1-thread-4 执行中。。。
pool-1-thread-3 执行中。。。
pool-1-thread-5 执行中。。。

猜你喜欢

转载自blog.csdn.net/wypersist/article/details/80275752