java进阶之多线程二线程池

本文详细介绍了Java中的四种线程池:单线程线程池、带缓存的线程池、指定线程数量的线程池以及延迟执行线程的线程池。通过示例代码展示了它们各自的特点和使用场景,包括任务排队、线程复用、线程限制和定时任务的执行。
摘要由CSDN通过智能技术生成

单线程线程池

class threadPool{
    public static void main(String[] args) {
        long f = System.currentTimeMillis();
        //-------------------------------------------
        //创建线程池只有一个线程
        ExecutorService es = Executors.newSingleThreadExecutor();
        es.execute(new myR());
        es.execute(new myR());
        es.execute(new myR());
        es.execute(new myR());
        es.shutdown();
        //-------------------------------------------
        long h = System.currentTimeMillis();
        System.out.println(h-f);
    }
}
class myR implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"执行了");
    }
}

执行结果:
在这里插入图片描述
线程池只有一个线程 所有多个任务是排队进入

多线程线程池

带缓存的线程池

class threadPool{
    public static void main(String[] args) throws InterruptedException {
        long f = System.currentTimeMillis();
        //-------------------------------------------
        //创建线程池 带缓存的线程池
        ExecutorService es = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            Thread.sleep(10*i);
            es.execute(new myR());

        }
        es.shutdown();
        //-------------------------------------------
        long h = System.currentTimeMillis();
        System.out.println(h-f);
    }

}
class myR implements Runnable{

        @Override
        public void run() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"执行了");
        }
}

执行结果
在这里插入图片描述
当有任务需要线程 线程池提供新建一个线程 如果有闲置的线程将会使用闲置的线程 最多新建 Integer.Max 个线程

指定线程个数的线程池


class threadPool{
    public static void main(String[] args) throws InterruptedException {
        long f = System.currentTimeMillis();
        //-------------------------------------------
        //创建线程池 指定个数
        ExecutorService es = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            es.execute(new myR());

        }
        es.shutdown();

        //-------------------------------------------
        long h = System.currentTimeMillis();
        System.out.println(h-f);
    }

}



class myR implements Runnable{

        @Override
        public void run() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"执行了");
        }

}

执行结果
在这里插入图片描述
当线程都在使用时 其他任务需要等待空出新线程

延迟执行线程的 线程池

class threadPool{
    public static void main(String[] args) throws InterruptedException {
        long f = System.currentTimeMillis();
        //-------------------------------------------
        //创建线程池可以更改数字 个数
        //如果只需要一个可以  使用Executors.newSingleThreadScheduledExecutor();
        ScheduledExecutorService es = Executors.newScheduledThreadPool(2);
        for (int i = 0; i < 10; i++) {
          // 参数: 线程  延迟时间 延迟单位
          es.schedule(new myR(),1000, TimeUnit.MILLISECONDS);
          //参数 : 线程  延迟时间 周期  延迟单位
     es.scheduleAtFixedRate(new myR(),1000,1000, TimeUnit.MILLISECONDS);
        }
        //-------------------------------------------
        long h = System.currentTimeMillis();
        System.out.println(h-f);
    }

}
class myR implements Runnable{

        @Override
        public void run() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"执行了");
        }

}

执行结果
在这里插入图片描述
间隔 一秒 之后执行 任务

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值