java.util.concurrent

How do java.util.concurrent.locks.Condition work?

一曲冷凌霜 提交于 2020-05-14 18:17:04
问题 Reading the Java 8 documentation about the java.util.concurrent.locks.Condition interface, the following example is given: class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(Object x) throws InterruptedException { lock.lock(); try { while (count == items.length) notFull.await(); items[putptr] = x; if (+

How do java.util.concurrent.locks.Condition work?

只愿长相守 提交于 2020-05-14 18:16:20
问题 Reading the Java 8 documentation about the java.util.concurrent.locks.Condition interface, the following example is given: class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(Object x) throws InterruptedException { lock.lock(); try { while (count == items.length) notFull.await(); items[putptr] = x; if (+

When to reset CyclicBarrier in java multithreading

半腔热情 提交于 2020-03-13 06:05:30
问题 I was reading CyclicBarrier in the following link http://java-latte.blogspot.in/2013/10/cyclicbarrier-in-java-concurrency.html. In the example 1, CyclicRaceDemo.java main method, CyclicBarrier is being reused without calling reset method. I ran the example and it worked fine. So, I am wondering what's the use of reset method. When should it be called? Or do we need to call it at all? 回答1: A CyclicBarrier is cyclic because it can be reused without resetting. From the Javadoc A synchronization

Behavior of entrySet().removeIf in ConcurrentHashMap

人走茶凉 提交于 2020-02-18 05:50:32
问题 I would like to use ConcurrentHashMap to let one thread delete some items from the map periodically and other threads to put and get items from the map at the same time. I'm using map.entrySet().removeIf(lambda) in the removing thread. I'm wondering what assumptions I can make about its behavior. I can see that removeIf method uses iterator to go through elements in the map, check the given condition and then remove them if needed using iterator.remove() . Documentation gives some info about

CompletableFuture — Aggregate Future to Fail Fast

て烟熏妆下的殇ゞ 提交于 2020-02-01 04:49:07
问题 I have been using the CompletableFuture.allOf(...) helper to create aggregate futures that will only become "done" when their composite futures are marked as complete, i.e: CompletableFuture<?> future1 = new CompletableFuture<>(); CompletableFuture<?> future2 = new CompletableFuture<>(); CompletableFuture<?> future3 = new CompletableFuture<>(); CompletableFuture<?> future = CompletableFuture.allOf(future1, future2, future3); I would like a slight variation on this functionality, where the

is there any Concurrent LinkedHashSet in JDK6.0 or other libraries?

半世苍凉 提交于 2020-01-22 23:01:54
问题 my code throw follow exception: java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761) at java.util.LinkedList$ListItr.next(LinkedList.java:696) at java.util.AbstractCollection.addAll(AbstractCollection.java:305) at java.util.LinkedHashSet.<init>(LinkedHashSet.java:152) ... I want a ConcurrentLinkedHashSet to fix it, but I only found ConcurrentSkipListSet in java.util.concurrent ,this is TreeSet , not LinkedHashSet any easies way

Difference between Phaser and CyclicBarrier

妖精的绣舞 提交于 2020-01-13 09:25:30
问题 I stumbled upon a doubt regarding a difference between CyclicBarrier and Phaser utilities in Java concurrent package. I understand that CyclicBarrier allows group of threads to wait until all threads arrive at a specific point. Phaser also do same but it supports multiple phases. I also understand that, a CyclicBarrier can be reused. I think this reuse facility makes its function same as Phaser . Consider following programs: Testing Phaser : import java.util.concurrent.Phaser; public class

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

◇◆丶佛笑我妖孽 提交于 2020-01-12 12:40:20
问题 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

Multithreaded execution where order of finished Work Items is preserved

試著忘記壹切 提交于 2020-01-11 16:45:09
问题 I have a flow of units of work, lets call them "Work Items" that are processed sequentially (for now). I'd like to speed up processing by doing the work multithreaded. Constraint: Those work items come in a specific order, during processing the order is not relevant - but once processing is finished the order must be restored. Something like this: |.| |.| |4| |3| |2| <- incoming queue |1| / | \ 2 1 3 <- worker threads \ | / |3| |2| <- outgoing queue |1| I would like to solve this problem in

How atomicity is achieved in the classes defined in java.util.concurrent.atomic package?

て烟熏妆下的殇ゞ 提交于 2020-01-03 08:26:15
问题 I was going through the source code of java.util.concurrent.atomic.AtomicInteger to find out how atomicity is achieved by the atomic operations provided by the class. For instance AtomicInteger.getAndIncrement() method source is as follows public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } } I am not able to understand the purpose of writing the sequence of operations inside a infinite for loop. Does