java.util.concurrent

How to Block and wait using AtomicBoolean

非 Y 不嫁゛ 提交于 2019-12-04 01:03:09
I am looking for a way of pausing a Thread. I started with affectively using a boolean flag (called 'paused'), and wrapping a check with a while loop (pause). Within the while loop there’s a Thread.wait() to block the execution. I’ve been looking at the AtomicBoolean , which seems to do the trick apart from it doesn’t block. Is there a alternative or extended version of AtomicBoolean that has a block method ? i.e. something like AtomicBoolean.getFalse() of AtomoicBoolean.get(false) ? They have a Blocking Queue, so a Blocking value. Current setup is : while (paused.get()) { synchronized (paused

Why are ConcurrentSkipListSet ascending Iterators 'faster' than descending ones?

白昼怎懂夜的黑 提交于 2019-12-04 00:36:52
I’m using the descendingIterator method on ConcurrentSkipListSet. I’ve just checked the documentation and noticed the following comment: ‘Ascending ordered views and their iterators are faster than descending ones.’ See https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#descendingIterator-- Unfortunately it doesn’t provide any more information on this. What kind of performance difference is there? is it significant? and why is there a performance difference? If you look at the Wikipedia page for Skip Lists you will see that they are effectively a

Hystrix command fails with “timed-out and no fallback available”

感情迁移 提交于 2019-12-03 23:50:51
I've noticed that some of the commands in my application fail with Caused by: ! com.netflix.hystrix.exception.HystrixRuntimeException: GetAPICommand timed-out and no fallback available. out: ! at com.netflix.hystrix.HystrixCommand.getFallbackOrThrowException(HystrixCommand.java:1631) out: ! at com.netflix.hystrix.HystrixCommand.access$2000(HystrixCommand.java:97) out: ! at com.netflix.hystrix.HystrixCommand$TimeoutObservable$1$1.tick(HystrixCommand.java:1025) out: ! at com.netflix.hystrix.HystrixCommand$1.performBlockingGetWithTimeout(HystrixCommand.java:621) out: ! at com.netflix.hystrix

java.util.ConcurrentModificationException android after remove elements from array list

﹥>﹥吖頭↗ 提交于 2019-12-03 23:24:12
I have the folloing code in my android app: /** * callback executed after fetching the data. */ public void OnPointsFetch(ArrayList<Shop> result) { toggleLoader(false); this.shops = result; if(activeFilter == Constants.POINTS_FILTER_AVAILABLE){ for(Shop s : result){ if(s.getClientPoints().getPointsAvailable() == 0){ this.shops.remove(s); } } } else{ for(Shop s : result){ if(s.getClientPoints().getPointsSpent() == 0){ this.shops.remove(s); } } } ptsListAdapter.setCollection(this.shops); ptsListAdapter.setFilter(this.activeFilter); } This method is called on the result of an async task. I need

Single threading a task without queuing further requests

我是研究僧i 提交于 2019-12-03 17:43:45
问题 I have a requirement for a task to be executed asynchronously while discarding any further requests until the task is finished. Synchronizing the method just queues up the tasks and doesn't skip. I initially thought to use a SingleThreadExecutor but that queues up tasks as well. I then looked at the ThreadPoolExecutor but it reads the queue to get the task to be executed and therefore will have one task executing and a minimum of one task queued (the others can be discarded using

What's the different between LinkedBlockingQueue and ConcurrentLinkedQueue?

限于喜欢 提交于 2019-12-03 16:16:05
I've read the blog, but i'm not sure whether his conclusion is correct : http://www.javacodegeeks.com/2010/09/java-best-practices-queue-battle-and.html#ixzz1seaiSLwp He said : As you can see from the provided performance results LinkedBlockingQueue achieved the best combined (adding and removing elements) performance results and should be your number one candidate for implementing producer – consumer schenarios. I wonder that, doen't it faster if i don't use lock in my code ? So why the LinkedBlockingQueue is faster than the lock-free Queue(ConcurrentLinkedQueue) ? Thanks !

ThreadPoolExecutor and the queue

做~自己de王妃 提交于 2019-12-03 13:34:03
问题 I thought that using ThreadPoolExecutor we can submit Runnable s to be executed either in the BlockingQueue passed in the constructor or using the execute method. Also my understanding was that if a task is available it will be executed. What I don't understand is the following: public class MyThreadPoolExecutor { private static ThreadPoolExecutor executor; public MyThreadPoolExecutor(int min, int max, int idleTime, BlockingQueue<Runnable> queue){ executor = new ThreadPoolExecutor(min, max,

ConcurrentLinkedQueue$Node remains in heap after remove()

妖精的绣舞 提交于 2019-12-03 12:40:49
问题 I have a multithreaded app writing and reading a ConcurrentLinkedQueue, which is conceptually used to back entries in a list/table. I originally used a ConcurrentHashMap for this, which worked well. A new requirement required tracking the order entries came in, so they could be removed in oldest first order, depending on some conditions. ConcurrentLinkedQueue appeared to be a good choice, and functionally it works well. A configurable amount of entries are held in memory, and when a new entry

Per-key blocking Map in Java

99封情书 提交于 2019-12-03 12:22:37
I'm dealing with some third-party library code that involves creating expensive objects and caching them in a Map . The existing implementation is something like lock.lock() try { Foo result = cache.get(key); if (result == null) { result = createFooExpensively(key); cache.put(key, result); } return result; } finally { lock.unlock(); } Obviously this is not the best design when Foos for different keys can be created independently. My current hack is to use a Map of Futures : lock.lock(); Future<Foo> future; try { future = allFutures.get(key); if (future == null) { future = executorService

Reinitialize fix delay in ScheduledExecutorService

♀尐吖头ヾ 提交于 2019-12-03 12:20:18
As per my requirement, I have to execute some particular code after certain period of time. To do the same I have chose ScheduledExecutorService.scheduleWithFixedDelay(runnable, 0, 5, TimeUnit.SECONDS) and it's working fine for me. But according to my another requirement, the time mentioned in fixedDelay should be configurable at runtime. Means, currently total Delay is 5 seconds but latter if user want then can change the time into 60 seconds and in runtime fixedDelay will run after 60 seconds. Any help would be appreciable. Please see the code: static int i = 0; static