|
| 1 | +import java.util.Map; |
| 2 | +import java.util.concurrent.*; |
| 3 | + |
| 4 | +/** |
| 5 | + * @Description: 利用future避免重复计算 + 缓存过期到期自动失效 |
| 6 | + * @Author: zzStar |
| 7 | + * @Date: 2020/10/13 13:45 |
| 8 | + */ |
| 9 | +public class CacheUsage5<A, V> implements Computable<A, V> { |
| 10 | + |
| 11 | + private final Map<A, Future<V>> cache = new ConcurrentHashMap<>(); |
| 12 | + |
| 13 | + private final Computable<A, V> computable; |
| 14 | + |
| 15 | + public CacheUsage5(Computable<A, V> computable) { |
| 16 | + this.computable = computable; |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + @Override |
| 21 | + public V compute(A arg) { |
| 22 | + Future<V> future = cache.get(arg); |
| 23 | + if (future == null) { |
| 24 | + Callable<V> callable = () -> computable.compute(arg); |
| 25 | + FutureTask<V> futureTask = new FutureTask<>(callable); |
| 26 | + |
| 27 | + //原子组合操作,每一个线程进来都先做一次判断 |
| 28 | + future = cache.putIfAbsent(arg, futureTask); |
| 29 | + if (future == null) { |
| 30 | + future = futureTask; |
| 31 | + System.out.println("从FutureTask调用计算函数"); |
| 32 | + futureTask.run(); |
| 33 | + } |
| 34 | + //计算之前放入缓存 |
| 35 | + //cache.put(arg, futureTask); |
| 36 | + } |
| 37 | + V v = null; |
| 38 | + try { |
| 39 | + //计算的时候有可能会出现异常 |
| 40 | + v = future.get(); |
| 41 | + } catch (InterruptedException | ExecutionException e) { |
| 42 | + System.out.println("出现异常 " + e.getMessage()); |
| 43 | + //取消线程任务,发送中断信号 |
| 44 | + future.cancel(true); |
| 45 | + } |
| 46 | + //在有结果之前会阻塞 |
| 47 | + return v; |
| 48 | + } |
| 49 | + |
| 50 | + //延迟功能 |
| 51 | + private final static ScheduledExecutorService service = Executors.newScheduledThreadPool(5); |
| 52 | + |
| 53 | + //缓存过期功能,重载 |
| 54 | + public V compute(A arg, long expire) { |
| 55 | + //超时时间>0 |
| 56 | + if (expire > 0) { |
| 57 | + service.schedule(() -> { |
| 58 | + //如果还在计算,则直接取消任务 |
| 59 | + Future<V> vFuture = cache.get(arg); |
| 60 | + if (vFuture != null) { |
| 61 | + if (!vFuture.isDone()) { |
| 62 | + vFuture.cancel(true); |
| 63 | + } |
| 64 | + } |
| 65 | + cache.remove(arg); |
| 66 | + System.out.println("过期时间到了,清除缓存:" + arg); |
| 67 | + }, expire,TimeUnit.MILLISECONDS); |
| 68 | + } |
| 69 | + service.shutdown(); |
| 70 | + return compute(arg); |
| 71 | + } |
| 72 | + |
| 73 | + //把缓存的时间设置为随机,过期时间不设置为统一,避免缓存雪崩 |
| 74 | + public V computeRandomExpire(A arg) { |
| 75 | + long expire = (long) (Math.random() * 10000); |
| 76 | + return compute(arg, expire); |
| 77 | + } |
| 78 | + |
| 79 | + public static void main(String[] args) { |
| 80 | + CacheUsage5<String, Integer> usage5 = new CacheUsage5<>(new ExpensiveFunc()); |
| 81 | + new Thread(() -> { |
| 82 | + try { |
| 83 | + Integer result = usage5.computeRandomExpire("1222"); |
| 84 | + System.out.println(Thread.currentThread().getName() + " result=" + result); |
| 85 | + } catch (Exception e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + }).start(); |
| 89 | + new Thread(() -> { |
| 90 | + try { |
| 91 | + Integer result = usage5.compute("1222"); |
| 92 | + System.out.println(Thread.currentThread().getName() + " result=" + result); |
| 93 | + } catch (Exception e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } |
| 96 | + }).start(); |
| 97 | + new Thread(() -> { |
| 98 | + try { |
| 99 | + Integer result = usage5.compute("1222"); |
| 100 | + System.out.println(Thread.currentThread().getName() + " result=" + result); |
| 101 | + } catch (Exception e) { |
| 102 | + e.printStackTrace(); |
| 103 | + } |
| 104 | + }).start(); |
| 105 | + } |
| 106 | +} |
0 commit comments