Guava

Refresh Guava LoadingCache everyday at a specific time

六月ゝ 毕业季﹏ 提交于 2020-06-26 05:33:32
问题 I need that my cache be refreshed everyday at a specific time, in my case, at midnight. I have way to do this with Guava LoadingCache? So far I only got the cache be renewed after a day, with the next code: private final LoadingCache<String, Long> cache = CacheBuilder.newBuilder() .refreshAfterWrite(1, TimeUnit.DAYS) .build(new CacheLoader<String, Long>() { public Long load(String key) { return getMyData("load", key); } } 回答1: Here's a code snipped that implements JB Nizeth's answer (Java 8):

Refresh Guava LoadingCache everyday at a specific time

房东的猫 提交于 2020-06-26 05:33:23
问题 I need that my cache be refreshed everyday at a specific time, in my case, at midnight. I have way to do this with Guava LoadingCache? So far I only got the cache be renewed after a day, with the next code: private final LoadingCache<String, Long> cache = CacheBuilder.newBuilder() .refreshAfterWrite(1, TimeUnit.DAYS) .build(new CacheLoader<String, Long>() { public Long load(String key) { return getMyData("load", key); } } 回答1: Here's a code snipped that implements JB Nizeth's answer (Java 8):

java.lang.NoSuchMethodError: org.openqa.selenium.support.ui.Wait.until(Lcom/google/common/base/Function;) using selenium-server-standalone-3.12.0

蓝咒 提交于 2020-06-23 07:03:31
问题 I've been struggling with selenium to fix this issue: java.lang.NoSuchMethodError: org.openqa.selenium.support.ui.Wait.until(Lcom/google/common/base/Function;)Ljava/lang/Object; This is where I get this error: Wait<WebDriver> wait = new FluentWait<>(getDriverInstance()) .withTimeout(timeout, TimeUnit.SECONDS) .pollingEvery(frequency, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); wait.until(driver -> { assert driver != null; elt.click(); return true; }); The most solutions on the

Can Guava’s TypeToken get the specific type of a generic field?

好久不见. 提交于 2020-06-09 05:34:12
问题 I wrote a parser for a file format called ASN.1 that uses Guice’s TypeLiteral.getFieldType(Field) to convert generic fields into specific Java types so I can construct the correct type (similar to Jackson or GSON databinding). But since I already depend on Guava and it seems to have a new TypeLiteral replacement, I’d like to use TypeToken instead. According to the Guave TypeToken documentation: TypeToken is similar to Guice's TypeLiteral class, but with one important difference: it supports

Guava Vs Apache Commons Hash/Equals builders

爱⌒轻易说出口 提交于 2020-05-14 14:49:48
问题 I was wondering what are key differences between Guava vs Apache Commons with respect to equals and hashCode builders. equals : Apache Commons: public boolean equals(Object obj) { if (obj == null) { return false; } if (obj == this) { return true; } if (obj.getClass() != getClass()) { return false; } MyClass other = (MyClass) obj; return new EqualsBuilder() .appendSuper(super.equals(obj)) .append(field1, other.field1) .append(field2, other.field2) .isEquals(); } Guava: public boolean equals

布隆过滤器(Bloom Filter)的原理和实现

柔情痞子 提交于 2020-05-08 18:49:11
什么情况下需要布隆过滤器? 先来看几个比较常见的例子 字处理软件中,需要检查一个英语单词是否拼写正确 在 FBI,一个嫌疑人的名字是否已经在嫌疑名单上 在网络爬虫里,一个网址是否被访问过 yahoo, gmail等邮箱垃圾邮件过滤功能 这几个例子有一个共同的特点: 如何判断一个元素是否存在一个集合中? 常规思路 数组 链表 树、平衡二叉树、Trie Map (红黑树) 哈希表 虽然上面描述的这几种数据结构配合常见的排序、二分搜索可以快速高效的处理绝大部分判断元素是否存在集合中的需求。但是当集合里面的元素数量足够大,如果有500万条记录甚至1亿条记录呢?这个时候常规的数据结构的问题就凸显出来了。数组、链表、树等数据结构会存储元素的内容,一旦数据量过大,消耗的内存也会呈现线性增长,最终达到瓶颈。有的同学可能会问,哈希表不是效率很高吗?查询效率可以达到O(1)。但是哈希表需要消耗的内存依然很高。使用哈希表存储一亿 个垃圾 email 地址的消耗?哈希表的做法:首先,哈希函数将一个email地址映射成8字节信息指纹;考虑到哈希表存储效率通常小于50%(哈希冲突);因此消耗的内存:8 * 2 * 1亿 字节 = 1.6G 内存,普通计算机是无法提供如此大的内存。这个时候,布隆过滤器(Bloom Filter)就应运而生。在继续介绍布隆过滤器的原理时,先讲解下关于哈希函数的预备知识。 哈希函数

SpringCloud系列——限流、熔断、降级

喜你入骨 提交于 2020-05-08 10:01:23
  前言   分布式环境下,服务直接相互调用,一个复杂的业务可能要调用多个服务,例如A -> B -> C -> D,当某个服务出现异常(调用超时、调用失败等)将导致整个流程阻塞崩溃,严重的整个系统都会崩掉,为了实现高可用,必要的保护机制必不可少   本文记录限流、熔断、降级的实现处理   限流   我们采用令牌桶限流法,并自己实现一个简单令牌桶限流   有个任务线程以恒定速率向令牌桶添加令牌   一个请求会消耗一个令牌,令牌桶里的令牌大于0,才会放行,反正不允许通过 /** * 简单的令牌桶限流 */ public class RateLimiter { /** * 桶的大小 */ private Integer limit; /** * 桶当前的token */ private static Integer tokens = 0 ; /** * 构造参数 */ public RateLimiter(Integer limit, Integer speed){ // 初始化桶的大小,且桶一开始是满的 this .limit = limit; tokens = this .limit; // 任务线程:每秒新增speed个令牌 new Thread(() -> { while ( true ){ try { Thread.sleep( 1000L ); int newTokens

Introduction to Thread Pools in Java

醉酒当歌 提交于 2020-05-07 18:38:15
1. Introduction This article is a look at thread pools in Java – starting with the different implementations in the standard Java library and then looking at Google’s Guava library. 2. The Thread Pool In Java, threads are mapped to system-level threads which are operating system’s resources. If you create threads uncontrollably, you may run out of these resources quickly. The context switching between threads is done by the operating system as well – in order to emulate parallelism. A simplistic view is that – the more threads you spawn, the less time each thread spends doing actual work. The

面试官:来,手写一个线程安全并且可以设置过期时间的LRU缓存

耗尽温柔 提交于 2020-05-06 23:08:12
划重点,手写一个 LRU 缓存在面试中还是挺常见的! 很多人就会问了:“网上已经有这么多现成的缓存了!为什么面试官还要我们自己实现一个呢?”。 咳咳咳,当然是为了面试需要。哈哈!开个玩笑,我个人觉得更多地是为了学习吧! 今天教大家: 实现一个线程安全的 LRU 缓存 实现一个线程安全并且带有过期时间的 LRU 缓存 考虑到了线程安全性我们使用了 ConcurrentHashMap 、ConcurrentLinkedQueue 这两个线程安全的集合。另外,还用到 ReadWriteLock(读写锁)。 为了实现带有过期时间的缓存,我们用到了 ScheduledExecutorService来做定时任务执行。 如果有任何不对或者需要完善的地方,请帮忙指出! 1. LRU 缓存介绍 LRU (Least Recently Used,最近最少使用)是一种缓存淘汰策略。 LRU缓存指的是当缓存大小已达到最大分配容量的时候,如果再要去缓存新的对象数据的话,就需要将缓存中最近访问最少的对象删除掉以便给新来的数据腾出空间。 2. ConcurrentLinkedQueue简单介绍 ConcurrentLinkedQueue是一个基于单向链表的无界无锁线程安全的队列,适合在高并发环境下使用,效率比较高。 我们在使用的时候,可以就把它理解为我们经常接触的数据结构——队列

Java中线程池,你真的会用吗?ExecutorService ThreadPoolExcutor

家住魔仙堡 提交于 2020-05-04 06:14:19
原文: https://www.hollischuang.com/archives/2888 在《 深入源码分析Java线程池的实现原理 》这篇文章中,我们介绍过了Java中线程池的常见用法以及基本原理。 在文中有这样一段描述: 可以通过Executors静态工厂构建线程池,但一般不建议这样使用。 关于这个问题,在那篇文章中并没有深入的展开。作者之所以这么说,是因为这种创建线程池的方式有很大的隐患,稍有不慎就有可能导致线上故障,如:一次Java线程池误用引发的血案和总结( https://zhuanlan.zhihu.com/p/32867181 ) 本文我们就来围绕这个问题来分析一下为什么JDK自身提供的构建线程池的方式并不建议使用?到底应该如何创建一个线程池呢? Executors Executors 是一个Java中的工具类。提供工厂方法来创建不同类型的线程池。  从上图中也可以看出,Executors的创建线程池的方法,创建出来的线程池都实现了ExecutorService接口。常用方法有以下几个: newFiexedThreadPool(int Threads) :创建固定数目线程的线程池。 newCachedThreadPool() :创建一个可缓存的线程池,调用execute 将重用以前构造的线程(如果线程可用)。如果没有可用的线程,则创建一个新线程并添加到池中