Guava

How to empty Guava cache every 30 seconds while sending it to another method?

纵饮孤独 提交于 2019-12-31 03:26:13
问题 I am populating my guava cache from multiple threads by calling add method. Now from the background thread which runs every 30 seconds, I want to send whatever is there in the cache to sendToDB method atomically? Below is my code: public class Example { private final ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor(); private final Cache<Integer, List<Process>> cache = CacheBuilder.newBuilder().maximumSize(100000) .removalListener(RemovalListeners

How to properly deal with exceptions coming from ListenableFuture guava?

血红的双手。 提交于 2019-12-31 02:45:08
问题 I have a library in which I have provided two methods, sync and async for our customer. They can call whichever method they feel is right for their purpose. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. They will pass DataKey object which has the user id in it. And we will figure out which machine to call basis on the user id. So we will make http call

异步编程之美

只愿长相守 提交于 2019-12-30 18:45:16
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在大学毕业后的工作期间,从最开始的编写同步代码,到慢慢为了提高系统性能,把一些任务使用异步的方式来处理,从而提高系统的响应时间,那么就会产生一些新的问题,如何监控在异步线程执行的任务的执行状态,是否出现了错误,出现了错误怎么处理,系统创建大量线程又该如何统一管理,这些种种问题都让使我意识到深入了解异步编程的必要性。 同步的代码, 在很多情况下, CPU其实是在等待中度过的, 比如等待一个网络连接, 等待MySQL服务器的数据返回异步的代码, 就是把这些等待的时间给充分利用起来了, 把网络连接, 访问数据库这种耗时的工作时都在注册一个callback或者event之后切换出来,让CPU先去干别的活(例如响应别的请求), 当网络连接,数据库返回结果时再回来执行刚才的callback中的代码,异步的代码可以大大的提升系统的容量上限, 因为充分利用了空闲的CPU时间, 但是对于单个的请求的性能提升帮助比较有限 (除非你的单个请求大量依赖这种IO操作)。 Java的异步编程其实是一个充分利用计算机CPU资源,不想让主程序阻塞在某个长时间运行的任务上,这类耗时的任务可以是IO操作、远程调用以及高密度计算任务。如果不使用多线程异步编程,我们的系统就会阻塞在耗时的子任务上,会导致极大延长完成主函数任务的时间

Guava MultiMap and ConcurrentModificationException [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-30 08:27:49
问题 This question already has answers here : Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop (24 answers) Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . I don't understand why I get a ConcurrentModificationException when I iterate through this multimap . I read the following entry, but I am not sure if I understood the whole thing. I tried to add a synchronized block. But my doubt is

Guava MultiMap and ConcurrentModificationException [duplicate]

佐手、 提交于 2019-12-30 08:27:01
问题 This question already has answers here : Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop (24 answers) Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . I don't understand why I get a ConcurrentModificationException when I iterate through this multimap . I read the following entry, but I am not sure if I understood the whole thing. I tried to add a synchronized block. But my doubt is

Is there a fast concat method for linked list in Java?

£可爱£侵袭症+ 提交于 2019-12-30 07:59:07
问题 How can I concat two linked lists in O(1) with Java via jdk1.6, google or apache commons collection or whatever? E.g. in the jdk there is only the addAll method which is O(n). Another feature I miss is to concat two lists where each of them could be in inverse order. To illustrate this assume two lists a->b->c and e->f->g could merged into a->b->c->e->f->g a->b->c->g->f->e c->b->a->e->f->g c->b->a->g->f->e Do you know of such a list implemenation or do I have to implement my own linked list?

Adding a key with an empty value to Guava Multimap

夙愿已清 提交于 2019-12-30 07:57:09
问题 I have a need to add a key to a Guava Multimap with an empty collection as the value. How do I accomplish this? I tried this: map.put( "my key", null ); but calling get() returns a list with one element, which is null. I worked around this by doing the following: map.putAll("my key2", new ArrayList()) but I'm wondering if this is a bad thing to do? I know Guava automatically removes a key when the last value is removed to keep containsKey() consistent. What's my best option here? 回答1:

Adding a key with an empty value to Guava Multimap

强颜欢笑 提交于 2019-12-30 07:57:08
问题 I have a need to add a key to a Guava Multimap with an empty collection as the value. How do I accomplish this? I tried this: map.put( "my key", null ); but calling get() returns a list with one element, which is null. I worked around this by doing the following: map.putAll("my key2", new ArrayList()) but I'm wondering if this is a bad thing to do? I know Guava automatically removes a key when the last value is removed to keep containsKey() consistent. What's my best option here? 回答1:

Guava way of sorting List according to another list?

↘锁芯ラ 提交于 2019-12-30 06:20:02
问题 I have List<Integer> consisting Ids of my Users. And after a database query, I am retrieving List<User> . I would like to order this list according to first Id list. List<User> may not include some of the Ids. What is the Guava way for sorting this list? 回答1: The fully "functional" way, using Guava, would combine Ordering#explicit() with Ordering#onResultOf() public class UserService { @Inject private UserDao userDao; public List<User> getUsersWithIds(List<Integer> userIds) { List<User> users

Guava way of sorting List according to another list?

懵懂的女人 提交于 2019-12-30 06:19:07
问题 I have List<Integer> consisting Ids of my Users. And after a database query, I am retrieving List<User> . I would like to order this list according to first Id list. List<User> may not include some of the Ids. What is the Guava way for sorting this list? 回答1: The fully "functional" way, using Guava, would combine Ordering#explicit() with Ordering#onResultOf() public class UserService { @Inject private UserDao userDao; public List<User> getUsersWithIds(List<Integer> userIds) { List<User> users