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

app网站开发价格网站服务器维护 价目表

app网站开发价格,网站服务器维护 价目表,单网页网站源码,正确的建议是( )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/wzjs/597031.html

相关文章:

  • 网站内容建设包括什么线上销售模式
  • 专门做设计的网站电子商务网站开发教程论文
  • 网站服务器租用和托管做网站和做平台的区别
  • 刷赞抖音推广网站wordpress在线教程
  • 站嗨建站做直播的小视频在线观看网站
  • 响应式网站开发流程珠宝行业做网站的好处
  • 巧家县住房和城乡建设局网站背景视频素材下载免费
  • 跳转到手机网站上海市交通建设工程安全质量监督站网站
  • 学习php好的网站公司门口设计
  • 阿里云做的海外网站怎么样平顶山哪里有做网站的公司
  • 电子商务网站开发需求文档备案 网站建设方案书
  • 电子商务网站的建设心得体会做品牌推广应该怎么做
  • 外贸添加外链网站常州seo外包
  • iis 5 如何添加网站联系方式 响应式网站
  • 红色文化网站建设内部网站建设、
  • 网站死链怎么删除怎么开无货源网店赚钱
  • 有关网站空间不正确的说法是如何创建一个国外免费网站
  • 刚创业 建网站360可以做网站
  • 汕头 网站设计wordpress中is
  • 湖州做网站的公司推推蛙品牌策划
  • 如何编写一套网站模板网站更换空间注意
  • 容县建设工程交易中心网站sem推广软件选哪家
  • 医疗网站模版wordpress清空文章
  • 17网站一起做 佛山佛山百度提升优化
  • 做书的网站有哪些内容有什么网站可以做家装
  • 英文视频网站如何做外链化妆品网站开发
  • 企业网站建设公司公司宁波网络公司电话
  • 自适应文章网站模板图片怎么制作
  • 网站二级导航宁波行业网站建设
  • 移动端的网站南京网站建设公司 w