四种线程池

对四种线程池进行学习

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TestThreadPool1 {
    public static void main(String[] args) {
        //testScheduledThreadPool();
        //testCachedThreadPool();
        //testSingleThreadPool();
        testFixedThreadPool();
    }

    /*
     //固定线程池,
     接口scheduleAtFixedRate原型定义及参数说明:
     public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
				long initialDelay,long period,TimeUnit unit);
	command:执行线程
    initialDelay:初始化延时
    period:两次开始执行最小间隔时间
    unit:计时单位

    scheduleAtFixedRate:是以固定的频率去执行任务,周期是指每次执行任务成功之间的间隔。
    固定的频率来执行某项计划,它不受计划执行时间的影响

    schedultWithFixedDelay:是以固定的延时去执行任务,延时是指上一次执行成功之后
    和下一次开始执行的之前的时间。
    即无论某个任务执行多长时间,等执行完了,我再延迟指定的时间。
    也就是第二个方法,它受计划执行时间的影响


    */
    //固定线程池,
    static void testScheduledThreadPool(){
        ScheduledExecutorService scheduledExecutorService=
                Executors.newScheduledThreadPool(5);
        scheduledExecutorService.scheduleAtFixedRate(
               new Runnable(){
                   @Override
                   public void run() {
                       System.out.println(" 执行周期任务");
                   }
               },1,2,TimeUnit.SECONDS);
    }



    /*
    缓存线程池,
    和缓存有关的线程池,每次有任务提交到线程池的时候,如果池中没有空闲的线程,
    线程池就会为这个任务创建一个线程,如果有空闲的线程,就会使用已有的空闲线程执行任务。
    如果一个线程60秒之内没有被使用过,这个线程就会被销毁,这样就节省了很多资源。
    */
    static  void testCachedThreadPool(){
        ExecutorService executorService=Executors.newCachedThreadPool();
        //创建一个缓存线程池,有多少个线程,缓存线程池就会创建多少个线程。
        for (int i=0;i<20;i++){
            int finalI = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("execute run method,i:"+ finalI);
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

    /*
    只有一个线程的线程池, 若有多余的任务提交到线程池中,则会被暂存到阻塞队列,
    待空闲时再去执行。按照先入先出的顺序执行任务。
    A thread pool with only one thread is temporarily stored in a blocking queue
    if additional tasks are submitted to the thread pool.
    Do it when you are free. Perform tasks in FIFO order.
    */
    static void testSingleThreadPool(){
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i=0;i<3;i++){
            int finalI = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("execute run method,i:"+ finalI);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

/*
固定大小的线程池,可以指定线程池的大小
Fixed size thread pool, which can specify the size of thread pool
该线程池中的线程数量始终不变,当有新任务提交时,线程池中有空闲线程则会立即执行,
如果没有,则会暂存到阻塞队列。对于固定大小的线程池,不存在线程数量的变化
The number of threads in the thread pool remains unchanged. When new tasks are submitted,
idle threads in the thread pool execute immediately, and if not,
they are temporarily stored in the blocking queue. For fixed-size thread pools,
there is no change in the number of threads
在线程池空闲时,即线程池中没有可运行任务时,它也不会释放工作线程,还会占用一定的系统资源。
When the thread pool is idle, that is, when there are no runnable tasks in the thread pool,
 it will not release the working threads, but also occupy certain system resources
*/
    static  void testFixedThreadPool(){
        ExecutorService executorService =Executors.newFixedThreadPool(5);
        for (int i=0; i<10; i++){

            int finalI = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("execute run method ,i:"+ finalI);
                    try {
                        Thread.sleep(8000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值