11-JUC-线程池的使用

一)线程的体系结构

跟接口
Executor:负责线程使用域调度的根接口
Executor:子接口
ThreadPoolExecutor:实现类
ScheduledExecutorService:子接口负责线程的调度
ScheduleThreadPoolExecutor:继承ThreadPoolExecutor和ScheduledExecutorService 实现了线程的调用和线程实现类。

线程池的好处:
解决线程创建和销毁的资源浪费。

二)线程池的创建

工具类:Excutors
ExecutorService new FixedThreadPool():创建固定大小的线程池。
ExecutorService new CachedThreadPool():创建缓存线程池,线程池的数量不确定,可以根据需求自动的更改数量。
ExecutorService new SingleThreadExcutor():创建单个线程池

线程吃API介绍:
submit:每次提交都是创建一个新的线程。
shoutdown:关闭线程池。优雅的关闭线程池。

/**
 * @project_name:juc
 * @date:2019/10/10:18:15
 * @author:shinelon
 * @Describe:使用线程池进行线程的创建
 */
public class ThreadPool
{
    public static void main(String[] args)
    {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        TestThreadPool testThreadPool = new TestThreadPool();
        pool.submit(testThreadPool);
        pool.shutdown();
    }
}

class TestThreadPool implements Runnable
{

    @Override
    public void run()
    {
        for (int i = 0; i < 100; i++)
        {
            System.out.println(Thread.currentThread().getName() + "===>" + i);
        }
    }
}

在这里插入图片描述

使用Lambda表达式进行

/**
 * @project_name:juc
 * @date:2019/10/10:18:27
 * @author:shinelon
 * @Describe:线程池使用Callable接口
 */
public class TreadPool2
{
    public static void main(String[] args) throws Exception
    {
        ExecutorService pool = Executors.newFixedThreadPool(5);

        Future<?> submit = pool.submit(() ->
                                               {
                                           int sum = 0;
                                           for (int i = 0; i <= 100; i++)
                                           {
                                               sum += i;
                                           }
                                           return sum;
                                       });
        System.out.println(submit.get());
        pool.shutdown();
    }
}

使用线程池调度进行线程的控制
ScheduleThreadPoolExecutor实现线程的控制:

/**
 * @project_name:juc
 * @date:2019/10/10:18:59
 * @author:shinelon
 * @Describe:实现线程池控制信息
 */
public class TestScheduleThreadPool
{
    public static void main(String[] args) throws ExecutionException, InterruptedException
    {
        //获取带调度的线程池
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
        ScheduledFuture<?> schedule = pool.schedule(() ->
                                                    {
                                                        System.out.println(Thread.currentThread().getName());
                                                        return 5;
                                                        //时间的延迟和秒数
                                                    }, 3, TimeUnit.SECONDS);
        System.out.println(schedule.get());
        pool.shutdown();
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值