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

阿里巴巴能拿货在家里做的网站网店运营推广实训

阿里巴巴能拿货在家里做的网站,网店运营推广实训,济南市莱芜区招聘信息,做网盘搜索网站合法吗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/147198.html

相关文章:

  • 衡水高端网站建设网络营销的特点包括
  • 网站维护后期费用seo竞价排名
  • 网站建设具体实施方案广告优化师是做什么的
  • 单页网页设计图片安徽网络关键词优化
  • centos建设网站百度seo排名优化公司哪家好
  • uilike学堂快手seo关键词优化
  • 在本地搭建wordpress东莞百度快照优化排名
  • 怎么开通网站泉州seo培训
  • 委外网站开发合同模板浏览器打开网站
  • 未来销售最好的行业seoul是啥意思
  • 哈尔滨网站建设多少钱站长之家seo工具
  • 做精神科网站手机百度ai入口
  • 承德网站开发关键词搜索引擎工具爱站
  • 有产品做推广,选哪个 网站seo成都培训
  • perl网站建设做seo有什么好处
  • 高端网站建设哪里好广州关键词优化外包
  • 深圳网站建设美橙互联淘宝店铺推广方法
  • 重庆企业网站建设永久免费跨境浏览app
  • 厦门网站做的比较好近期国家新闻
  • 网站建设服务的会计处理360网站推广官网
  • 网页被禁止浏览怎么解决佛山网络排名优化
  • 北海网站建设怎么在网上做广告
  • 做摄影的网站知乎发外链比较好的平台
  • 网站 廉政建设 板块关键词排名seo优化
  • cms做门户网站电脑系统优化软件哪个好用
  • 响应式网页设计实例专业网站优化排名
  • 邯郸做网站的公司英雄联盟韩国
  • 有没有免费的网站服务器目前网络推广平台
  • 把网站做成手机版网站服务器怎么搭建
  • 广东做网站策划找关键词的方法与技巧