• Guava工具


    1. 缓存
    2. 类型转化
    3. 字符分割
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>31.1-jre</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    package com.steven.springboot.redis.utils;
    
    import com.google.common.base.Joiner;
    import com.google.common.base.Splitter;
    import com.google.common.cache.Cache;
    import com.google.common.cache.CacheBuilder;
    import org.springframework.boot.autoconfigure.mail.MailProperties;
    
    import javax.validation.constraints.Max;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    public class GuavaUtil {
        private static Cache<String, Object> dataMap = CacheBuilder.newBuilder()
                //设置cache的初始大小为10,要合理设置该值
                .initialCapacity(100)
                .maximumSize(1000)
                //设置并发数为5,即同一时间最多只能有5个线程往cache执行写入操作
                .concurrencyLevel(5)
                //设置cache中的数据在写入之后的存活时间
                .expireAfterWrite(3, TimeUnit.SECONDS)
                .build();
    
        public static void put(String key, Object value) {
            dataMap.put(key, value);
        }
    
        /**
         * 如果key不存在,返回null
         */
        public static Object get(String key) {
            return dataMap.getIfPresent(key);
        }
    
        /**
         * 移除key
         */
        public static void remove(String key) {
            dataMap.invalidate(key);
        }
    
        /**
         * 字符串分割
         *
         * @param list 集合
         * @return 字符串
         */
        public static String spliter(List<Object> list) {
            return Joiner.on(",").join(list);
        }
    
        public static String mapToString(Map<String, Object> map) {
            return Joiner.on(",").withKeyValueSeparator("=").join(map);
        }
    
        /**
         * 将String转化为list
         * @param str 带转化字符串
         * @return 转化成的list
         */
        public static List<String> stringToList(String str){
            return Splitter.on(",").splitToList(str);
        }
    
        public static void main(String[] args) throws InterruptedException {
            /*GuavaCacheUtil.put("name","jack");*/
            /*//jack*/
            /*System.out.println(GuavaCacheUtil.get("name"));*/
            /*GuavaCacheUtil.remove("name");*/
            /*//null*/
            /*System.out.println(GuavaCacheUtil.get("name"));*/
          /*  String split = spliter(Arrays.asList(1, 2, 3, 4, 5));
            System.out.println(split);*/
            /*HashMap<String, Object> result = new HashMap<>();
            result.put("1", "张三");
            result.put("2", "李四");
            String string = mapToString(result);
            System.out.println(string);*/
            List<String> strings = stringToList("1,2,3,4");
            System.out.println(strings);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    public static void main(String[] args) throws InterruptedException {
            ThreadFactory factory = new DefaultThreadFactory("task", 4);
            ExecutorService threadPool = new ThreadPoolExecutor(3, 6, 20, TimeUnit.MINUTES, new ArrayBlockingQueue<>(30), factory, new ThreadPoolExecutor.DiscardPolicy());
            ListeningExecutorService service = MoreExecutors.listeningDecorator(threadPool);
            ListenableFuture<String> future = service.submit(() -> {
                System.out.println("任务在执行!!!");
                return "task";
            });
    
            future.addListener(()->{
                try {
                    future.get();
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }, service);
    
            Futures.addCallback(future, new FutureCallback<String>(){
                @Override
                public void onSuccess(String s) {
                    System.out.println("callback result:"+s);
                }
    
                @Override
                public void onFailure(Throwable throwable) {
                    System.out.println(throwable.getMessage());
                }
            },service);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    这几个小妙招让你学会如何压缩图片大小
    07-Go语言中map数据结构用法
    模型预测控制(MPC)十一:变量约束的预测控制
    【计算机视觉】不来试试图片轮廓提取?
    docker中安装redis详解
    第十四届蓝桥杯第二期模拟赛题解
    openGauss学习笔记-93 openGauss 数据库管理-访问外部数据库-oracle_fdw
    msvcr110dll是干嘛的,win系统提示缺少msvcr110.dll解决步骤分享
    最新!2024年影响因子正式发布!CNS竟普降这么多
    旅游景区度假村展示型网站如何建设渠道品牌
  • 原文地址:https://blog.csdn.net/SuperstarSteven/article/details/125536093