当前位置: 首页 > news >正文

做自媒体视频搬运网站网站建设与管理淘宝

做自媒体视频搬运网站,网站建设与管理淘宝,深圳公司网站建设哪家好,虎年ppt模板免费下载1. 功能: map 线程安全,能够对存入的数据设置过期,或者自定义删除 2. aliyun代码看到的一个对象正好符合上述需求 出处是aliyun sdk core jar包的一个类。感兴趣可以去下载下jar查看 下面是源码: package com.aliyuncs.policy.…

1. 功能:

            map 线程安全,能够对存入的数据设置过期,或者自定义删除

2. aliyun代码看到的一个对象正好符合上述需求

    出处是aliyun sdk core jar包的一个类。感兴趣可以去下载下jar查看

下面是源码:

package com.aliyuncs.policy.cache;import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;public class ThrottlingPool {private static final Map<String, Entity> map = new ConcurrentHashMap();private static final ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, (new BasicThreadFactory.Builder()).namingPattern("throttling-pool-%d").daemon(true).build());public ThrottlingPool() {}public static synchronized void put(String key, Object data) {put(key, data, -1);}public static synchronized void put(final String key, Object data, int expire) {remove(key);if (data != null) {if (expire >= 0) {Future future = executor.schedule(new Runnable() {public void run() {synchronized(ThrottlingPool.class) {ThrottlingPool.map.remove(key);}}}, (long)expire, TimeUnit.MILLISECONDS);map.put(key, new Entity(data, expire, future));} else {map.put(key, new Entity(data, expire, (Future)null));}}}public static synchronized Object get(String key) {Entity entity = (Entity)map.get(key);return entity != null ? entity.getValue() : null;}public static synchronized <T> T get(String key, Class<T> clazz) {return (T)clazz.cast(get(key));}public static synchronized int getExpire(String key) {Entity entity = (Entity)map.get(key);return entity != null ? entity.getExpire() : 0;}public static synchronized Object remove(String key) {Entity entity = (Entity)map.remove(key);if (entity == null) {return null;} else {Future future = entity.getFuture();if (future != null) {future.cancel(true);}return entity.getValue();}}public static synchronized int size() {return map.size();}public static synchronized void clear() {for(Entity entity : map.values()) {if (entity != null) {Future future = entity.getFuture();if (future != null) {future.cancel(true);}}}map.clear();}public static synchronized Map<String, Entity> getPool() {return map;}private static class Entity {private Object value;private int expire;private Future future;public Entity(Object value, int expire, Future future) {this.value = value;this.expire = expire;this.future = future;}public Object getValue() {return this.value;}public int getExpire() {return this.expire;}public Future getFuture() {return this.future;}}
}

2. 但是有个问题,如果数据量大,且都设置有过期时间,容易过期不及时!单线程处理不过来

3. 下面代码采用延迟队列一版:

import java.util.concurrent.*;public class ThrottlingPool {private static final ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();private static final DelayQueue<DelayedCacheEntry> delayQueue = new DelayQueue<>();private static final ExecutorService executor = Executors.newSingleThreadExecutor(r -> {Thread t = new Thread(r);t.setDaemon(true);return t;});public static void put(String key, Object data, long expireMs) {long expirationTime = System.currentTimeMillis() + expireMs;delayQueue.removeIf(entry -> entry.getKey().equals(key));map.put(key, data);delayQueue.offer(new DelayedCacheEntry(key, expirationTime));}public static Object get(String key) {return map.get(key);}public static void remove(String key) {map.remove(key);}public static int size() {return map.size();}// 启动一个后台线程处理过期任务static {executor.execute(() -> {while (!Thread.currentThread().isInterrupted()) {try {DelayedCacheEntry entry = delayQueue.take();
//                    synchronized (ThrottlingPool.class) {map.remove(entry.getKey());
//                    }} catch (InterruptedException ex) {ex.printStackTrace();Thread.currentThread().interrupt();break;}}});// 钩子Runtime.getRuntime().addShutdownHook(new Thread(() -> {executor.shutdown();}));
//        Thread cleanupThread = new Thread(() -> {
//            while (true) {
//                try {
//                    DelayedCacheEntry entry = delayQueue.take();
//                    synchronized (ThrottlingPool.class) {
//                        map.remove(entry.getKey());
//                    }
//                } catch (InterruptedException e) {
//                    Thread.currentThread().interrupt();
//                    break;
//                }
//            }
//        });
//        cleanupThread.setDaemon(true);
//        cleanupThread.start();}private static class DelayedCacheEntry implements Delayed {private final String key;private final long expirationTime;public DelayedCacheEntry(String key, long expirationTime) {this.key = key;this.expirationTime = expirationTime;}@Overridepublic long getDelay(TimeUnit unit) {long diff = expirationTime - System.currentTimeMillis();return unit.convert(diff, TimeUnit.MILLISECONDS);}@Overridepublic int compareTo(Delayed o) {return Long.compare(this.expirationTime, ((DelayedCacheEntry) o).expirationTime);}public String getKey() {return key;}}}

4.本人水平有限,如有问题,欢迎指正

http://www.dtcms.com/a/452837.html

相关文章:

  • IP 协议的相关特性
  • 《投资-88》价值投资者的认知升级与交易规则重构 - 第三层:估值安全边际,“再好的公司,如果买贵了,也会变成一笔糟糕的投资。”
  • 工程师 - Raspberry Pi Pico程序:读取SPI数据后从串口输出
  • 虚幻引擎5 GAS开发俯视角RPG游戏 P04-12 可缩放浮点数的曲线表
  • 接口请求工具对比 apifox apipost swagger postman等
  • C++联合体(Union)详解:与结构体的区别、联系与深度解析
  • LangChain部署RAG part2.搭建多模态RAG引擎(赋范大模型社区公开课听课笔记)
  • SSM--day4--SpringMVC(补充)
  • Flink Checkpoint与反压问题排查手册:从日志分析到根因定位
  • 元宇宙的教育应用:重构学习体验与知识传递
  • 建设99网站江西网站开发哪家好
  • RabbitMQ高可用集群搭建教程(基于CentOS 7.9 + Erlang 23.2.7 + RabbitMQ 3.8.8)
  • 【LangChain】P14 LangChain 输出解析器深度解析:Json解析器、XML解析器、字符串及列表、日期解析器
  • 仿真软件-多机器人2
  • 《基于 ERT 的稀疏电极机器人皮肤技术》ICRA2020论文解析
  • 聚焦CRISPR技术配套工具链的开源生态建设
  • 网站做视频窗口接口收费么免费搭建自己的网站
  • ​​Avalonia UI 开发核心注意事项:从理念到部署的避坑指南​
  • 从chatGPT获取的关于相机焦距与其他参数的关系
  • 拒绝做网站的理由wordpress自适应 slide
  • 【IT老齐456】Spring Boot优雅开发多线程应用,笔记01
  • 网站收录怎么弄极路由4 做网站
  • 备考华为HCIA - 云计算,培训与自学到底该怎么选?
  • 106、23种设计模式之备忘录模式(15/23)
  • LangChain部署rag Part3olmOCR与MinerU工具(赋范大模型社区公开课听课笔记)
  • C++进阶:使用普通函数重载算数运算符
  • 从内核调优到集群部署:基于Linux环境下KingbaseES数据库安装指南
  • Micro850 控制器深度解析:硬件特性与 I/O 接线核心(罗克韦尔2)
  • Python oct() 函数
  • (一) 机器学习之深度神经网络