创建多线程的四种方式 (线程池 重点)

1.集成Thread

2.实现Rubbable接口

3.实现Callable接口 配合FuturTask

class myThread implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("come in");
        return 1024;
    }
}

public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask(new myThread());
        new Thread(futureTask, "AA").start();
        Integer o = futureTask.get();
        System.out.println(o);
    }
}

4.线程池

  1. 线程池三个常用的方式 (JDK自带的 实际开发我们不用) 因为不能控制资源假设高并发同时创建了很多的线程 服务器直接瘫痪…
//创建一个固定线程的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        //创建一个单个线程的线程池
        ExecutorService executorService2 = Executors.newSingleThreadExecutor();
        //创建一个会自动扩容的线程池
        ExecutorService executorService3 = Executors.newCachedThreadPool();

	try {
            for (int i = 0; i < 10; i++) {
                executorService3.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "*****");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService3.shutdown();
        }
  1. 线程池的七个重要参数
    这里注意: 我们看源码的时候会发现只有五个参数 这里我为什么会说是七个参数呢
    在这里插入图片描述
    点进去看 调用this传了七个参数
    在这里插入图片描述
    在这里插入图片描述
    参数2 最大值为 Integer.MAX_VALUE 2147483647
    解释参数3 4。 比如说这个方法,当大量请求进来了他就会创建好多个线程,而后面一定时间单位没有那么多请求了,他就会关闭达到一定时间没有用过的线程(比如太忙被叫过来加班,后来不忙了提前下班了。)。
    //创建一个会自动扩容的线程池 ExecutorService executorService3 = Executors.newCachedThreadPool();
    参数7. 就是我们最大线程数和我们阻塞队列全部满了,将会启动拒绝策略。

在这里插入图片描述

四种拒绝策略

在这里插入图片描述

手写改造线程池

创建一个核心线程数为2 最大为5 阻塞队列为3 超时1s销毁新增线程的线程池 阻塞策略抛异常

ExecutorService pool = new ThreadPoolExecutor(
                2,
                5,
                1L, 
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(), 
                new ThreadPoolExecutor.AbortPolicy());

怎么合理配置线程池

首先,查看自己电脑的核数System.out.println(Runtime.getRuntime().availableProcessors());
核数+1 =最大线程数

completableFuture 异步编排

这6步骤的执行有依赖关系 我们后边几步需要前面的执行结果 可以用异步编排实现
在这里插入图片描述
在这里插入图片描述

 CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getName());
            int i = 10 / 4;
            System.out.println("运行结果" + i);
            return i;
        }, executorService);

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

 CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getName());
            int i = 10 / 4;
            System.out.println("运行结果" + i);
            return i;
        }, executorService).thenRunAsync(()->{
            System.out.println("任务2启动了 ... 结束");
        },executorService);

任务1和任务2都执行完了 再执行任务3

future01.thenAcceptBothAsync() 这个可以接收前面的结果
future01.thenCombineAsync() 这个可以定义返回值

CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程启动了..." + Thread.currentThread().getName());
            int i = 10 / 4;
            System.out.println("运行结果" + i);
            System.out.println("任务1线程结束了..." + Thread.currentThread().getName());
            return i;
        }, executorService);

        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程启动了..." + Thread.currentThread().getName());
            System.out.println("任务2线程结束了..." + Thread.currentThread().getName());
            return "hello";
        }, executorService);

        future01.thenAcceptBothAsync(future02,(f1,f2)->{
            System.out.println("任务3开始...");
            System.out.println("之前的结果" + f1 + f2);
        },executorService);

A和B只要有一个执行完成就执行C
在这里插入图片描述

多任务组合

在这里插入图片描述

配置并使用线程池

package com.gulimall.product.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class MyThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties threadPoolConfigProperties) {
    //配置类代码
       return new ThreadPoolExecutor(
                threadPoolConfigProperties.getCoreSize(),
                threadPoolConfigProperties.getMaxSize(),
                threadPoolConfigProperties.getKeepActiveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

    }
}

为了方便配置 我们可以自定义配置文件 并且添加下面依赖在配置文件里会有提示

<!--属性的提示工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
package com.gulimall.product.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "gulimall.thread")
@Component
@Data
public class ThreadPoolConfigProperties {
    private Integer coreSize;
    private Integer maxSize;
    private Integer keepActiveTime;
}

@Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
		//异步编排
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            //1 sku基本信息 pms_sku_info
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);

        CompletableFuture<Void> imageFuture = infoFuture.thenAcceptAsync((res) -> {
            //2 sku图片信息
            List<SkuImagesEntity> skuImagesEntities = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(skuImagesEntities);
        }, executor);

        CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync((res) -> {
            //3 获取spu销售属性组合
            List<ItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            //4 获取spu介绍 pms_spu_info_desc
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfoDescEntity);
        }, executor);

        CompletableFuture<Void> groupAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            //5 获取spu规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);

        //等待所有的异步任务都完成才返回我们的结果
        CompletableFuture.allOf(imageFuture,attrFuture,descFuture,groupAttrFuture).get();
        // 6.查询当前sku是否参与秒杀优惠
        return skuItemVo;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Linux多线程服务端编程:使用muduo C++网络库》主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread。 目 录 第1部分C++ 多线程系统编程 第1章线程安全的对象生命期管理3 1.1当析构函数遇到多线程. . . . . . . . . . . . . . . . .. . . . . . . . . . . 3 1.1.1线程安全的定义. . . . . . . . . . . . . . . . .. . . . . . . . . . . 4 1.1.2MutexLock 与MutexLockGuard. . . . . . . . . . . . . . . . . . . . 4 1.1.3一个线程安全的Counter 示例.. . . . . . . . . . . . . . . . . . . 4 1.2对象的创建很简单. . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 5 1.3销毁太难. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 7 1.3.1mutex 不是办法. . . . . . . . . . . . . . . . . . . .. . . . . . . . 7 1.3.2作为数据成员的mutex 不能保护析构.. . . . . . . . . . . . . . 8 1.4线程安全的Observer 有多难.. . . . . . . . . . . . . . . . . . . . . . . . 8 1.5原始指针有何不妥. . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 11 1.6神器shared_ptr/weak_ptr . . . . . . . . . .. . . . . . . . . . . . . . . . 13 1.7插曲:系统地避免各种指针错误. . . . . . . . . . . . . . . . .. . . . . . 14 1.8应用到Observer 上.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.9再论shared_ptr 的线程安全.. . . . . . . . . . . . . . . . . . . . . . . . 17 1.10shared_ptr 技术与陷阱. . . .. . . . . . . . . . . . . . . . . . . . . . . . 19 1.11对象池. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 21 1.11.1enable_shared_from_this . . . . . . . . . . . . . . . . . . . . . . 23 1.11.2弱回调. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 24 1.12替代方案. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 26 1.13心得与小结. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . 26 1.14Observer 之谬. . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 第2章线程同步精要 2.1互斥器(mutex). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.1.1只使用非递归的mutex . . . . . . . . . . . . . .. . . . . . . . . . 33 2.1.2死锁. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 35 2.2条件变量(condition variable). . . . . . . . . .

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值