Java中「Future」接口详解

news2024/10/3 3:33:11

主打一手结果导向;

一、背景

在系统中,异步执行任务,是很常见的功能逻辑,但是在不同的场景中,又存在很多细节差异;

有的任务只强调「执行过程」,并不需要追溯任务自身的「执行结果」,这里并不是指对系统和业务产生的效果,比如定时任务、消息队列等场景;

但是有些任务即强调「执行过程」,又需要追溯任务自身的「执行结果」,在流程中依赖某个异步结果,判断流程是否中断,比如「并行」处理;

串行处理】整个流程按照逻辑逐步推进,如果出现异常会导致流程中断;

并行处理】主流程按照逻辑逐步推进,其他「异步」交互的流程执行完毕后,将结果返回到主流程,如果「异步」流程异常,会影响部分结果;

此前在《「订单」业务》的内容中,聊过关于「串行」和「并行」的应用对比,即在订单详情的加载过程中,通过「并行」的方式读取:商品、商户、订单、用户等信息,提升接口的响应时间;

二、Future接口

1、入门案例

异步是对流程的解耦,但是有的流程中又依赖异步执行的最终结果,此时就可以使用「Future」接口来达到该目的,先来看一个简单的入门案例;

public class ServerTask implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        Thread.sleep(2000);
        return 3;
    }
}
public class FutureBase01 {
    public static void main(String[] args) throws Exception {
        TimeInterval timer = DateUtil.timer();
        // 线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        // 批量任务
        List<ServerTask> serverTasks = new ArrayList<>() ;
        for (int i=0;i<3;i++){
            serverTasks.add(new ServerTask());
        }
        List<Future<Integer>> taskResList = executor.invokeAll(serverTasks) ;
        // 结果输出
        for (Future<Integer> intFuture:taskResList){
            System.out.println(intFuture.get());
        }
        // 耗时统计
        System.out.println("timer...interval = "+timer.interval());
    }
}

这里模拟一个场景,以线程池批量执行异步任务,在任务内线程休眠2秒,以并行的方式最终获取全部结果,只耗时2秒多一点,如果串行的话耗时肯定超过6秒;

2、Future接口

Future表示异步计算的结果,提供了用于检查计算是否完成、等待计算完成、以及检索计算结果的方法。

核心方法

  • get():等待任务完成,获取执行结果,如果任务取消会抛出异常;
  • get(long timeout, TimeUnit unit):指定等待任务完成的时间,等待超时会抛出异常;
  • isDone():判断任务是否完成;
  • isCancelled():判断任务是否被取消;
  • cancel(boolean mayInterruptIfRunning):尝试取消此任务的执行,如果任务已经完成、已经取消或由于其他原因无法取消,则此尝试将失败;

基础用法

public class FutureBase02 {
    public static void main(String[] args) throws Exception {
        // 线程池执行任务
        ExecutorService executor = Executors.newFixedThreadPool(3);
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(3000);
                return "task...OK";
            }
        }) ;
        executor.execute(futureTask);
        // 任务信息获取
        System.out.println("是否完成:"+futureTask.isDone());
        System.out.println("是否取消:"+futureTask.isCancelled());
        System.out.println("获取结果:"+futureTask.get());
        System.out.println("尝试取消:"+futureTask.cancel(Boolean.TRUE));
    }
}

FutureTask

Future接口的基本实现类,提供了计算的启动和取消、查询计算是否完成以及检索计算结果的方法;

在「FutureTask」类中,可以看到线程异步执行任务时,其中的核心状态转换,以及最终结果写出的方式;

虽然「Future」从设计上,实现了异步计算的结果获取,但是通过上面的案例也可以发现,流程的主线程在执行get()方法时会阻塞,直到最终获取结果,显然对于程序来说并不友好;

JDK1.8提供「CompletableFuture」类,对「Future」进行优化和扩展;

三、CompletableFuture类

1、基础说明

「CompletableFuture」类提供函数编程的能力,可以通过回调的方式处理计算结果,并且支持组合操作,提供很多方法来实现异步编排,降低异步编程的复杂度;

「CompletableFuture」实现「Future」和「CompletionStage」两个接口;

  • Future:表示异步计算的结果;
  • CompletionStage:表示异步计算的一个步骤,当一个阶段计算完成时,可能会触发其他阶段,即步骤可能由其他CompletionStage触发;

入门案例

public class CompletableBase01 {
    public static void main(String[] args) throws Exception {
        // 线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        // 任务执行
        CompletableFuture<String> cft = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Res...OK";
        }, executor);
        // 结果输出
        System.out.println(cft.get());
    }
}

2、核心方法

2.1 实例方法

public class Completable01 {
    public static void main(String[] args) throws Exception {
        // 线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);

        // 1、创建未完成的CompletableFuture,通过complete()方法完成
        CompletableFuture<Integer> cft01 = new CompletableFuture<>() ;
        cft01.complete(99) ;

        // 2、创建已经完成CompletableFuture,并且给定结果
        CompletableFuture<String> cft02 = CompletableFuture.completedFuture("given...value");

        // 3、有返回值,默认ForkJoinPool线程池
        CompletableFuture<String> cft03 = CompletableFuture.supplyAsync(() -> {return "OK-3";});

        // 4、有返回值,采用Executor自定义线程池
        CompletableFuture<String> cft04 = CompletableFuture.supplyAsync(() -> {return "OK-4";},executor);

        // 5、无返回值,默认ForkJoinPool线程池
        CompletableFuture<Void> cft05 = CompletableFuture.runAsync(() -> {});

        // 6、无返回值,采用Executor自定义线程池
        CompletableFuture<Void> cft06 = CompletableFuture.runAsync(()-> {}, executor);
    }
}

2.2 计算方法

public class Completable02 {
    public static void main(String[] args) throws Exception {
        // 线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> cft01 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "OK";
        },executor);

        // 1、计算完成后,执行后续处理
        // cft01.whenComplete((res, ex) -> System.out.println("Result:"+res+";Exe:"+ex));

        // 2、触发计算,如果没有完成,则get设定的值,如果已完成,则get任务返回值
        // boolean completeFlag = cft01.complete("given...value");
        // if (completeFlag){
        //     System.out.println(cft01.get());
        // } else {
        //     System.out.println(cft01.get());
        // }

        // 3、开启新CompletionStage,重新获取线程执行任务
        cft01.whenCompleteAsync((res, ex) -> System.out.println("Result:"+res+";Exe:"+ex),executor);
    }
}

2.3 结果获取方法

public class Completable03 {
    public static void main(String[] args) throws Exception {
        // 线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> cft01 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Res...OK";
        },executor);
        // 1、阻塞直到获取结果
        // System.out.println(cft01.get());

        // 2、设定超时的阻塞获取结果
        // System.out.println(cft01.get(4, TimeUnit.SECONDS));

        // 3、非阻塞获取结果,如果任务已经完成,则返回结果,如果任务未完成,返回给定的值
        // System.out.println(cft01.getNow("given...value"));

        // 4、get获取抛检查异常,join获取非检查异常
        System.out.println(cft01.join());
    }
}

2.4 任务编排方法

public class Completable04 {
    public static void main(String[] args) throws Exception {
        // 线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> cft01 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("OK-1");
            return "OK";
        },executor);

        // 1、cft01任务执行完成后,执行之后的任务,此处不关注cft01的结果
        // cft01.thenRun(() -> System.out.println("task...run")) ;

        // 2、cft01任务执行完成后,执行之后的任务,可以获取cft01的结果
        // cft01.thenAccept((res) -> {
        //     System.out.println("cft01:"+res);
        //     System.out.println("task...run");
        // });

        // 3、cft01任务执行完成后,执行之后的任务,获取cft01的结果,并且具有返回值
        // CompletableFuture<Integer> cft02 = cft01.thenApply((res) -> {
        //     System.out.println("cft01:"+res);
        //     return 99 ;
        // });
        // System.out.println(cft02.get());

        // 4、顺序执行cft01、cft02
        // CompletableFuture<String> cft02 = cft01.thenCompose((res) ->  CompletableFuture.supplyAsync(() -> {
        //     System.out.println("cft01:"+res);
        //     return "OK-2";
        // }));
        // cft02.whenComplete((res,ex) -> System.out.println("Result:"+res+";Exe:"+ex));

        // 5、对比任务的执行效率,由于cft02先完成,所以取cft02的结果
        // CompletableFuture<String> cft02 = cft01.applyToEither(CompletableFuture.supplyAsync(() -> {
        //     System.out.println("run...cft02");
        //     try {
        //         Thread.sleep(3000);
        //     } catch (InterruptedException e) {
        //         e.printStackTrace();
        //     }
        //     return "OK-2";
        // }),(res) -> {
        //     System.out.println("either...result:" + res);
        //     return res;
        // });
        // System.out.println("finally...result:" + cft02.get());

        // 6、两组任务执行完成后,对结果进行合并
        // CompletableFuture<String> cft02 = CompletableFuture.supplyAsync(() -> "OK-2") ;
        // String finallyRes = cft01.thenCombine(cft02,(res1,res2) -> {
        //     System.out.println("res1:"+res1+";res2:"+res2);
        //     return res1+";"+res2 ;
        // }).get();
        // System.out.println(finallyRes);


        CompletableFuture<String> cft02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("OK-2");
            return  "OK-2";
        }) ;
        CompletableFuture<String> cft03 = CompletableFuture.supplyAsync(() -> {
            System.out.println("OK-3");
            return "OK-3";
        }) ;
        // 7、等待批量任务执行完返回
        // CompletableFuture.allOf(cft01,cft02,cft03).get();

        // 8、任意一个任务执行完即返回
        System.out.println("Sign:"+CompletableFuture.anyOf(cft01,cft02,cft03).get());
    }
}

2.5 异常处理方法

public class Completable05 {
    public static void main(String[] args) throws Exception {
        // 线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> cft01 = CompletableFuture.supplyAsync(() -> {
            if (1 > 0){
                throw new RuntimeException("task...exception");
            }
            return "OK";
        },executor);

        // 1、捕获cft01的异常信息,并提供返回值
        String finallyRes = cft01.thenApply((res) -> {
            System.out.println("cft01-res:" + res);
            return res;
        }).exceptionally((ex) -> {
            System.out.println("cft01-exe:" + ex.getMessage());
            return "error" ;
        }).get();
        System.out.println("finallyRes="+finallyRes);


        CompletableFuture<String> cft02 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "OK-2";
        },executor);
        // 2、如果cft02未完成,则get时抛出指定异常信息
        boolean exeFlag = cft02.completeExceptionally(new RuntimeException("given...exception"));
        if (exeFlag){
            System.out.println(cft02.get());
        } else {
            System.out.println(cft02.get());
        }
    }
}

3、线程池问题

  • 在实践中,通常不使用ForkJoinPool#commonPool()公共线程池,会出现线程竞争问题,从而形成系统瓶颈;
  • 在任务编排中,如果出现依赖情况或者父子任务,尽量使用多个线程池,从而避免任务请求同一个线程池,规避死锁情况发生;

四、CompletableFuture原理

1、核心结构

在分析「CompletableFuture」其原理之前,首先看一下涉及的核心结构;

CompletableFuture

在该类中有两个关键的字段:「result」存储当前CF的结果,「stack」代表栈顶元素,即当前CF计算完成后会触发的依赖动作;从上面案例中可知,依赖动作可以没有或者有多个;

Completion

依赖动作的封装类;

UniCompletion

继承Completion类,一元依赖的基础类,「executor」指线程池,「dep」指依赖的计算,「src」指源动作;

BiCompletion

继承UniCompletion类,二元或者多元依赖的基础类,「snd」指第二个源动作;

2、零依赖

顾名思义,即各个CF之间不产生依赖关系;

public class DepZero {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> cft1 = CompletableFuture.supplyAsync(()-> "OK-1",executor);
        CompletableFuture<String> cft2 = CompletableFuture.supplyAsync(()-> "OK-2",executor);
        System.out.println(cft1.get()+";"+cft2.get());
    }
}

3、一元依赖

即CF之间的单个依赖关系;这里使用「thenApply」方法演示,为了看到效果,使「cft1」长时间休眠,断点查看「stack」结构;

public class DepOne {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> cft1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "OK-1";
        },executor);

        CompletableFuture<String> cft2 = cft1.thenApply(res -> {
            System.out.println("cft01-res"+res);
            return "OK-2" ;
        });
        System.out.println("cft02-res"+cft2.get());
    }
}

断点截图

原理分析

观察者Completion注册到「cft1」,注册时会检查计算是否完成,未完成则观察者入栈,当「cft1」计算完成会弹栈;已完成则直接触发观察者;

可以调整断点代码,让「cft1」先处于完成状态,再查看其运行时结构,从而分析完整的逻辑;

4、二元依赖

即一个CF同时依赖两个CF;这里使用「thenCombine」方法演示;为了看到效果,使「cft1、cft2」长时间休眠,断点查看「stack」结构;

public class DepTwo {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> cft1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "OK-1";
        },executor);
        CompletableFuture<String> cft2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "OK-2";
        },executor);

        // cft3 依赖 cft1和cft2 的计算结果
        CompletableFuture<String> cft3 = cft1.thenCombine(cft2,(res1,res2) -> {
            System.out.println("cft01-res:"+res1);
            System.out.println("cft02-res:"+res2);
            return "OK-3" ;
        });
        System.out.println("cft03-res:"+cft3.get());
    }
}

断点截图

原理分析

在「cft1」和「cft2」未完成的状态下,尝试将BiApply压入「cft1」和「cft2」两个栈中,任意CF完成时,会尝试触发观察者,观察者检查「cft1」和「cft2」是否都完成,如果完成则执行;

5、多元依赖

即一个CF同时依赖多个CF;这里使用「allOf」方法演示;为了看到效果,使「cft1、cft2、cft3」长时间休眠,断点查看「stack」结构;

public class DepMore {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        CompletableFuture<String> cft1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "OK-1";
        },executor);
        CompletableFuture<String> cft2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "OK-2";
        },executor);

        CompletableFuture<String> cft3 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "OK-3";
        },executor);

        // cft4 依赖 cft1和cft2和cft3 的计算结果
        CompletableFuture<Void> cft4 = CompletableFuture.allOf(cft1,cft2,cft3);
        CompletableFuture<String> finallyRes = cft4.thenApply(tm -> {
            System.out.println("cft01-res:"+cft1.join());
            System.out.println("cft02-res:"+cft2.join());
            System.out.println("cft03-res:"+cft3.join());
            return "OK-4";
        });
        System.out.println("finally-res:"+finallyRes.get());
    }
}

断点截图

原理分析

多元依赖的回调方法除了「allOf」还有「anyOf」,其实现原理都是将依赖的多个CF补全为平衡二叉树,从断点图可知会按照树的层级处理,核心结构参考二元依赖即可;

五、参考源码

编程文档:
https://gitee.com/cicadasmile/butte-java-note

应用仓库:
https://gitee.com/cicadasmile/butte-flyer-parent

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/421970.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

你可能并不需要useEffect

背景 相信大家在写react时都有这样的经历&#xff1a;在项目中使用了大量的useEffect&#xff0c;以至于让我们的代码变得混乱和难以维护。 难道说useEffect这个hook不好吗&#xff1f;并不是这样的&#xff0c;只是我们一直在滥用而已。 在这篇文章中&#xff0c;我将展示怎…

【Spring源码】讲讲Bean的生命周期

1、前言 面试官&#xff1a;“看过Spring源码吧&#xff0c;简单说说Spring中Bean的生命周期” 大神仙&#xff1a;“基本生命周期会经历实例化 -> 属性赋值 -> 初始化 -> 销毁”。 面试官&#xff1a;“......” 2、Bean的生命周期 如果是普通Bean的生命周期&am…

【故障诊断】基于 KPCA 进行降维、故障检测和故障诊断研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

【Flowable】Flowable流程设计器

Flowable流程设计器有两种实现方式 Eclipse DesignerFlowable UI应用 1.Eclipse Designer Flowable提供了名为Flowable Eclipse Designer的Eclipse插件&#xff0c;可以用于图形化地建模、测试与部署BPMN 2.0流程。 (1).下载安装Eclipse 去Eclipse官网下载即可&#xff1a…

【数据结构:复杂度】时间复杂度

本节重点内容&#xff1a; 算法的复杂度时间复杂度的概念大O的渐进表示法常见时间复杂度计算举例⚡算法的复杂度 算法在编写成可执行程序后&#xff0c;运行时需要耗费时间资源和空间(内存)资源 。因此衡量一个算法的好坏&#xff0c;一般是从时间和空间两个维度来衡量的&…

光伏发电系统模拟及其发电预测开源python工具pvlib

1. 太阳辐照量模拟 pysolar是一个用于计算太阳位置和辐照量的Python库。它是基于python语言编写的&#xff0c;可以方便地在各种python项目中使用。pysolar主要用于计算太阳的位置、太阳高度角、太阳方位角、日出和日落时间等信息。这些信息可以用于太阳能电池板和太阳能集热器…

Spark SQL实战(04)-API编程之DataFrame

1 SparkSession Spark Core: SparkContext Spark SQL: 难道就没有SparkContext&#xff1f; 2.x之后统一的 package com.javaedge.bigdata.chapter04import org.apache.spark.sql.{DataFrame, SparkSession}object SparkSessionApp {def main(args: Array[String]): Unit …

ChatGPT的发展对客户支持能提供什么帮助?

多数组织认为客户服务是一种开销&#xff0c;实际上还可以将客户服务看成是一种机会。它可以让你在销售后继续推动客户的价值。成功的企业深知&#xff0c;客户服务不仅可以留住客户&#xff0c;还可以增加企业收入。客户服务是被低估的手段&#xff0c;它可以通过推荐、见证和…

linux安装Detectron2

参考官方文档&#xff1a;https://detectron2.readthedocs.io/en/latest/tutorials/install.html 1.使用image拉取docker image链接&#xff1a;https://hub.docker.com/r/pytorch/pytorch/tags?page1&name1.8.1-cuda11.1-cudnn8-devel 左上角红框这里搜索1.8.1-cuda1…

Scala - 时间工具类 LocalDateTime 常用方法整理

目录 一.引言 二.LocalDateTime 获取与格式化 1.获取当前时间 LocalDateTime 2.根据时间戳获取 LocalDateTime 3.指定时间获取 LocalDataTime 4.LocalDataTime 格式化 三.LocalDateTime 读取时间细节 1.获取年-Year 2.获取月-Month 3.获取日-Day 4.获取时-Hour 5.获…

Vue3+vite2 博客前端开发

Vue3vite2 博客前端开发 文章目录Vue3vite2 博客前端开发前言页面展示代码设计卡片设计背景&#xff08;Particles.js粒子效果&#xff09;右侧个人信息与公告内容页友链总结前言 大家是否也想拥有一个属于自己的博客&#xff1f;但是如何去开发博客&#xff0c;怎样去开发一个…

新一代AI带来更大想象空间!上海将打造元宇宙超级场景!

引子 上海市经信委主任吴金城4月12日在“2023上海民生访谈”节目表示&#xff0c;上海将着力建设元宇宙智慧医院、前滩东体元宇宙、张江数字孪生未来之城等元宇宙超级场景。 吴金城说&#xff0c;新一代人工智能将带来更大的想象空间。比如&#xff0c;人工智能和元宇宙数字人的…

实验7---myBatis和Spring整合

实验七 myBatis和Spring整合 一、实验目的及任务 通过该实验&#xff0c;掌握mybatis和spring整合方法&#xff0c;掌握生成mapper实现类的两种生成方式。 二、实验环境及条件 主机操作系统为Win10&#xff0c;Tomcat,j2sdk1.6或以上版本。 三、实验实施步骤 略 四、实验报告内…

wait()、sleep()、notify()的解析

wait()、sleep()、notify()的解析 【&#x1f388;问题1】&#xff1a;wait()、sleep()、notify()有什么作用&#xff1f;【&#x1f388;问题2】&#xff1a;wait()、sleep()的区别&#xff1f;【&#x1f388;问题3】&#xff1a;为什么 wait() 方法不定义在 Thread 中&…

九龙证券|今年最贵新股来了,本周还有超低价新股可申购

本周&#xff08;4月17日—4月21日&#xff09;&#xff0c;截至现在&#xff0c;共有3只新股将进行申购&#xff0c;别离为科创板的晶合集成、创业板的三博脑科、北交所的华原股份。其间华原股份将于周一申购&#xff0c;发行价为3.93元/股&#xff0c;晶合集成将于周四申购&a…

全国青少年软件编程(Scratch)等级考试一级考试真题2023年3月——持续更新.....

一、单选题(共25题&#xff0c;共50分) 1. 下列说法不正确的是&#xff1f;&#xff08; &#xff09; A.可以从声音库中随机导入声音 B.可以录制自己的声音上传 C.可以修改声音的大小 D.不能修改声音的速度 试题解析&#xff1a;针对声音可以进行导入&#xff0c;上传&…

Android 不同分辨率下的Drawable尺寸资源设置

启动器图标 36x36 (0.75x) 用于低密度48x48&#xff08;1.0x 基线&#xff09;用于中密度72x72 (1.5x) 用于高密度96x96 (2.0x) 用于超高密度144x144 (3.0x) 用于超超高密度192x192 (4.0x) 用于超超超高密度&#xff08;仅限启动器图标&#xff1b;请参阅上面的 注&#xff09…

redis 主从模式、哨兵模式、cluster模式的区别

参考&#xff1a; ​https://blog.csdn.net/qq_41071876/category_11284995.html https://blog.csdn.net/weixin_45821811/article/details/119421774 https://blog.csdn.net/weixin_43001336/article/details/122816402 Redis有三种模式&#xff0c;分别是&#xff1a;主…

【C++】STL——用一个哈希表封装出unordered_map和unordered_set

用一个哈希表(桶)封装出unordered_map和unordered_set 文章目录用一个哈希表(桶)封装出unordered_map和unordered_set一、哈希表源码二、哈希函数模板参数的控制三、对上层容器构建仿函数便于后续映射四、string类型无法取模问题五、哈希表默认成员函数实现1.构造函数2.拷贝构造…

【JavaEE】ConcurrentHashMap与Hashtable有什么区别?

博主简介&#xff1a;努力的打工人一枚博主主页&#xff1a;xyk:所属专栏: JavaEE初阶Hashtable、ConcurrentHashMap是使用频率较高的数据结构&#xff0c;它们都是以key-value的形式来存储数据&#xff0c;且都实现了Map接口&#xff0c;日常开发中很多人对其二者之间的区别并…